Home Ask Login Register

Developers Planet

Your answer is one click away!

Phuong Tr February 2016

cocos2d js - Touch on overlay sprites that move up together

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?



Dazlonar February 2016

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.

Post Status

Asked in February 2016
Viewed 2,972 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life