关于事件没有触发的问题,头疼,两天了

跟着皂荚花的实例教程学习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


```

这是StartGame的源码,这里的onKeyReleased就能正常触发执行,MainGame死活不行

#include "StartGame.h"

StartGame::StartGame(void)
{
}

StartGame::~StartGame(void)
{
    //移除监听器
    Director::getInstance()->getEventDispatcher()->removeAllEventListeners();
}

Scene * StartGame::createScene()
{
    auto scene = Scene::create();
    auto layer = StartGame::create();
    scene->addChild(layer);

    return scene;
}

bool StartGame::init()
{
    if (!Layer::init())
        return false;

    auto size = Director::getInstance()->getWinSize();

    //加载缓存文件
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui.plist");
    //使用缓存文件创建精灵
    auto background = Sprite::createWithSpriteFrameName("background.png");
    background->setPosition(Point(size.width / 2, size.height / 2));
    this->addChild(background);

    //设置logo
    auto logo = Sprite::createWithSpriteFrameName("shoot_copyright.png");
    logo->setPosition(Point(size.width / 2, size.height - logo->getContentSize().height));
    //设置透明度是0
    logo->setOpacity(0);
    this->addChild(logo);

    //设置logo的动作
    auto fade = FadeIn::create(2.0f);
    logo->runAction(fade);

    //设置开始游戏场景处的动画
    auto animation = Animation::create();
    for (int i = 0; i < 4; i++)
    {
        auto spriteFrameName = String::createWithFormat("game_loading%d.png", i + 1);
        auto spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName->getCString());
        animation->addSpriteFrame(spriteFrame);
    }
    animation->setDelayPerUnit(0.5f);
    animation->setLoops(-1);
    //指定第一帧执行loading动画
    auto sprite = Sprite::createWithSpriteFrameName("game_loading1.png");
    sprite->setPosition(size.width / 2, size.height / 2);
    this->addChild(sprite);
    //动作
    auto animate = Animate::create(animation);
    sprite->runAction(animate);

    //添加一个游戏开始按钮
    //从xml文件中读取中文显示出来
    auto dictionary = Dictionary::createWithContentsOfFile("font/text.xml");
    auto playTextString = (__String *)(dictionary->objectForKey("play"));
    auto playText = Label::createWithTTF(playTextString->getCString(), "font/DFPShaoNvW5-GB.ttf", 40);
    playText->setColor(Color3B(100, 100, 100));
    auto playMenu = MenuItemLabel::create(playText, CC_CALLBACK_1(StartGame::play, this));
    auto menu = Menu::create(playMenu, NULL);
    menu->setPosition(size.width / 2, size.height*0.3);
    this->addChild(menu);

    //对手机返回键的监听
    auto listener = EventListenerKeyboard::create();
    //和回调函数绑定
    listener->onKeyReleased = CC_CALLBACK_2(StartGame::onKeyReleased, this);
    //添加到事件分发器中
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

    return true;
}
//对android返回键的响应函数
void StartGame::onKeyReleased(EventKeyboard::KeyCode keyCode, Event * pEvent)
{
    CCLOG("StartGame::onKeyReleased().");
    Director::getInstance()->end();
}
//开始游戏按钮的响应函数
void StartGame::play(Ref * ref)
{
    Director::getInstance()->replaceScene(MainGame::createScene());
    log("play game!");
}


```





#ifndef _START_GAME_H_
#define _START_GAME_H_
#include "cocos2d.h"

#include "MainGame.h"

USING_NS_CC;

class StartGame : public Layer
{
public:
    //继承layer需要覆写的几个函数
    static Scene * createScene();
    bool init();
    CREATE_FUNC(StartGame);
public:
    //构造和析构函数,新建项目的时候就自动为我们建立好了,vs还是很方便的
    StartGame(void);
    ~StartGame(void);
public:
    //对游戏开始菜单的响应函数
    void play(Ref * ref);
    //对键盘的响应函数
    void onKeyReleased(EventKeyboard::KeyCode keyCode, Event * pEvent);
};

#endif


```

谢谢,回头用一下试试
新手刚开始学习,很多概念都不了解,所以就是想知道,为什么那三个事件没有得到触发呢?难道是被哪个对象给拦截了吗

。。
http://zhongyi.sina.com/zx/news/296255.shtml
http://zhongyi.sina.com/zx/news/296256.shtml
http://zhongyi.sina.com/zx/news/296259.shtml
http://zhongyi.sina.com/zx/news/296260.shtml
http://zhongyi.sina.com/zx/news/296261.shtml
http://zhongyi.sina.com/zx/news/296264.shtml
http://zhongyi.sina.com/zx/news/296266.shtml
http://zhongyi.sina.com/zx/news/296267.shtml
http://zhongyi.sina.com/zx/news/296270.shtml
http://zhongyi.sina.com/zx/news/296271.shtml
http://zhongyi.sina.com/zx/news/296273.shtml
http://zhongyi.sina.com/zx/news/296275.shtml
http://zhongyi.sina.com/zx/news/296277.shtml
http://zhongyi.sina.com/zx/news/296281.shtml
http://zhongyi.sina.com/zx/news/296282.shtml

你说的方法我都试过了,添加listener之前removeAllEventListener也不行,用addEventListenerWithFixedPriority(touchListener,-300)也不行,直接用_eventDispatcher也不行
唉,崩溃
不过还是要谢谢你,问题虽然没解决,但也学到不少东西

是不是先处理了StartGame的onKeyReleased事件,游戏退出了,导致下层的MainGame的事件没被触发

额 removeAllEventListener 还是不要使用了

用了会出问题,所有event事件都被删除了

用这个吧Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);

然后 把返回键的那段代码去掉

看看touch事件能不能进,查看是否是因为返回键代码导致问题

好了 我项目里面都能使用

如果还是不能的解决的话

私信我一个联系方式吧 我告诉你