By和To的区别
By算的是相对于节点对象的当前位置,To算的是绝对位置。
基本动作
移动
使用MoveTo和MoveBy完成节点对象在一个设置的时间后移动。
旋转
使用RotateTo和RotateBy完成节点对象在一个设置的时间后顺时旋转指定角度。
缩放
使用ScaleBy和ScaleTo完成节点对象的比例缩放。
淡入淡出
FadeIn修改节点对象的透明度属性,从完全透明到完全不透明。FadeOut相反。
色彩混合
auto mySprite = Sprite::create("mysprite.png");
// Tints a node to the specified RGB values
auto tintTo = TintTo::create(2.0f, 120.0f, 232.0f, 254.0f);
mySprite->runAction(tintTo);
// Tints a node BY the delta of the specified RGB values.
auto tintBy = TintBy::create(2.0f, 120.0f, 232.0f, 254.0f);
mySprite->runAction(tintBy);
帧动画
使用Animate对象可以很容易的通过每隔一个短暂时间进行图像替代的方式,实现一个翻页效果。
auto mySprite = Sprite::create("mysprite.png");
// now lets animate the sprite we moved
Vector<SpriteFrame*> animFrames;
animFrames.reserve(12);
animFrames.pushBack(SpriteFrame::create("Blue_Front1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Front2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Front3.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Left1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Left2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Left3.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Back1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Back2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Back3.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Right1.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Right2.png", Rect(0,0,65,81)));
animFrames.pushBack(SpriteFrame::create("Blue_Right3.png", Rect(0,0,65,81)));
// create the animation out of the frames
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f);
Animate* animate = Animate::create(animation);
// run it and repeat it forever
mySprite->runAction(RepeatForever::create(animate));
变速运动
变速动作可以让节点对象具有加速度,产生平滑同时相对复杂的动作,可以用来模仿一些物理运动,这样比实际使用物理引擎的性能消耗低,使用起来也简单。下例是一个精灵从屏幕顶部落下然后不断跳动。
// create a sprite
auto mySprite = Sprite::create("mysprite.png");
// create a MoveBy Action to where we want the sprite to drop from.
auto move = MoveBy::create(2, Vec2(200, dirs->getVisibleSize().height -
newSprite2->getContentSize().height));
auto move_back = move->reverse();
// create a BounceIn Ease Action
auto move_ease_in = EaseBounceIn::create(move->clone() );
// create a delay that is run in between sequence events
auto delay = DelayTime::create(0.25f);
// create the sequence of actions, in the order we want to run them
auto seq1 = Sequence::create(move_ease_in, delay, move_ease_in_back,
delay->clone(), nullptr);
// run the sequence and repeat forever.
mySprite->runAction(RepeatForever::create(seq1));
序列
允许把一个方法添加进去CallFunc对象,然后将CallFunc添加到Sequence,这样在执行序列的时候就能触发方法调用。
auto mySprite = Sprite::create("mysprite.png");
// create a few actions.
auto jump = JumpBy::create(0.5, Vec2(0, 0), 100, 1);
auto rotate = RotateTo::create(2.0f, 10);
// create a few callbacks
auto callbackJump = CallFunc::create([](){
log("Jumped!");
});
auto callbackRotate = CallFunc::create([](){
log("Rotated!");
});
// create a sequence with the actions and callbacks
auto seq = Sequence::create(jump, callbackJump, rotate, callbackRotate, nullptr);
// run it
mySprite->runAction(seq);
Spawn
和Sequence是非常相似的,区别是Spawn同时执行所有的动作,Spawn对象可以添加任意数量的动作和其它Spawn对象。
动作的克隆
MoveBy::create(10, Vec2(400,100));
你的heroSprite就在10s的时间中,从(0,0)移动到了(400,100),heroSprite有了一个新位置(400,100),更重要的是动作对象也有了节点位置相关的内部状态了。现在假如你有一个坐标位置相关的内部状态了。现在假如你有一个坐标位置 是(200,200)的emenySprite就会移动到(800,200)的坐标位置,并不是你期待的结果。因为第二次将这个动作应用的时候,它已经有内部状态了。使用clone()能避免这种情况,克隆获得一个新的动作对象。
// create our Sprites
auto heroSprite = Sprite::create("herosprite.png");
auto enemySprite = Sprite::create("enemysprite.png");
// create an Action
auto moveBy = MoveBy::create(10, Vec2(400,100));
// run it on our hero
heroSprite->runAction(moveBy);
// run it on our enemy
enemySprite->runAction(moveBy); // oops, this will not be unique!
// uses the Actions current internal state as a starting point.
/ create our Sprites
auto heroSprite = Sprite::create("herosprite.png");
auto enemySprite = Sprite::create("enemysprite.png");
// create an Action
auto moveBy = MoveBy::create(10, Vec2(400,100));
// run it on our hero
heroSprite->runAction(moveBy);
// run it on our enemy
enemySprite->runAction(moveBy->clone()); // correct! This will be unique
动作的倒转
倒转(Reverse)的功能也和字面意思一样,调用reverse()可以让一系列动作按相反的方向执行。
// reverse a sequence, spawn or action
mySprite->runAction(mySpawn->reverse());
最后欢迎大家访问我的个人网站:1024s