coco2d-js demo程序之滚动的小球

近期有一个游戏叫围住神经猫,报道说是使用html5技术来做的。 html5的跨平台的优良特性非常不错。对于人手不足,技术不足,选用html5技术实现跨平台的梦想真是不错。

近期在看coco2d-js这个跨平台游戏开发框架。非常不错,写了一个demo程序供大家參考。

/**
 * Created by caicai on 14-7-27.
 */
var Ball = cc.Sprite.extend({
    velocity:null,
    ctor:function () {
        this._super(res.Ball_png);
        var size = cc.director.getWinSize();
        this.x = size.width/2;
        this.y = size.height/2;

        this.velocity = cc.p(10,10);
    },
    update:function(dt){
        this.setPosition(cc.pAdd(this.getPosition(), cc.pMult(this.velocity, dt)));
        this.checkHitEdge();
    },
    checkHitEdge: function() {
        var pos = this.getPosition();
        var winSize = cc.director.getWinSize();

        if (pos.x > winSize.width - this.width || pos.x < this.width) {
            this.velocity.x *= -1;
        } else if (pos.y > winSize.height - this.height || pos.y < this.height) {
            this.velocity.y *= -1;
        }
    }
});

var GameLayer = cc.Layer.extend({
    _ball:null,
    _touchBeginAt: null,
    ctor:function () {
        this._super();

        this._ball = new Ball();
        this.addChild(this._ball);

        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan,
            onTouchMoved: this.onTouchMoved,
            onTouchEnded: this.onTouchEnded
        }, this);

        this.scheduleUpdate();
        return true;
    },

    update:function(dt){
        this._ball.update(dt);
    },

    onTouchBegan:function(touch, event) {
        this._touchBeginAt = touch.getLocation();
        console.log("begin")
        return true;
    },

    onTouchMoved:function(touch, event) {
    },

    onTouchEnded:function(touch, event) {
        console.log("end")
        var endAt = touch.getLocation();
        if(this._touchBeginAt == null) return true;
        var velocity = cc.pSub(endAt, this._touchBeginAt);
        event.getCurrentTarget()._ball.velocity = velocity;
        return true;
    }

});

var BallScene = cc.Scene.extend({
    layer:null,
    onEnter:function () {
        this._super();
        this.layer = new GameLayer();
        this.addChild(this.layer);

        this.schedule(this.update, 0);

    },
    update: function(dt){
        this.layer.update(dt);
    }

});

眼下还不完好,还有改进空间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值