首页 > 按照书上写cocos2dx中的触摸事件,运行后没有反应

按照书上写cocos2dx中的触摸事件,运行后没有反应

这是我按照书打的一段代码,是点击屏幕后精灵会动,但是我运行后虽然没有error,但是没反应

bool GameScene::init()
{
    this->setTouchEnabled(true);
    Size size = Director::getInstance()->getWinSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();



    //鲸鱼精灵

    whale = Sprite::create("whale.png");
    whale->setPosition(Point(whale->getContentSize().width/2,size.height/2));
    this->addChild(whale,0);


    //添加监听器

    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan,this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);

    return true;
}

bool GameScene::onTouchBegan(Touch * touch,Event * event)
{
    return true;
}

void GameScene::onTouchEnded(Touch * touch,Event *event)
{
    auto location = touch->getLocation();
    whale->stopAllActions();
    whale->runAction(MoveTo::create(1,Point(location.x,location.y)));
    float o = location.x-whale->getPosition().x;
    float a = location.y-whale->getPosition().y;
    float at = (float) CC_RADIANS_TO_DEGREES(atanf(o/a));
    if(a<0)
    {
        if(o<0)
        {
            at = 180 + fabs(at);
        }
        else
        {
            at = 180 - fabs(at);
        }

    }
    whale->runAction(RotateTo::create(1,at));
}

listener只绑定了onTouchBegan,而核心代码却在onTouchEnded,但你又没有绑定onTouchEnded,你让它怎么执行onTouchEnded?

【热门文章】
【热门文章】