Coco2d-js demo program rolling ball

Source: Internet
Author: User

Coco2d-js demo program rolling ball

Recently, there was a game called a nervous cat. It was reported that it was made using html5 technology. The excellent cross-platform features of html5 are very good. For lack of manpower and technology, it is really good to use html5 technology to realize the dream of cross-platform.

Recently look at coco2d-js this cross-platform game development framework, very good, wrote a demo program for your reference.

/** * 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);    }});

There is still room for improvement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.