跟着皂荚花的实例教程学习3.0,http://www.zaojiahua.com/adding-a-player.html
到第三章就出问题了,三个事件都得不到触发执行,无论在windows下还是在android下都一样,高手帮忙看看怎么回事
#include "MainGame.h" MainGame::MainGame(void) { } MainGame::~MainGame(void) { //移除事件监听器 Director::getInstance()->getEventDispatcher()->removeEventListener(m_listener); } Scene * MainGame::createScene() { auto scene = Scene::create(); auto layer = MainGame::create(); scene->addChild(layer); return scene; } bool MainGame::init() { if (!Layer::init()) return false; m_size = Director::getInstance()->getWinSize(); //添加地图层 auto background = Background::create(); this->addChild(background); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("shoot.plist"); //添加玩家飞机 m_player = Player::create(); m_player->setPosition(Point(m_size.width / 2, m_size.height*0.2)); this->addChild(m_player); //添加android返回键的监听器 auto listener = EventListenerKeyboard::create(); listener->onKeyReleased = CC_CALLBACK_2(MainGame::onKeyReleased, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); //单点触摸的监听器 auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(MainGame::onTouchBegan, this); touchListener->onTouchMoved = CC_CALLBACK_2(MainGame::onTouchMoved, this); //设置吞噬触摸 touchListener->setSwallowTouches(true); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this); return true; } //android返回键的监听 void MainGame::onKeyReleased(EventKeyboard::KeyCode keyCode, Event * pEvent) { //切换到开始游戏场景 CCLOG("MainGame::onKeyReleased()."); Director::getInstance()->replaceScene(StartGame::createScene()); } //单点触摸的监听函数 bool MainGame::onTouchBegan(Touch * touch, Event * pEvent) { CCLOG("MainGame::onTouchBegan()."); //判断手指点击的区域是否在飞机上 auto point = touch->getLocation(); auto rect = m_player->getBoundingBox(); if (rect.containsPoint(point)) return true; return false; } void MainGame::onTouchMoved(Touch * touch, Event * pEvent) { CCLOG("MainGame::onTouchMoved()."); //跟随手指移动飞机的位置 auto addPoint = touch->getLocation() - touch->getPreviousLocation(); m_player->setPosition(m_player->getPosition() + addPoint); //判断飞机移动的范围,不要超出了边界 auto size = m_player->getContentSize(); //如果x方向的坐标小于了它自己宽度的一般,或者是大于了屏幕的宽度减去自己宽度的一半的大小就越界了 if (m_player->getPositionX() < size.width / 2) { m_player->setPositionX(size.width / 2); } else if (m_player->getPositionX() > m_size.width - size.width / 2) { m_player->setPositionX(m_size.width - size.width / 2); } //y方向根据x方向的判断方法进行类推 if (m_player->getPositionY() < size.height / 2) { m_player->setPositionY(size.height / 2); } else if (m_player->getPositionY() > m_size.height - size.height / 2) { m_player->setPositionY(m_size.height - size.height / 2); } } ```#ifndef _MAIN_GAME_H_ #define _MAIN_GAME_H_ #include "cocos2d.h" //包含地图层 #include "Background.h" //开始游戏场景 #include "StartGame.h" #include "Player.h" USING_NS_CC; //游戏主场景 class MainGame : public Layer { public: MainGame(void); ~MainGame(void); public: static Scene * createScene(); bool init(); CREATE_FUNC(MainGame); public: //对返回键的监听函数 void onKeyReleased(EventKeyboard::KeyCode keyCode, Event * pEvent); bool onTouchBegan(Touch * touch, Event * pEvent); void onTouchMoved(Touch * touch, Event * pEvent); private: Size m_size; EventListenerKeyboard * m_listener; EventListenerTouchOneByOne * m_touchListener; Player * m_player; }; #endif ```