Reputation: 37
I used latest version cocos2d-js to create my game. On the game screen, I added multiple sprites overlay in a row, like this Overlay sprites
I added an event listener to move up a sprite on y-axis when it was clicked. However, when I clicked on the point that any two sprites contain, the two sprites moved up together.
This is my event listener code
var listener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget();
var location = target.convertToNodeSpace(touch.getLocation());
var targetSize = target.getContentSize();
var targetRectangle = cc.rect(0, 0, targetSize.width, targetSize.height);
if (cc.rectContainsPoint(targetRectangle, location)){
target.setPositionY(50);
}
}
});
How can I prevent to move them up together and move only one sprite?
Thanks.
Upvotes: 1
Views: 435
Reputation: 96
onTouchBegan
must returns boolean value as result, if returns true
it's means touch was handled and event cycle will be stopped. Try to return true
, if rect contains point.
Hope this helps. And sorry for my English.
Upvotes: 1