cocos2dx 2.2.0 Hello world から touchイベントまで
■ HelloWorldScene.h
class HelloWorld : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually CREATE_FUNC(HelloWorld); //touch Event ここを追加 virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); };
最後のtouch Eventの4行を追加
■ HelloWorldScene.cpp
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 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(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // 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("KURUKURU TOUCH", "Arial", 24); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer this->addChild(pLabel, 1); // add "HelloWorld" splash screen" CCSprite* chara00Sprite = CCSprite::create("p_o_kongzhong_0001.png"); CCSprite* chara01Sprite = CCSprite::create("p_o_kongzhong_0004.png"); // position the sprite on the center of the screen chara00Sprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); chara01Sprite->setPosition(ccp(300, 200)); // add the sprite as a child to this layer this->addChild(chara00Sprite, 0, 0); this->addChild(chara01Sprite, 0, 1); //start watching touching ここ追加 this->setTouchMode(kCCTouchesOneByOne); this->setTouchEnabled(true); return true; } void HelloWorld::menuCloseCallback(CCObject* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); #else CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif #endif } bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLOG("ccTouchBegan"); CCPoint location = pTouch->getLocation(); CCLOG("ccTouchBegan: x: %f, y: %f", location.x, location.y); return true; } void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLOG("ccTouchMoved"); } void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLOG("ccTouchCnnceled"); } void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLOG("ccTouchEnded"); }
HelloWorld::init() 内の最後の2行と
最後のほうのccTouch関係のメソッドつくる
■ 起動し画面をタッチしたりスワイプさせるとログにでる。
■ タッチした座標ログに出す
CCPoint location = pTouch->getLocation(); //タッチした座標取得 CCLOG("ccTouchBegan: x: %f, y: %f", location.x, location.y); //ログの書き方はこれ
参考)
http://study-cocos2d-x.info/device/69/
http://www.slideshare.net/itokami1123/lt20130421
http://mettoboshi.com/cocos2d-x/cocos2d-x-animation-2