创建MoveTo和MoveBy的实例时,第一个参数是动作时间间隔,第二个参数是位置坐标。两者的区别在于,前者是移动到位置坐标所指的位置,而后者是在当前位置的基础上移动位置坐标所代表的像素值.
MoveActionScene.cpp文件
#include"MoveActionScene.h"
Scene* MoveAction::createScene(){
auto scene = Scene::create();
auto layer = MoveAction::create();
scene->addChild(layer);
return scene;
}
bool MoveAction::init(){
if (!Layer::init()){
return false;
}
//获得屏幕尺寸大小
Size visibleSize = Director::getInstance()->getVisibleSize();
//创建一个精灵
auto plane = Sprite::create("plane.png");
//设置精灵位置,精灵在屏幕的最左边,精灵的高度为屏幕的一半。
//getContentSize是父类Node的属性,返回节点的大小
//无论节点如何缩放或旋转,contentSize属性值始终不变,此处设置精灵坐标,Node的position属性是相对于锚点的
//而锚点位于纹理图像的几何中心
plane->setPosition(Point(plane->getContentSize().width / 2, visibleSize.height / 2));
//将精灵添加为当前层的子节点
this->addChild(plane);
//创建一个moveTo动作
auto moveTo = MoveTo::create(5, Point(860, 320));
//精灵执行moveto动作,将在5秒内从当前位置移动到像素(860,320)的位置
plane->runAction(moveTo);
auto plane1 = Sprite::create("plane.png");
//设置飞机精灵位置,精灵在屏幕的最左边,精灵的高度为屏幕的一半
plane1->setPosition(Point(plane1->getContentSize().width / 2, visibleSize.height / 2));
//将精灵添加为当前层的子节点
this->addChild(plane1);
//定义一个moveBy动作
auto moveBy = MoveBy::create(5, Point(860, 320));
//精灵执行moveBy动作,将在5秒内从原位置向右移动860像素,向上移动320像素
plane1->runAction(moveBy);
return true;
}