按照书上写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));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
listener只绑定了onTouchBegan,而核心代码却在onTouchEnded,但你又没有绑定onTouchEnded,你让它怎么执行onTouchEnded?