bool Game::init()
{
for(int a=0;a<13;a++)
{
auto sprite=Sprite::create((name+a)) ///name是一个char数组,储存了图片名字!
sprites->setPosition(40+10a, 80);//精灵依次排列,有重叠部分。
addChild(sprite)
Click(sprite,80)
}
return true;
}
void Game::Click(Sprite _sprite, float Y)
{
anto poker = EventListenerTouchOneByOne::create();
//poker->setSwallowTouches(true);//如果设置禁止向下传递触摸,就只有最后一个精灵响应,不设置点击重叠位置,只要那个位置有的精灵都响应了!!!
poker->onTouchBegan = [&](Touch touch, Event* event1) {
auto sprite = (Sprite*)event1->getCurrentTarget();
auto pos = Director::getInstance()->convertToGL(touch->getLocationInView());
auto zX = sprite->getPositionX();
auto zY = sprite->getPositionY();
bool YorN = sprite->getBoundingBox().containsPoint(pos);
if (YorN)
{
if (zY ==Y){
auto moveto = MoveTo::create(0, Point(zX, Y + 10));
sprite->runAction(moveto);
}
else{
auto moveto = MoveTo::create(0, Point(zX, Y));
sprite->runAction(moveto);
}
}
else{
auto moveto = MoveTo::create(0, Point(zX, Y));
sprite->runAction(moveto);
}
return true; };
_eventDispatcher->addEventListenerWithSceneGraphPriority(poker, _sprite);
}
怎么能点击时只有被点击的精灵响应!!!