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,
hammerSprite:null,
levelSprite: null,
ctor:function () {
//////////////////////////////
// 1. super init first
this._super();
/////////////////////////////
// 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));
this.hammerStaticSprite.attr({
x: size.width/2,
y: 20+size.height/10
});
this.addChild(this.hammerStaticSprite, 1);//scrTrgimg
// this.hammerStaticSprite.setVisible(false);
this.hammerStaticSprite.setTag(“staticH”);
cc.spriteFrameCache.addSpriteFrames(res.hammer_plist);
this.spriteSheet = new cc.SpriteBatchNode(res.hammer_png);
this.addChild(this.spriteSheet,2);
// init runningAction
var animFrames = [];
for (var i = 0; i < 12; i++) {
var str = i + ".png";
var frame = cc.spriteFrameCache.getSpriteFrame(str);
animFrames.push(frame);
}
var animation = new cc.Animation(animFrames, 0.06);
this.runningAction = new cc.RepeatForever(new cc.Animate(animation));
this.hammerSprite = new cc.Sprite("#0.png");
this.hammerSprite.attr({
x: size.width / 2,
y: 20 + size.height / 10
});
this.hammerSprite.runAction(this.runningAction);
this.spriteSheet.addChild(this.hammerSprite, 2);
this.hammerSprite.setTag("nonstaticH");
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.
Thanks