coco2d-js demo程式之滾動的小球

來源:互聯網
上載者:User

標籤:man   target   ret   cto   date   cat   滾動   city   html5   

近期有一個遊戲叫圍住神經貓,報道說是使用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);    }});

眼下還不完好,還有改進空間。

coco2d-js demo程式之滾動的小球

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.