您好,登錄后才能下訂單哦!
#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 menuRemoveCallback(CCObject* pSender); //啟動觸屏事件 virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); //觸摸注冊事件 virtual void registerWithTouchDispatcher(); //單點觸摸事件 virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent); //移動事件 virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent); }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" #include "SimpleAudioEngine.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; } bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } //設置當前允許觸摸 this->setTouchEnabled(true); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width / 2- 30, 20) ); CCMenuItemImage *pCloseItem1 = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuRemoveCallback) ); pCloseItem1->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width / 2 + 30, 20) ); CCMenu* pMenu = CCMenu::create(pCloseItem1,pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); CCLabelTTF* pLabel = CCLabelTTF::create("江蘇理工", "Thonburi", 34); CCSize size = CCDirector::sharedDirector()->getWinSize(); //一開始設置為綠色 pLabel->setColor(ccGREEN); pLabel->setPosition( ccp(size.width / 2, size.height / 2) ); this->addChild(pLabel,1); //讓節點閃爍的方法 CCAction *action = CCBlink::create(5, 20); pLabel->runAction(action); //變色的方法 CCAction *action1 = CCTintTo::create(5, 255, 0, 0); pLabel->runAction(action1); //左上角顯示姓名 CCLabelTTF* pLabel1 = CCLabelTTF::create("丁小未", "Thonburi", 34); CCSize size1 = CCDirector::sharedDirector()->getWinSize(); pLabel1->setAnchorPoint(ccp(0, 1)); pLabel1->setPosition( ccp(0, size1.height) ); this->addChild(pLabel1,1); //右上角顯示性別 CCLabelTTF* pLabel2 = CCLabelTTF::create("男", "Thonburi", 34); CCSize size2 = CCDirector::sharedDirector()->getWinSize(); pLabel2->setAnchorPoint(ccp(1, 1)); pLabel2->setPosition( ccp(size2.width, size2.height) ); this->addChild(pLabel2,1); //右下角顯示年齡 CCLabelTTF* pLabel3 = CCLabelTTF::create("23", "Thonburi", 34); CCSize size3 = CCDirector::sharedDirector()->getWinSize(); pLabel3->setAnchorPoint(ccp(1, 0)); pLabel3->setPosition( ccp(size3.width, 0) ); this->addChild(pLabel3,1); return true; } void HelloWorld::menuCloseCallback(CCObject* pSender) { //結束關閉事件 // CCDirector::sharedDirector()->end(); // //#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // exit(0); //#endif CCSize size = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF *pLabel = CCLabelTTF::create("我是添加的", "Thonburi", 24); pLabel->setPosition(ccp(size.width/2+30,size.height/2+30)); pLabel->setTag(10); this->addChild(pLabel,1); } void HelloWorld::menuRemoveCallback(CCObject *pSender) { CCNode *pLabel = this->getChildByTag(10); this->removeChild(pLabel); } //多點觸摸方法 void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) { //添加子視圖 //隨機數是CCRANDOM_0_1,是產生0-1之間的隨機數 // CCSize size = CCDirector::sharedDirector()->getWinSize(); // CCLabelTTF *pLabel = CCLabelTTF::create("觸屏添加", "Thonburi", 24); // pLabel->setPosition(ccp(100, 100)); // pLabel->setTag(10); // this->addChild(pLabel,1); CCLog("多點觸摸Began"); } bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLog("單點觸摸"); return true;//如果這個不返回true的話,則move方法沒用 } void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLog("單點moved"); } //觸摸注冊事件 //如果沒有這個,默認的是多點觸摸,Targeted是單點,Standed是多點觸摸 void HelloWorld::registerWithTouchDispatcher() { CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。