How to add click listener for class Sprite

How to add click listener for class Sprite
0.0 0


I create some sprite class (they are just pictures).I want to click them and then make them move.
how to achieve it???



You need to add listen the touch event in its parent layer, and then when you handle the touch event, you can detect if it’s related to your sprite, so can do some manipulations. There is no direct event dispatching for sprites.



but how to detect if it’s related to my sprite…….


Firstly, you can register the sprite object as a property in your layer class or add the sprite with a predefined tag.
Then when you handle your touch event, just retrieve the sprite with your property or sprite tag so that you can check the touch point position with the bounding box of your sprite.

That’s it.


This is my source code,when I click the layer the console will print “layer was clicked”.
I want to the sprite is clicked that the console will print something else……
I halfly know what you mean but I don’t know how to exactly code it……
Can you change my codes

thank you,that is always you to anser me every time I ask the questions……


var GameLayer = cc.Layer.extend({
    ctor: function () {
        if ('mouse' in sys.capabilities )
    init: function () {
        //call super class's super function

        //add pop_sprite
        var pop_sprite=cc.Sprite.create(color[random_color]);
        console.log("layer was clicked......");

var GameScene = cc.Scene.extend({
    onEnter: function () {
        var layer = new GameLayer();