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);