void SpriteManager::AddEventListenerSprite(Sprite* sp) { //添加到监听器 Director *director = Director::getInstance(); (director->getEventDispatcher())->addEventListenerWithSceneGraphPriority(listenerTouch->clone(), sp); listenerTouch->retain(); }
void GameLayer::registerEventHandlers() { Director* director = Director::getInstance(); EventDispatcher* eventDispatcher = director->getEventDispatcher(); EventListenerTouchOneByOne* eventListener = EventListenerTouchOneByOne::create(); eventListener->setSwallowTouches(true); eventListener->onTouchEnded = [=](Touch* touch, Event* event){ if (!getAcceptInput()) { return; } Vec2 position = m_tileLayer->convertTouchToNodeSpace(touch); for (auto iterator = m_tileLayer->getChildren().begin(); iterator != m_tileLayer->getChildren().end(); iterator++) { auto child = *iterator; GameTileView* gameTileView = dynamic_cast<GameTileView*>(child); if(gameTileView->getBoundingBox().containsPoint(position)) { if (gameTileView->getGameTile()->getCanMove()) { doSelectGameTileView(gameTileView); } break; } } }; eventListener->onTouchBegan = [](Touch* touch, Event* event){ return true; }; eventListener->onTouchMoved = [](Touch* touch, Event* event){}; eventListener->onTouchCancelled = [](Touch* touch, Event* event){}; eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, m_tileLayer); }
Node::Node(void) : _rotationX(0.0f) , _rotationY(0.0f) , _scaleX(1.0f) , _scaleY(1.0f) , _vertexZ(0.0f) , _position(Point::ZERO) , _skewX(0.0f) , _skewY(0.0f) , _anchorPointInPoints(Point::ZERO) , _anchorPoint(Point::ZERO) , _contentSize(Size::ZERO) , _additionalTransformDirty(false) , _transformDirty(true) , _inverseDirty(true) // children (lazy allocs) // lazy alloc , _ZOrder(0) , _parent(nullptr) // "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true , _tag(Node::INVALID_TAG) // userData is always inited as nil , _userData(nullptr) , _userObject(nullptr) , _shaderProgram(nullptr) , _orderOfArrival(0) , _running(false) , _visible(true) , _ignoreAnchorPointForPosition(false) , _reorderChildDirty(false) , _isTransitionFinished(false) , _updateScriptHandler(0) , _componentContainer(nullptr) #if CC_USE_PHYSICS , _physicsBody(nullptr) #endif , _displayedOpacity(255) , _realOpacity(255) , _displayedColor(Color3B::WHITE) , _realColor(Color3B::WHITE) , _cascadeColorEnabled(false) , _cascadeOpacityEnabled(false) { // set default scheduler and actionManager Director *director = Director::getInstance(); _actionManager = director->getActionManager(); _actionManager->retain(); _scheduler = director->getScheduler(); _scheduler->retain(); _eventDispatcher = director->getEventDispatcher(); _eventDispatcher->retain(); ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine(); _scriptType = engine != nullptr ? engine->getScriptType() : kScriptTypeNone; kmMat4Identity(&_transform); kmMat4Identity(&_inverse); kmMat4Identity(&_additionalTransform); }
void GameLayer::registerHandler(Node* node, std::function<void(Touch*, Event*)> onTouchEnded) { Director* director = Director::getInstance(); EventDispatcher* eventDispatcher = director->getEventDispatcher(); EventListenerTouchOneByOne* eventListener = EventListenerTouchOneByOne::create(); eventListener->setSwallowTouches(true); eventListener->onTouchEnded = onTouchEnded; eventListener->onTouchBegan = [](Touch* touch, Event* event){ return true; }; eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, node); }
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); }
bool IdleLayer::init() { if(!LayerColor::init()) { return false; } Director* director = Director::getInstance(); Vec2 visibleOrigin = director->getVisibleOrigin(); Size visibleSize = director->getVisibleSize(); setContentSize(visibleSize); GameLayer* gameLayer = GameLayer::create(IDLE); gameLayer->getUiLayer()->setVisible(false); addChild(gameLayer); // gameLayer->setColor(Color3B::RED); Widget* idleForeground = GUIReader::getInstance()->widgetFromJsonFile("ui/connect/idle_foreground.json"); Node* background = idleForeground->getChildByName("background"); background->setContentSize(visibleSize); Text* touchToStart = dynamic_cast<Text*>(idleForeground->getChildByName("touchToStart")); touchToStart->setString(LANG->getLang("touch_to_start")); touchToStart->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2)); touchToStart->runAction(RepeatForever::create(Blink::create(1, 1))); addChild(idleForeground); EventDispatcher* eventDispatcher = director->getEventDispatcher(); EventListenerTouchOneByOne* eventListener = EventListenerTouchOneByOne::create(); eventListener->setSwallowTouches(true); eventListener->onTouchEnded = [=](Touch* touch, Event* event){ auto scene = MainMenuLayer::createScene(); director->replaceScene(scene); }; eventListener->onTouchBegan = [](Touch* touch, Event* event){ return true; }; eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, touchToStart); CallFunc* connectAction = CallFunc::create([=](){ auto game = gameLayer->getGame(); if (!game->isEmpty() && gameLayer->getAcceptInput()) { auto path = game->getRandomPath(); if(path) { auto front = path->getPath().front(); auto back = path->getPath().back(); gameLayer->doConnect(front->getX(), front->getY(), back->getX(), back->getY()); } } }); CallFunc* shuffleAction = CallFunc::create([=](){ auto game = gameLayer->getGame(); if (!game->isEmpty()) { if (rand()%10 == 0) { gameLayer->doShuffle(); } } }); runAction(RepeatForever::create(Sequence::create(DelayTime::create(1.5), connectAction, shuffleAction, NULL))); return true; }