cocos2d-x: cocos2d-x 在当前场景中点击图片按钮进入下一场景

1.按钮可以在触摸里面判断,void ccTouchesEnded(CCSet* touches, CCEvent* event);
  首先获得点击屏幕的点,
  CCTouch* touch = (CCTouch*)(touches->anyObject());
  CCPoint touchPoint = touch->getLocationInView();
  touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
  然后在获得图片按钮的矩形(sprite),
  CCRect rect = sprite->boundingBox();
  在判断你点击的这个点touchPoint 是否在图片按钮矩形里面rect ;
   if(rect.containsPoint(touchPoint))
    {//为true就进来,说明点中在图片按钮矩形里面
    CCScene* scen = /*下一场景的实例类*/HelloWorld::scene();
    CCDirector::sharedDirector()->replaceScene(scen);
    }

   最后提醒,在使用触摸实现此功能的时候,一定要先开启触摸响应。
   this->setTouchEnabled(true); //开启触摸响应
  这是2.1版本的.
2.当然也可以把图片按钮做成菜单CCMenu,然后在按钮上添加回调函数,同样可以实现进入下一场景。
  点击的时候切换场景执行:CCDirector::sharedDirector()->replaceScene(scene);
  这个scene是你自己创建好的下一个场景。
 

你可能感兴趣的:(cocos2d-x: cocos2d-x 在当前场景中点击图片按钮进入下一场景)