#include<iostream>
#include<cocos2d.h>
USING_NS_CC;
class ActionCombination :public Layer{
public:
static Scene* createScene();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
CREATE_FUNC(ActionCombination);
};
Sequence动作和Spawn动作的区别是前者按顺序执行,后者同时执行多个动作。
#include "ActionCombination.h"
Scene* ActionCombination::createScene(){
auto scene = Scene::create();
auto layer = ActionCombination::create();
scene->addChild(layer);
return scene;
}
bool ActionCombination::init(){
if (!Layer::init()){
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(ActionCombination::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width / 2,
origin.y + closeItem->getContentSize().height / 2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
//创建一个精灵在左边,用于测试sequence组合动作
auto left = Sprite::create("dance.png");
left->setPosition(Point(visibleSize.width / 2 - 200, visibleSize.height / 2));
this->addChild(left);
//定义第一个RotateTo动作,旋转45度
auto rotateTo1 = RotateTo::create(5, 45);
//定义第一个RotateTo动作,旋转90度
auto rotateTo2 = RotateTo::create(5, 90);
//注意,是按顺序执行2个动作
left->runAction(Sequence::create(rotateTo1, rotateTo2, NULL));
//右边
auto right = Sprite::create("dance.png");
right->setPosition(Point(visibleSize.width / 2 + 200, visibleSize.height / 2));
auto rotateTo3 = RotateTo::create(5, 45);
auto rotateTo4 = RotateTo::create(5, 90);
right->runAction(Spawn::create(rotateTo3, rotateTo4, NULL));
return true;
}
void ActionCombination::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}