菜鸟学习Cocos2d-x 3.x——浅谈动作Action
http://www.jellythink.com/archives/749
动作实例
简单的一个动作@H_301_8@
BezierTo
// 创建精灵对象
Sprite* sp = Sprite::create("sprite.png");
sp->setPosition(Point(50,visibleSize.height / 2));
this->addChild(sp);
// 创建贝塞尔曲线
ccBezierConfig bezier;
bezier.controlPoint_1 = Point(100,0); // 波谷偏向值
bezier.controlPoint_2 = Point(200,250); // 波峰偏向值
bezier.endPosition = Point(300,50); // 动作终点
// 创建动作
BezierTo* bezierTo = BezierTo::create(4.0f,bezier);
// 执行动作
sp->runAction(bezierTo);
MoveTo
// 创建精灵对象
Sprite* sp = Sprite::create("sprite.png");
sp->setPosition(Point(50,visibleSize.height / 2));
this->addChild(sp);
// 创建MoveTo对象
// MoveTo (250 150为移动到的位置),0.9f为动作持续时间(单位为秒)
MoveTo* moveTo = MoveTo::create(0.9f,Point(250,150));
// 精灵执行动作
sp->runAction(moveTo);
卡牌翻转效果的实现动作
/* 卡牌翻转效果的实现 */
Sprite* sp11 = Sprite::create("pos.png");
sp11->setPosition(Point(600,visibleSize.height / 2));
this->addChild(sp11);
Sprite* sp12 = Sprite::create("nes.png");
sp12->setPosition(Point(600,visibleSize.height / 2));
this->addChild(sp12);
// 创建MoveBy对象
// 动作持续时间,X,Y方向的拉伸值
ScaleTo* scaleTo1 = ScaleTo::create(3.8f,1.0f,1.0f);
ScaleTo* scaleTo2 = ScaleTo::create(3.8f,0.0f,1.0f);
// 执行动作
sp11->runAction(scaleTo1);
sp12->runAction(scaleTo2);
闪烁动作
// 闪烁动作
Sprite* spBlink = Sprite::create("bird.png");
spBlink->setPosition(Point(800,visibleSize.height / 2));
this->addChild(spBlink);
// 创建闪烁动作 动作持续时间, 闪烁次数
Blink *blink = Blink::create(5.0f,10);
//
spBlink->runAction(blink);
持续动作@H_301_8@
//持续执行动作RepeatForever
// 创建精灵对象
Sprite* sp2 = Sprite::create("bird.png");
sp2->setPosition(Point(300,visibleSize.height / 2));
this->addChild(sp2);
// JumpBy动作
/* 参数 duration 持续时间,以秒为单位 position 跳跃的距离 height 跳跃的高度 jumps 跳跃次数 */
JumpBy *jumpBy = JumpBy::create(3.0f,Point(100,1),100,1);
RepeatForever* repeatForeverAction = RepeatForever::create(jumpBy);
// 执行动作
sp2->runAction(repeatForeverAction);
一连串动作集合@H_301_8@
/* 执行一连串的动作 */
// 创建精灵对象
Sprite* sp3 = Sprite::create("sprite.png");
sp3->setPosition(Point(visibleSize.width/2,visibleSize.height / 2 ));
this->addChild(sp3);
//动作
MoveBy *movBy = MoveBy::create(2.2f,Point(30,20));
JumpBy* jmpBy = JumpBy::create(3.0f,Point(50,5);
RotateBy* rotateBy = RotateBy::create(2.5f,220,10);
// 组建一连串的动作
Action* actions = Spawn::create(movBy,jmpBy,rotateBy,NULL);
// 执行动作
sp3->runAction(actions);
动作函数回调@H_301_8@
Size visibleSize = Director::getInstance()->getVisibleSize();
// 创建精灵对象
Sprite* sp = Sprite::create("sprite.png");
sp->setPosition(Point(visibleSize.width/2,visibleSize.height / 2));
this->addChild(sp);
//动作
MoveTo *moveToHome = MoveTo::create(10.0f,Point(visibleSize.width - 100,visibleSize.height / 2));
auto callBackFunc = [&](){ // 回调一个函数
backHome();
};
//创建回调函数
CallFunc* callFunc = CallFunc::create(callBackFunc);
// 组建一连串的动作,包括动作回调函数
Action* actions = Spawn::create(moveToHome,callFunc,NULL);
// 执行动作
sp->runAction(actions);
void HelloWorld::backHome()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Label* label = Label::create("I am home","Arial",35);
label->setPosition(visibleSize.width / 2,visibleSize.height / 2);
this->addChild(label);
}
#@H_301_8@
//持续执行动作RepeatForever
// 创建精灵对象
Sprite* sp2 = Sprite::create("bird.png");
sp2->setPosition(Point(300,visibleSize.height / 2));
this->addChild(sp2);
// JumpBy动作
/* 参数 duration 持续时间,以秒为单位 position 跳跃的距离 height 跳跃的高度 jumps 跳跃次数 */
JumpBy *jumpBy = JumpBy::create(3.0f,Point(100,1),100,1);
RepeatForever* repeatForeverAction = RepeatForever::create(jumpBy);
// 执行动作
sp2->runAction(repeatForeverAction);
一连串动作集合@H_301_8@
/* 执行一连串的动作 */
// 创建精灵对象
Sprite* sp3 = Sprite::create("sprite.png");
sp3->setPosition(Point(visibleSize.width/2,visibleSize.height / 2 ));
this->addChild(sp3);
//动作
MoveBy *movBy = MoveBy::create(2.2f,Point(30,20));
JumpBy* jmpBy = JumpBy::create(3.0f,Point(50,5);
RotateBy* rotateBy = RotateBy::create(2.5f,220,10);
// 组建一连串的动作
Action* actions = Spawn::create(movBy,jmpBy,rotateBy,NULL);
// 执行动作
sp3->runAction(actions);
动作函数回调@H_301_8@
Size visibleSize = Director::getInstance()->getVisibleSize();
// 创建精灵对象
Sprite* sp = Sprite::create("sprite.png");
sp->setPosition(Point(visibleSize.width/2,visibleSize.height / 2));
this->addChild(sp);
//动作
MoveTo *moveToHome = MoveTo::create(10.0f,Point(visibleSize.width - 100,visibleSize.height / 2));
auto callBackFunc = [&](){ // 回调一个函数
backHome();
};
//创建回调函数
CallFunc* callFunc = CallFunc::create(callBackFunc);
// 组建一连串的动作,包括动作回调函数
Action* actions = Spawn::create(moveToHome,callFunc,NULL);
// 执行动作
sp->runAction(actions);
void HelloWorld::backHome()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Label* label = Label::create("I am home","Arial",35);
label->setPosition(visibleSize.width / 2,visibleSize.height / 2);
this->addChild(label);
}
#@H_301_8@
Size visibleSize = Director::getInstance()->getVisibleSize();
// 创建精灵对象
Sprite* sp = Sprite::create("sprite.png");
sp->setPosition(Point(visibleSize.width/2,visibleSize.height / 2));
this->addChild(sp);
//动作
MoveTo *moveToHome = MoveTo::create(10.0f,Point(visibleSize.width - 100,visibleSize.height / 2));
auto callBackFunc = [&](){ // 回调一个函数
backHome();
};
//创建回调函数
CallFunc* callFunc = CallFunc::create(callBackFunc);
// 组建一连串的动作,包括动作回调函数
Action* actions = Spawn::create(moveToHome,callFunc,NULL);
// 执行动作
sp->runAction(actions);
void HelloWorld::backHome()
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Label* label = Label::create("I am home","Arial",35);
label->setPosition(visibleSize.width / 2,visibleSize.height / 2);
this->addChild(label);
}