coco2dx---onTouchBegan

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}

auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
//    you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
                                       "CloseNormal.png",
                                       "CloseSelected.png",
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                            origin.y + closeItem->getContentSize().height/2));

// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);

/////////////////////////////
// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label

auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
                        origin.y + visibleSize.height - label->getContentSize().height));

// add the label as a child to this layer
this->addChild(label, 1);

// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");

// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer
this->addChild(sprite, 0);

auto button = cocos2d::ui::Button::create("HelloWorld.png");
button->setPosition(Vec2(origin.x + visibleSize.width/4,
                         origin.y + visibleSize.height - label->getContentSize().height));
button->addClickEventListener(CC_CALLBACK_1(HelloWorld::pp, this));
this->addChild(button);

schedule(schedule_selector(HelloWorld::handUpdate), 1.f, 10, 5);

auto redLayer = LayerColor::create(cocos2d::Color4B(100, 200, 300, 255), visibleSize.width/2, visibleSize.height/2);
redLayer->setTag(200);
redLayer->setIgnoreAnchorPointForPosition(false);
redLayer->setAnchorPoint(cocos2d::Vec2(0.5f, 0.5f));
redLayer->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2);
redLayer->setPositionZ(-80.f);
this->addChild(redLayer, 0);
redLayer->setLocalZOrder(1);
redLayer->setGlobalZOrder(1);

auto greenLayer = LayerColor::create(cocos2d::Color4B(200, 200, 255, 255), visibleSize.width/4, visibleSize.height/4);
greenLayer->setTag(100);
greenLayer->setIgnoreAnchorPointForPosition(false);
greenLayer->setAnchorPoint(cocos2d::Vec2(0.5f, 0.5f));
greenLayer->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2-50);
greenLayer->setPositionZ(0);
this->addChild(greenLayer, 1);
greenLayer->setLocalZOrder(0);
greenLayer->setGlobalZOrder(0);

auto listener1 = EventListenerTouchOneByOne::create();
listener1->onTouchBegan = [](Touch* touch, Event* event)
{
    // 獲取事件所綁定的 target
    auto target = static_cast<Node*>(event->getCurrentTarget());
    
    // 獲取當(dāng)前點擊點所在相對按鈕的位置坐標(biāo)
    Point pt = touch->getLocation();
    Point locationInNode = target->convertToNodeSpace(pt);
    Size s = target->getContentSize();
    Rect rect = Rect(0, 0, s.width, s.height);
    
    // 點擊范圍判斷檢測
    if (rect.containsPoint(locationInNode))
    {
        log("sprite1 began... x = %f, y = %f", locationInNode.x, locationInNode.y);
        target->setOpacity(180);
        return true;
    }
    
    log("next pass1");
    return false;
};
listener1->setSwallowTouches(false);

auto listener2 = EventListenerTouchOneByOne::create();
listener2->onTouchBegan = [](Touch* touch, Event* event)
{
    // 獲取事件所綁定的 target
    auto target = static_cast<Node*>(event->getCurrentTarget());
    
    // 獲取當(dāng)前點擊點所在相對按鈕的位置坐標(biāo)
    Point pt = touch->getLocation();
    Point locationInNode = target->convertToNodeSpace(pt);
    Size s = target->getContentSize();
    Rect rect = Rect(0, 0, s.width, s.height);
    
    // 點擊范圍判斷檢測
    if (rect.containsPoint(locationInNode))
    {
        log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
        target->setOpacity(180);
        return true;
    }
    
    log("next pass");
    return false;
};
listener2->setSwallowTouches(true);

this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, redLayer);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, greenLayer);

return true;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,890評論 0 33
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,005評論 0 23
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 問題:follow() 調(diào)用本類的另一個 函數(shù) addFollow();不成功,提示未定義函數(shù):Call to ...
    abs1004閱讀 724評論 0 0
  • 昨晚作業(yè): ?填寫表格(可以每天填寫) ?定一個目標(biāo)體重,然后抽人像卡表示對這個體制的真實看法 109斤-定的高了...
    徐小婷寶貝閱讀 285評論 0 1

友情鏈接更多精彩內(nèi)容