Coco2d-js The rolling Ball of the demo program

Source: Internet
Author: User
Tags addchild

A recent game called the HTML5, reported to have been done using the technology of the cat. HTML5 's cross-platform excellent features are very good, for manpower shortage, technical shortcomings, the choice of HTML5 technology to achieve cross-platform dream is really good.

Recently in See Coco2d-js this cross-platform game development framework, very good, wrote a demo program for everyone to 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, on        TouchMoved: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); }});

It's not perfect at the moment, there is room for improvement.

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.