hero的动作效果
.cpp文件
#include "HelloWorldScene.h" USING_NS_CC; USING_NS_CC_EXT; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object Scene *scene = Scene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init()) { return false; } Size screenSize = Director::getInstance()->getWinSize(); Sprite *bg = Sprite::create("background.png"); bg->setPosition(Point(screenSize.width/2, screenSize.height/2)); addChild(bg); hero = Sprite::create("hero.png"); hero->setPosition(Point(screenSize.width/2, screenSize.height/2)); addChild(hero); MenuItemFont *itemFlipX = MenuItemFont::create("FlipX",CC_CALLBACK_1(HelloWorld::testFlipX, this)); itemFlipX->setPosition(Point(100, 50)); MenuItemFont *itemFlipY = MenuItemFont::create("FlipY",CC_CALLBACK_1(HelloWorld::testFlipY, this)); itemFlipY->setPosition(Point(200, 50)); MenuItemFont *itemHide = MenuItemFont::create("Hide",CC_CALLBACK_1(HelloWorld::testHide, this)); itemHide->setPosition(Point(300, 50)); MenuItemFont *itemShow = MenuItemFont::create("Show",CC_CALLBACK_1(HelloWorld::testShow, this)); itemShow->setPosition(Point(400, 50)); Menu *actionMenu = Menu::create(itemFlipX,itemFlipY,itemHide,itemShow,NULL); actionMenu->setPosition(Point(0,0)); addChild(actionMenu); return true; } HelloWorld::~HelloWorld(){ CC_SAFE_RELEASE(hero); } void HelloWorld::testFlipX(Object* sender) { if (hero->isFlipX()) { hero->runAction(FlipX::create(false)); }else{ hero->runAction(FlipX::create(true)); } } void HelloWorld::testFlipY(Object* sender) { if (hero->isFlipY()) { hero->runAction(FlipY::create(false)); }else{ hero->runAction(FlipY::create(true)); } } void HelloWorld::testShow(Object* sender) { hero->runAction(Show::create()); } void HelloWorld::testHide(Object* sender) { hero->runAction(Hide::create()); }.h文件
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class HelloWorld :public Layer { Sprite *hero ; public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); ~HelloWorld(); // there's no 'id' in cpp, so we recommend returning the class instance pointer //static Scene* scene(); static cocos2d::Scene* createScene(); CREATE_FUNC(HelloWorld); EditBox *_editName; EditBox* _editPassword; void testFlipX(Object* sender); void testFlipY(Object* sender); void testHide(Object* sender); void testShow(Object* sender); }; #endif // __HELLOWORLD_SCENE_H__效果图: