按钮的事件监听器
EventListener for buttons
我正在使用 Cocos2d-x 开发 2D 汽车游戏,但遇到了问题。
我想使用 Box2D 来简化碰撞和移动管理,但在此之前我想让我的 4 个按钮正常工作。
关键是我有这样的东西
RaceScene.cpp
bool Race::init() {
//...
auto forwardArrow = MenuItemImage::create("forward_arrow.png", "forward_arrow.png", CC_CALLBACK_1(Race::forward, this));
forwardArrow->setAnchorPoint(Vec2(0,0));
forwardArrow->setPosition(Vec2(origin.x + visibleSize.width - forwardArrow->getContentSize().width*1.2 ,
origin.y + forwardArrow->getContentSize().height));
//...
}
void Race::forward(Ref* pSender)
{
/*MessageBox("YOU PRESSED THE FORWARD BUTTON","Alert");*/
}
但我注意到这不是方法,因为我不能一次按下超过 1 个按钮,也不能根据按下按钮的时间改变速度。
所以我用谷歌搜索并查看了 Cocos 文档,每个人都在谈论 EventListeners,但我找不到如何将它添加到我的问题中。
我不需要处理屏幕触摸,我需要处理每个按钮触摸。
谢谢。
因此,经过几个小时的研究和尝试,以及从 Cocos2d-x 论坛获得一些帮助,我得到了解决方案。
RaceScene.cpp
bool Race::init() {
...
forwardArrow = Sprite::create("forward_arrow.png");
forwardArrow->setAnchorPoint(Vec2(0,0));
forwardArrow->setPosition(Vec2(origin.x + visibleSize.width - forwardArrow->getContentSize().width*1.2 ,
origin.y + forwardArrow->getContentSize().height));
this->addChild(forwardArrow);
...
auto listenerForwardButton = EventListenerTouchOneByOne::create();
listenerForwardButton->setEnabled(true);
listenerForwardButton->setSwallowTouches(true);
listenerForwardButton->onTouchBegan = CC_CALLBACK_2(Race::onTouchesForwardBegan, this);
// trigger when you let up
listenerForwardButton->onTouchEnded = CC_CALLBACK_2(Race::onTouchesForwardEnded, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, forwardArrow);
}
bool Race::onTouchesForwardBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
Vec2 touchPoint = touch->getLocation();
Rect boundingBoxArrow = this->forwardArrow->getBoundingBox();
if (boundingBoxArrow.containsPoint(touchPoint)){
return true;
}
return false;
}
void Race::onTouchesForwardEnded(cocos2d::Touch* touch, cocos2d::Event* event)
{
cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y);
MessageBox("YOU PRESSED THE FORWARD BUTTON","Alert");
auto forward = MoveBy::create(1,Vec2(-5,0));
mainSprite->runAction(forward);
}
RaceScene.h
class Race : public cocos2d::Layer {
public:
virtual bool init();
cocos2d::Sprite* forwardArrow;
...
private:
bool onTouchesForwardBegan(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchesForwardEnded(cocos2d::Touch* touch, cocos2d::Event* event);
...
};
请注意,您需要 listener->onTouchBegan
,CC_CALLBACK_X
重定向到 bool
函数。当这个 returns true
时,onTouchEnd
函数起作用,当这个 returns false
.
时不起作用
希望对大家有所帮助。
我正在使用 Cocos2d-x 开发 2D 汽车游戏,但遇到了问题。 我想使用 Box2D 来简化碰撞和移动管理,但在此之前我想让我的 4 个按钮正常工作。 关键是我有这样的东西
RaceScene.cpp
bool Race::init() {
//...
auto forwardArrow = MenuItemImage::create("forward_arrow.png", "forward_arrow.png", CC_CALLBACK_1(Race::forward, this));
forwardArrow->setAnchorPoint(Vec2(0,0));
forwardArrow->setPosition(Vec2(origin.x + visibleSize.width - forwardArrow->getContentSize().width*1.2 ,
origin.y + forwardArrow->getContentSize().height));
//...
}
void Race::forward(Ref* pSender)
{
/*MessageBox("YOU PRESSED THE FORWARD BUTTON","Alert");*/
}
但我注意到这不是方法,因为我不能一次按下超过 1 个按钮,也不能根据按下按钮的时间改变速度。
所以我用谷歌搜索并查看了 Cocos 文档,每个人都在谈论 EventListeners,但我找不到如何将它添加到我的问题中。
我不需要处理屏幕触摸,我需要处理每个按钮触摸。
谢谢。
因此,经过几个小时的研究和尝试,以及从 Cocos2d-x 论坛获得一些帮助,我得到了解决方案。
RaceScene.cpp
bool Race::init() {
...
forwardArrow = Sprite::create("forward_arrow.png");
forwardArrow->setAnchorPoint(Vec2(0,0));
forwardArrow->setPosition(Vec2(origin.x + visibleSize.width - forwardArrow->getContentSize().width*1.2 ,
origin.y + forwardArrow->getContentSize().height));
this->addChild(forwardArrow);
...
auto listenerForwardButton = EventListenerTouchOneByOne::create();
listenerForwardButton->setEnabled(true);
listenerForwardButton->setSwallowTouches(true);
listenerForwardButton->onTouchBegan = CC_CALLBACK_2(Race::onTouchesForwardBegan, this);
// trigger when you let up
listenerForwardButton->onTouchEnded = CC_CALLBACK_2(Race::onTouchesForwardEnded, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, forwardArrow);
}
bool Race::onTouchesForwardBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
Vec2 touchPoint = touch->getLocation();
Rect boundingBoxArrow = this->forwardArrow->getBoundingBox();
if (boundingBoxArrow.containsPoint(touchPoint)){
return true;
}
return false;
}
void Race::onTouchesForwardEnded(cocos2d::Touch* touch, cocos2d::Event* event)
{
cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y);
MessageBox("YOU PRESSED THE FORWARD BUTTON","Alert");
auto forward = MoveBy::create(1,Vec2(-5,0));
mainSprite->runAction(forward);
}
RaceScene.h
class Race : public cocos2d::Layer {
public:
virtual bool init();
cocos2d::Sprite* forwardArrow;
...
private:
bool onTouchesForwardBegan(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchesForwardEnded(cocos2d::Touch* touch, cocos2d::Event* event);
...
};
请注意,您需要 listener->onTouchBegan
,CC_CALLBACK_X
重定向到 bool
函数。当这个 returns true
时,onTouchEnd
函数起作用,当这个 returns false
.
希望对大家有所帮助。