1. MoveTo 和 MoveBy
MoveTo 就是移动到指定坐标的一个动作。
MoveTo::create(float duration, const Vec2& deltaPosition)
float duration :动作执行持续时间,单位为秒。
const Vec2& deltaPosition:指定移动的目的坐标。
MoveBy就是移动距离
MoveBy::create(float duration, const Vec2& deltaPosition)
float duration :动作执行持续时间,单位为秒。
const Vec2& deltaPosition:指定移动的距离。
-
auto sprite = Sprite::create("sprite.png");
-
-
// position the sprite on the center of the screen
-
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
-
-
// add the sprite as a child to this layer
-
this->addChild(sprite, 0);
-
-
//创建MoveTo动作对象
-
auto moveto = MoveTo::create(0.9f,Point(100,100));
-
-
//创建Moveby动作对象
-
auto moveby = MoveBy::create(0.9f,Point(100,100));
-
-
//精灵执行动作
-
//sprite->runAction(moveto);
-
-
sprite->runAction(moveby);
2. ScaleTo 和ScaleBy
scaleTo:不管精灵当前的拉伸倍数是多少,它都会把精灵缩放到指定的倍数。
ScaleTo::create(float duration, float sx, float sy)
float duration:动作的持续时间,单位为秒
float sx :X方向的拉伸值
float sy:Y方向的拉伸值
scaleBy:是在精灵当前的拉伸倍数的基础上,再次进行拉伸。例如精灵当前的拉伸倍数2.0,指定精灵x拉伸2.0f,y拉伸1.0f,则精灵的当前x拉伸倍数变为4.0f,就是在原来的2.0的基础上x再放大两倍。
ScaleBy::create(float duration, float sx, float sy)
float duration:动作的持续时间,单位为秒
float sx :X方向的拉伸值
float sy:Y方向的拉伸值
-
auto sprite = Sprite::create("sprite.png");
-
// position the sprite on the center of the screen
-
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
-
-
auto sprite2 = Sprite::create("sprite.png");
-
// position the sprite on the center of the screen
-
sprite2->setPosition(Vec2(visibleSize.width/2 + 200, visibleSize.height/2 + origin.y));
-
sprite2->setScale(2.0f);
-
-
auto sprite3 = Sprite::create("sprite.png");
-
// position the sprite on the center of the screen
-
sprite3->setPosition(Vec2(visibleSize.width/2 - 200, visibleSize.height/2 + origin.y));
-
sprite3->setScale(2.0f);
-
-
// add the sprite as a child to this layer
-
this->addChild(sprite, 0);
-
this->addChild(sprite2, 1);
-
this->addChild(sprite3);
-
-
//创建ScaleTo对象
-
auto scaleTo = ScaleTo::create(2.8f,1.0f,1.0f);
-
-
//创建ScaleBy对象
-
auto scaleBy = ScaleBy::create(2.8f,2.0f,1.0f);
-
-
//精灵执行动作
-
-
sprite3->runAction(scaleTo);
-
sprite2->runAction(scaleBy);
3. Blink 精灵闪烁
Blink::create(float duration, int blinks)
float duration:动作持续时间,单位为秒
int blinks:闪烁次数
-
auto sprite4 = Sprite::create("sprite.png");
-
sprite4->setPosition(Vec2(visibleSize.width/2 - 300, visibleSize.height/2 + origin.y));
-
sprite4->setScale(2.0f);
-
this->addChild(sprite4);
-
-
//创建Blink动作对象
-
auto blink = Blink::create(3.0f,3);
-
-
sprite4->runAction(blink);
4.BezierTo和BezierBy
创建贝塞尔曲线动作需要一个ccBezierConfig 对象,找个对象有3个属性:
bezier.controlPoint_1 :波谷偏向值
bezier.controlPoint_2 :波峰偏小值
bezier.endPosition :动作终点位置
BezierTo 直接移动到各个坐标点
BezierBy 是在精灵当前位置为基准进行移动的。
-
//创建贝塞尔曲线的配置
-
ccBezierConfig bezier;
-
bezier.controlPoint_1 = Point(100,0);
-
bezier.controlPoint_2 = Point(200,250);
-
bezier.endPosition = Point(300,0);
-
//创建BezierTo动作对象
-
auto bezierTo = BezierTo::create(3.0f,bezier);
-
-
//创建BezierBy动作对象
-
//auto bezierBy = BezierBy::create(3.0f,bezier);
-
-
sprite5->runAction(bezierTo);
-
//sprite5->runAction(bezierBy);
5. repeatForever 和 repeat
repeatForever :创建一个永久性重复动作。
repeat:创建一个指定次数的动作。
-
auto sprite6 = Sprite::create("sprite.png");
-
sprite6->setPosition(Vec2(visibleSize.width/2 - 300, visibleSize.height/2 +100));
-
this->addChild(sprite6);
-
-
auto jumpBy = JumpBy::create(3.0f,Point(50,1),100,1);
-
//以jumpBy为参数,创建个永久性的重复动作
-
auto repeatForever = RepeatForever::create(jumpBy);
-
-
//以jumpBy为参数,创建个指定次数的重复动作
-
auto repeat = Repeat::create(jumpBy,3);
-
-
//sprite6->runAction(repeatForever);
-
sprite6->runAction(repeat);
6. Sequence和Spawn
Spawn:让所有的动作一起播放
Sequence:让动作一个一个播放
-
auto sprite = Sprite::create("sprite.png");
-
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
-
this->addChild(sprite, 0);
-
-
//创建一个移动动作对象
-
auto moveBy = MoveBy::create(2.2f,Point(40,20));
-
-
//创建一个弹跳动作
-
auto jumpBy = JumpBy::create(3.0f,Point(50,1),100,5);
-
-
//创建一个旋转动作
-
auto rotateBy = RotateBy::create(2.5f,220,10);
-
-
auto actions = Sequence::create(moveBy,jumpBy,rotateBy,NULL);
-
-
//auto actions = Spawn::create(moveBy,jumpBy,rotateBy,NULL);
-
-
//循环播放
-
//auto repeat = RepeatForever::create(actions);
-
sprite->runAction(actions);
7. 动作监听
CallFunc 它的动作就是回调一个函数。使用CC_CALLBACK_0创建要回调的函数。
-
bool ActionTest::init()
-
{
-
// 1. super init first
-
if ( !Layer::init() )
-
{
-
return false;
-
}
-
-
Size visibleSize = Director::getInstance()->getVisibleSize();
-
Vec2 origin = Director::getInstance()->getVisibleOrigin();
-
-
auto sprite = Sprite::create("sprite.png");
-
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
-
this->addChild(sprite, 0);
-
-
//创建一个移动动作对象
-
auto moveBy = MoveBy::create(2.2f,Point(40,20));
-
-
//创建一个弹跳动作
-
auto jumpBy = JumpBy::create(3.0f,Point(50,1),100,5);
-
-
//创建一个旋转动作
-
auto rotateBy = RotateBy::create(2.5f,220,10);
-
-
//auto actions = Sequence::create(moveBy,jumpBy,rotateBy,NULL);
-
-
//auto actions = Spawn::create(moveBy,jumpBy,rotateBy,NULL);
-
-
//循环播放
-
//auto repeat = RepeatForever::create(actions);
-
-
-
auto moveto = MoveTo::create(10.0f,Vec2(visibleSize.width, visibleSize.height/2 + origin.y));
-
-
auto callfunc = CallFunc::create(CC_CALLBACK_0(ActionTest::backhome,this));
-
-
auto actions = Sequence::create(moveto,callfunc,NULL);
-
sprite->runAction(actions);
-
return true;
-
}
-
-
void ActionTest::backhome()
-
{
-
auto lable = LabelTTF::create("I am Home!","Arial",35);
-
lable->setPosition(Point(300,300));
-
this->addChild(lable);
-
}
-
阅读(2494) | 评论(0) | 转发(0) |