您好,登錄后才能下訂單哦!
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" using namespace cocos2d; class HelloWorld : public cocos2d::CCLayer { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld); //函數回調,帶一個參數的回調 void funcN_CallBack(void *sender); //帶兩個參數的回調 void funcND_CallBack(void *sender,void *data); }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include "SecondScene.h" using namespace cocos2d; using namespace CocosDenshion; CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::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 ( !CCLayer::init() ) { return false; } ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); // ask director the window size CCSize size = CCDirector::sharedDirector()->getWinSize(); CCCallFuncN *callFuncN = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::funcN_CallBack)); pLabel->runAction(callFuncN); // position the label on the center of the screen pLabel->setPosition( ccp(size.width / 2, size.height - 20) ); // add the label as a child to this layer this->addChild(pLabel, 1); CCLabelTTF* pLabel2 = CCLabelTTF::create("hi", "Thonburi", 34); //帶兩個參數的回調 CCString *str = CCString::create("帶兩個參數的回調"); str->retain(); CCCallFuncND *callFuncND = CCCallFuncND::create(this,callfuncND_selector(HelloWorld::funcND_CallBack),str); //最后一個參數是void*可以是任意類型 pLabel2->runAction(callFuncND); pLabel2->setPosition(ccp(size.width / 2 + 20,40)); this->addChild(pLabel2,1); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen pSprite->setPosition( ccp(size.width/2, size.height/2) ); // add the sprite as a child to this layer this->addChild(pSprite, 0); return true; } int i=0; void HelloWorld::menuCloseCallback(CCObject* pSender) { CCSize size = CCDirector::sharedDirector()->getWinSize(); CCMenuItem *item = (CCMenuItem *)pSender; /******舒緩動作********************************************************/ // CCActionInterval *action = CCMoveTo::create(2, ccp(20,size.height-20)); // item->runAction(action); /******先慢再快********************************************************/ // CCMoveTo *move = CCMoveTo::create(3, ccp(size.width-20, size.height-20)); // CCActionInterval *ease = CCEaseInOut::create(move, 4); // item->runAction(ease); /*******循環移動**********************************************************/ //float duration = CCRANDOM_0_1()*5+1; float duration = 0.1f; CCMoveTo *move1 = CCMoveTo::create(duration, ccp(20, 20)); CCMoveTo *move2 = CCMoveTo::create(duration,ccp(20, size.height-20)); CCMoveTo *move3 = CCMoveTo::create(duration, ccp(size.width - 20, size.height-20)); CCMoveTo *move4 = CCMoveTo::create(duration, ccp(size.width-20, 20)); CCSequence *sequence = CCSequence::create(move1,move2,move3,move4,NULL); //CCRepeatForever *repeat = CCRepeatForever::create(sequence); item->runAction(sequence); i++; //跳轉 if (i==2) { //replease切換場景 // SecondScene *sense = SecondScene::create(); // CCScene *secScene = CCScene::create(); // secScene->addChild(sense,0); // CCDirector::sharedDirector()->replaceScene(secScene); //push切換場景 CCTransitionFade *secondScene = CCTransitionFade::create(1.0f,SecondScene::scene(),ccGREEN); CCDirector::sharedDirector()->pushScene(secondScene); } } //回調函數 void HelloWorld::funcN_CallBack(void *sender) { CCLabelTTF *label = (CCLabelTTF *)sender; label->setString("帶一個參數的回調"); CCLog("CallFuncN的回調"); } //帶兩個參數的回調 void HelloWorld::funcND_CallBack(void *sender,void *data) { CCString *str = (CCString *)data; CCLabelTTF *label = (CCLabelTTF *)sender; label->setString(str->getCString()); }
#ifndef ___013_9_4___________SecondScene__ #define ___013_9_4___________SecondScene__ #include <iostream> #include "cocos2d.h" using namespace cocos2d; class SecondScene : public cocos2d::CCLayer { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(SecondScene); }; #endif /* defined(___013_9_4___________SecondScene__) */
#include "SecondScene.h" #include "SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; CCScene* SecondScene::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object SecondScene *layer = SecondScene::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 SecondScene::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(SecondScene::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); // ask director the window size CCSize size = CCDirector::sharedDirector()->getWinSize(); // position the label on the center of the screen pLabel->setPosition( ccp(size.width / 2, size.height / 2) ); pLabel->setAnchorPoint(ccp(0.5, 0.5)); pLabel->setTag(10); // add the label as a child to this layer this->addChild(pLabel, 1); } void SecondScene::menuCloseCallback(CCObject* pSender) { // CCDirector::sharedDirector()->end(); // // #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // exit(0); // #endif CCSize size = CCDirector::sharedDirector()->getWinSize(); CCMenuItem *item = (CCMenuItem *)pSender; /******舒緩動作********************************************************/ // CCActionInterval *action = CCMoveTo::create(2, ccp(20,size.height-20)); // item->runAction(action); /******先慢再快********************************************************/ // CCMoveTo *move = CCMoveTo::create(3, ccp(size.width-20, size.height-20)); // CCActionInterval *ease = CCEaseInOut::create(move, 4); // item->runAction(ease); /*******循環移動**********************************************************/ //float duration = CCRANDOM_0_1()*5+1; float duration = 0.1f; CCMoveTo *move1 = CCMoveTo::create(duration, ccp(20, 20)); CCMoveTo *move2 = CCMoveTo::create(duration,ccp(20, size.height-20)); CCMoveTo *move3 = CCMoveTo::create(duration, ccp(size.width - 20, size.height-20)); CCMoveTo *move4 = CCMoveTo::create(duration, ccp(size.width-20, 20)); CCSequence *sequence = CCSequence::create(move1,move2,move3,move4,NULL); CCRepeatForever *repeat = CCRepeatForever::create(sequence); item->runAction(repeat); CCLabelTTF *label = (CCLabelTTF *)this->getChildByTag(10); CCRotateBy *rotateBy = CCRotateBy::create(8, 360); CCMoveTo *move11 = CCMoveTo::create(2, ccp(0, size.height/2)); CCMoveTo *move13 = CCMoveTo::create(2, ccp(size.width, size.height/2)); CCMoveTo *move12 = CCMoveTo::create(2, ccp(size.width/2, size.height/2)); CCSequence *sequence1 = CCSequence::create(move11,move12,move13,move12,NULL); CCSpawn *span = CCSpawn::create(sequence1,rotateBy,NULL); CCRepeatForever *repeat1 = CCRepeatForever::create(span); label->runAction(repeat1); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。