图片的点击事件 和场景精灵类

var Sen = cc.Sprite.extend({
    direction:1,
    direc:1,
    const:5,
    p:3,
    ctor:function(){
        this._super("res/png_1.png");
        this.const = parseInt(Math.random()*5)
        this.setPosition(Math.random()*cc.winSize.width,Math.random()*cc.winSize.height)
        this.scheduleUpdate()
        this._init()
    },
    update:function(){
        this.x += this.const*this.direction
        this.y += this.const*this.direc
        if(this.x < 0 || this.x >cc.winSize.width){
            this.direction *= -1
        }
        if(this.y < 0 || this.y >cc.winSize.height){
            this.direc *= -1
        }
    },
	//点击事件
    _init:function(){
        var that = this;
        var listener = 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)){
                    that._replace(target)
                }
            }
        });
        cc.eventManager.addListener(listener, this);
    },
    _replace:function(target){
        this.p--
        if(this.p<=0){
            target.removeFromParent(true)
        }
        this.initWithFile("res/png_"+this.p+".png")
    }
})
 
var HelloWorldLayer = cc.Layer.extend({

    ctor:function () {
        this._super();
        this._init();
    },
    _init:function (){
        for(var i=0;i<5;i++){
            var sq = new Sen()
            this.addChild(sq)
        }
    }
});
var HelloWorldScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new HelloWorldLayer();
        this.addChild(layer);
    }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值