本文整理汇总了C++中EventDispatcher::addEventListenerWithFixedPriority方法的典型用法代码示例。如果您正苦于以下问题:C++ EventDispatcher::addEventListenerWithFixedPriority方法的具体用法?C++ EventDispatcher::addEventListenerWithFixedPriority怎么用?C++ EventDispatcher::addEventListenerWithFixedPriority使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EventDispatcher
的用法示例。
在下文中一共展示了EventDispatcher::addEventListenerWithFixedPriority方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: registerEventListener
void Hero::registerEventListener()
{
Director *director = Director::getInstance();
EventDispatcher *eventDispatcher = director->getEventDispatcher();
EventListenerCustom *customEventListener = EventListenerCustom::create("slide", CC_CALLBACK_1(Hero::onSlide, this));
eventDispatcher->addEventListenerWithFixedPriority(customEventListener, 1);
customEventListener = EventListenerCustom::create("jump", CC_CALLBACK_1(Hero::onJump, this));
eventDispatcher->addEventListenerWithFixedPriority(customEventListener, 1);
customEventListener = EventListenerCustom::create("right", CC_CALLBACK_1(Hero::onRight, this));
eventDispatcher->addEventListenerWithFixedPriority(customEventListener, 1);
customEventListener = EventListenerCustom::create("left", CC_CALLBACK_1(Hero::onLeft, this));
eventDispatcher->addEventListenerWithFixedPriority(customEventListener, 1);
}
示例2:
void Widget::FocusNavigationController::addKeyboardEventListener()
{
if (nullptr == _keyboardListener)
{
_keyboardListener = EventListenerKeyboard::create();
_keyboardListener->onKeyReleased = CC_CALLBACK_2(Widget::FocusNavigationController::onKeypadKeyPressed, this);
EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->addEventListenerWithFixedPriority(_keyboardListener, _keyboardEventPriority);
}
}
示例3: onEnter
//.........这里部分代码省略.........
//添加敌机2.
auto enemyFighter2 = Enemy::createWithEnemyTypes(EnemyTypeEnemy2);
enemyFighter2->setVelocity(Vec2(0, -100));
this->addChild(enemyFighter2, 10 , GameSceneNodeBatchTagEnemy);
//玩家的飞机.
this->fighter = Fighter::createWithSpriteFrameName("gameplay.fighter.png");
this->fighter->setHitPoints(5);
this->fighter->setPosition(Vec2(visibleSize.width / 2, 70));
this->addChild(this->fighter, 10, GameSceneNodeTagFighter);
//注册 触摸飞机事件监听器
touchFighterlistener = EventListenerTouchOneByOne::create();
touchFighterlistener->setSwallowTouches(true);
//移动飞机.
touchFighterlistener->onTouchBegan = [](Touch* touch, Event* event) {
return true;
};
touchFighterlistener->onTouchMoved = [](Touch* touch, Event* event) {
auto target = event->getCurrentTarget();
target->setPosition(target->getPosition() + touch->getDelta());
};
// 添加 触摸飞机事件监听器
EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->addEventListenerWithSceneGraphPriority(touchFighterlistener, this->fighter);
//注册 碰撞事件监听器
contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = [this](PhysicsContact& contact)
{
auto spriteA = contact.getShapeA()->getBody()->getNode();
auto spriteB = contact.getShapeB()->getBody()->getNode();
Node* enemy1 = nullptr;
////////////////////////////检测 飞机与敌人的碰撞 start//////////////////////////////////
log("A = %d ------------ B = %d", spriteA->getTag(), spriteB->getTag());
if (spriteA->getTag() == GameSceneNodeTagFighter && spriteB->getTag() == GameSceneNodeBatchTagEnemy)
{
enemy1 = spriteB;
}
if (spriteA->getTag() == GameSceneNodeBatchTagEnemy && spriteB->getTag() == GameSceneNodeTagFighter)
{
enemy1 = spriteA;
}
if (enemy1 != nullptr) {//发生碰撞
this->handleFighterCollidingWithEnemy((Enemy*)enemy1);
return false;
}
/////////////////////////检测 飞机与敌人的碰撞 end/////////////////////////////////////
////////////////////////////检测 炮弹与敌人的碰撞 start////////////////////////////////
Node* enemy2 = nullptr;
if (spriteA->getTag() == GameSceneNodeBatchTagBullet &&
spriteB->getTag() == GameSceneNodeBatchTagEnemy)
{
//不可见的炮弹不发生碰撞
if (!spriteA->isVisible())
return false;
//使得炮弹消失
spriteA->setVisible(false);
enemy2 = spriteB;
}
if (spriteA->getTag() == GameSceneNodeBatchTagEnemy
&& spriteB->getTag() == GameSceneNodeBatchTagBullet)
{
//不可见的炮弹不发生碰撞
if (!spriteB->isVisible())
return false;
//使得炮弹消失
spriteB->setVisible(false);
enemy2 = spriteA;
}
if (enemy2 != nullptr) {//发生碰撞
this->handleBulletCollidingWithEnemy((Enemy*)enemy2);
return false;
}
/////////////////////////检测 炮弹与敌人的碰撞 end/////////////////////////////////////
return false;
};
eventDispatcher->addEventListenerWithFixedPriority(contactListener,1);
//每0.2s 调用shootBullet函数发射1发炮弹.
this->schedule(schedule_selector(GamePlayLayer::shootBullet), 0.2f);
this->score = 0;
this->scorePlaceholder = 0;
//在状态栏中设置玩家的生命值
this->updateStatusBarFighter();
//在状态栏中显示得分
this->updateStatusBarScore();
}
示例4: setViews
//.........这里部分代码省略.........
}
if (enemy1 != nullptr) {//发生碰撞
if(hero&&hero->getIsAlive())
{
this->EnemyAttackPlane();
}
//this->handleFighterCollidingWithEnemy((Enemy*)enemy1);
return false;
}
/////////////////////////检测 飞机与敌人的碰撞 end/////////////////////////////////////
////////////////////////////检测 炮弹与敌人的碰撞 start////////////////////////////////
Node* enemy2 = nullptr;
Node* bullet = nullptr;
if (spriteA&&spriteB&&spriteA->getTag() == GameSceneNodeBatchTagBullet &&
spriteB->getTag() == GameSceneNodeBatchTagEnemy)
{
//不可见的炮弹不发生碰撞
//if (!spriteA->isVisible())
// return false;
//使得炮弹消失
//spriteA->setVisible(false);
bullet = spriteA/*->removeFromParentAndCleanup(true)*/;
enemy2 = spriteB;
}
if (spriteA&&spriteB&&spriteA->getTag() == GameSceneNodeBatchTagEnemy
&& spriteB->getTag() == GameSceneNodeBatchTagBullet)
{
//不可见的炮弹不发生碰撞
//if (!spriteB->isVisible())
// return false;
//使得炮弹消失
//spriteB->setVisible(false);
bullet = spriteB/*->removeFromParent()*/;
enemy2 = spriteA;
}
if (enemy2 != nullptr) {//发生碰撞
bullet->removeFromParent();
this->bulletAttackEnemy((EnemyBase*)enemy2);
//this->handleBulletCollidingWithEnemy((Enemy*)enemy2);
return false;
}
/////////////////////////检测 炮弹与敌人的碰撞 end/////////////////////////////////////
////////////////////////////检测 飞机与UFO2的碰撞 start//////////////////////////////////
Dlog::showLog("A = %d ------------ B = %d", spriteA->getTag(), spriteB->getTag());
Node* enemy3 = nullptr;
if (spriteA&&spriteB&&spriteA->getTag() == GameSceneNodeTagFighter && spriteB->getTag() == GameSceneNodeBatchTagBomb2)
{
enemy3 = spriteB;
}
if (spriteA&&spriteB&&spriteA->getTag() == GameSceneNodeBatchTagBomb2 && spriteB->getTag() == GameSceneNodeTagFighter)
{
enemy3 = spriteA;
}
if (enemy3 != nullptr) {//发生碰撞
SimpleAudioEngine::getInstance()->playEffect("sound/get_bomb.mp3");
enemy3->setVisible(false);
enemy3->removeFromParent();
this->BombAttackPlane();
//this->handleFighterCollidingWithEnemy((Enemy*)enemy1);
return false;
}
/////////////////////////检测 飞机与UFO2的碰撞 end/////////////////////////////////////
////////////////////////////检测 飞机与UFO1的碰撞 start//////////////////////////////////
Dlog::showLog("A = %d ------------ B = %d", spriteA->getTag(), spriteB->getTag());
Node* enemy4 = nullptr;
if (spriteA&&spriteB&&spriteA->getTag() == GameSceneNodeTagFighter && spriteB->getTag() == GameSceneNodeBatchTagBomb1)
{
enemy4 = spriteB;
}
if (spriteA&&spriteB&&spriteA->getTag() == GameSceneNodeBatchTagBomb1 && spriteB->getTag() == GameSceneNodeTagFighter)
{
enemy4 = spriteA;
}
if (enemy4 != nullptr) {//发生碰撞
SimpleAudioEngine::getInstance()->playEffect("sound/get_double_laser.mp3");
enemy4->setVisible(false);
enemy4->removeFromParent();
this->DoubleBulletsIsOn = true;
this->scheduleOnce(schedule_selector(InGameLayer::updateDoubleBulletsIsOn),11.0f);
//this->handleFighterCollidingWithEnemy((Enemy*)enemy1);
return false;
}
/////////////////////////检测 飞机与UFO1的碰撞 end/////////////////////////////////////
return false;
};
EventDispatcher* eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->addEventListenerWithFixedPriority(contactListener,1);
this->schedule(SEL_SCHEDULE(&InGameLayer::createEnemys),CCRANDOM_0_1()*0.25+0.36f);
this->schedule(SEL_SCHEDULE(&InGameLayer::moveAllEnemys),0.08f);
this->schedule(SEL_SCHEDULE(&InGameLayer::createbullets),0.2f);
//this->schedule(SEL_SCHEDULE(&InGameLayer::moveAllbullets),0.016f);
this->scheduleUpdate();
}
示例5: init
// on "init" you need to initialize your instance
bool MapLayer::init()
{
//////////////////////////////
// 1. super init first
if ( !LayerColor::initWithColor( Color4B( 255, 0, 0, 255 )))
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
auto img = Sprite::create("image/map.png");
// img->setPosition( getPosition());
// img->setContentSize( Size( MAP_WIDTH, MAP_HEIGHT ));
img->setPosition( MAP_WIDTH/2, MAP_HEIGHT/2);
this->addChild( img, 0 );
setContentSize( Size( MAP_WIDTH, MAP_HEIGHT ) );
setPosition( ( visibleSize.width - MAP_WIDTH ) / 2, ( visibleSize.height - MAP_HEIGHT ) / 2 );
// this->setContentSize( Size( 100, 100 ));
////////////////////////////////
this->setTouchEnabled( true );
auto m_touchListener = EventListenerTouchOneByOne::create();
// m_touchListener->setSwallowTouches(true);
m_touchListener->onTouchBegan = CC_CALLBACK_2(MapLayer::onTouchBegan, this);
m_touchListener->onTouchMoved = CC_CALLBACK_2(MapLayer::onTouchMoved, this);
m_touchListener->onTouchEnded = CC_CALLBACK_2(MapLayer::onTouchEnded, this);
EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->addEventListenerWithFixedPriority( m_touchListener, 1 );
////////////////////////////////
auto keyboardListener = EventListenerKeyboard::create();
// EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
keyboardListener->onKeyPressed = CC_CALLBACK_2( MapLayer::onKeyPressed, this );
keyboardListener->onKeyReleased = CC_CALLBACK_2( MapLayer::onKeyReleased, this );
dispatcher->addEventListenerWithFixedPriority( keyboardListener, 1 );
// setKeyboardEnabled( true );
setKeyboardEnabled( true );
/////////////////////////////////////////////////////////////////////////////////////////////////////////
auto label = LabelTTF::create("test test.", "Arial", 30);
// position the label on the center of the screen
label->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
label->setColor( Color3B( 255, 0, 0 ));
// add the label as a child to this layer
this->addChild(label, 1);
return true;
}