How to invisible sprite from touch listener of another sprite?

How to invisible sprite from touch listener of another sprite?
0.0 0


Hi , I’m just started learning cocos2d js. I have 2 sprite “hammerStaticSprite” and “hammerSprite”.

I create touch listener and add listener to these sprites. Both sprites are at same position and both are of same size. I set “hammerStaticSprite” to visible and “hammerSprite” to invisible.

What I want to do is that if I touch at hammerStaticSprite then I want to set visibility of hammerSprite to true and visibility of hammerStaticSprite to false in touch listener.

I try following but unable to hide another sprite.

ar GameLayer = cc.Layer.extend({
spriteSheet: null,
runningAction: null,
hammerStaticSprite: null,
sprite: null,
levelSprite: null,

ctor:function () {
    // 1. super init first

    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.
    // ask the window size
    var size = cc.winSize;

this.hammerStaticSprite = new cc.Sprite(res.staticHammer);
// this.hammerSprite.setAnchorPoint(cc.p(0, 0));
x: size.width/2,
y: 20+size.height/10
this.addChild(this.hammerStaticSprite, 1);//scrTrgimg
// this.hammerStaticSprite.setVisible(false);

    this.spriteSheet = new cc.SpriteBatchNode(res.hammer_png);

    // init runningAction
    var animFrames = [];
    for (var i = 0; i < 12; i++) {
        var str =  i + ".png";
        var frame = cc.spriteFrameCache.getSpriteFrame(str);

    var animation = new cc.Animation(animFrames, 0.06);
    this.runningAction = new cc.RepeatForever(new cc.Animate(animation));

    this.hammerSprite = new cc.Sprite("#0.png");
        x: size.width / 2,
        y: 20 + size.height / 10
    this.spriteSheet.addChild(this.hammerSprite, 2);

var listener1 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,

        onTouchBegan: function (touch, event) {
            var target = event.getCurrentTarget();
            var locationInNode = target.convertToNodeSpace(touch.getLocation());
            var s = target.getContentSize();
            var rect = cc.rect(0, 0, s.width, s.height);
            if (cc.rectContainsPoint(rect, locationInNode)) {
                labelLevelNo.setString("new Dog");
                cc.log("hammerSprite sprite was touched");
                return true;
            return false;
        onTouchEnded: function (touch, event) {
            var target = event.getCurrentTarget();

            if (target.getTag() == "nonstaticH")
                this.hammerStaticSprite.setVisible(false); //this I want to do.
         //   target.runAction(cc.sequence(sprite_action1, actionCallFunc));
            //var sprite_action1 = cc.MoveBy.create(1, cc.p(location.x- size.width / 2, location.y));

        onTouchMoved: function (touch, event) {
            labelLevelNo.setString("new1 onTouchMove");
    cc.eventManager.addListener(listener1.clone(), this.hammerSprite);
    cc.eventManager.addListener(listener1.clone(), this.hammerStaticSprite);

please help me.



the ‘this’ variable inside OnTouchEnded does not point to the Layer object.
you should replace

if (target.getTag() == "nonstaticH")
                this.hammerStaticSprite.setVisible(false); //this I want to do.


if (target.getTag() == "nonstaticH")
                target.setVisible(false); //this I want to do.


ok. Thank you very much.