zoukankan      html  css  js  c++  java
  • cocos2dx-触摸事件

     1 //创建一个精灵
     2     Sprite *spriteTest = Sprite::create("1.png");
     3     spriteTest->setPosition(Point(100,100));
     4     this->addChild(spriteTest);
     5    //创建一个单点触摸事件监听
     6     auto listener = EventListenerTouchOneByOne::create();
     7     //向下传递触摸
     8     listener->setSwallowTouches(true);
     9     //触摸回调
    10     listener->onTouchBegan = [](Touch *touch,Event *event)
    11     {
    12         auto target = event->getCurrentTarget();
    13         Point locationInNode = target->convertToNodeSpace(touch->getLocation());
    14         Size size = target->getContentSize();
    15         Rect rect = Rect(0, 0, size.width, size.height);
    16         if (rect.containsPoint(locationInNode)) {
    17             CCLOG("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
    18             target->setOpacity(180);
    19             return true;
    20         }
    21         return false;
    22     };
    23     
    24     listener->onTouchMoved = [](Touch *touch,Event *event)
    25     {
    26         auto target = event->getCurrentTarget();
    27         target->setPosition(target->getPosition()+touch->getDelta());
    28     };
    29     
    30     listener->onTouchEnded = [](Touch *touch,Event *event)
    31     {
    32         auto target = event->getCurrentTarget();
    33         target->setOpacity(255);
    34     };
    35     
    36     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, spriteTest);
  • 相关阅读:
    2019-2020nowcoder牛客寒假基础2
    2019-2020nowcoder牛客寒假基础1
    CF1291
    Daily Codeforces
    2019ICPC 上海现场赛
    Codeforces Round #686 (Div. 3)
    Codeforces Round #685 (Div. 2)
    Educational Codeforces Round 98 (Rated for Div. 2)
    Codeforces Round #654 (Div. 2)
    Codeforces Round #683 (Div. 2, by Meet IT)
  • 原文地址:https://www.cnblogs.com/wanyongjian/p/5093390.html
Copyright © 2011-2022 走看看