Ten mobile game development artifact cocos2d-x editor touch event

Source: Internet
Author: User

In this section, I will allow little monsters to move in the direction of my touch, while the place of the touch will produce a four-byte divergence effect.

 

The effect is as follows:

 

 

Code download: http://www.kuaipan.cn/file/id_25348935635744782.htm? Source = 1

 

 

Open MainLayer. js and modify the onDidLoadFromCCB function as follows to make the touch available;

 

MainLayer.prototype.onDidLoadFromCCB = function () {    if (sys.platform == 'browser') {        this.onEnter();    }    else {        this.rootNode.onEnter = function () {            this.controller.onEnter();        };    }    this.rootNode.schedule(function (dt) {        this.controller.onUpdate(dt);    });    this.rootNode.onExit = function () {        this.controller.onExit();    };    this.rootNode.onTouchesBegan = function (touches, event) {        this.controller.onTouchesBegan(touches, event);        return true;    };    this.rootNode.onTouchesMoved = function (touches, event) {        this.controller.onTouchesMoved(touches, event);        return true;    };    this.rootNode.onTouchesEnded = function (touches, event) {        this.controller.onTouchesEnded(touches, event);        return true;    };    this.rootNode.setTouchEnabled(true);};

Add the touch start function at the bottom of the file, touch the mobile function, and touch the end function;

 

 

MainLayer.prototype.onTouchesBegan = function (touches, event) {    var loc = touches[0].getLocation();}MainLayer.prototype.onTouchesMoved = function (touches, event) {    cc.log(onTouchesMoved);}MainLayer.prototype.onTouchesEnded = function (touches, event) {    cc.log(onTouchesEnded);}

 

 

Create a function for moving small monsters by point (x, y;

 

MainLayer. prototype. monsterMove = function (x, y) {this. monster. stopAllActions (); cc. animationCache. getInstance (). addAnimations (Resources/snow_frame.plist); // Add the Frame Animation file var action0 = cc. sequence. create (cc. moveTo. create (5, cc. p (x, y); // move var actionFrame = cc forward. animate. create (cc. animationCache. getInstance (). getAnimation (monster); // get the Frame Animation var action1 = cc. repeat. create (actionFrame, 90000); var action2 = cc. spawn. create (action0, action1); // synchronize the animation this. monster. runAction (action2 );}

When the touch ends, add the monsterMove function. When a point is touched, the little monster will immediately move to this position;

 

 

MainLayer.prototype.onTouchesEnded = function (touches, event) {    cc.log(onTouchesEnded);    var loc = touches[0].getLocation();    this.monsterMove(loc.x, loc.y);}

Create a divergence particle under the "participation" directory as taught by the blog. Now, add the divergence effect to the touch. The effect period is 3 seconds and disappears after 3 seconds;

 

 

Open MainLayer. js again and add the particle creation function.

 

MainLayer.prototype.createParticle = function (name, x, y) {    var particle = cc.ParticleSystem.create(Resources/particles/ + name + .plist);    particle.setAnchorPoint(cc.p(0.5, 0.5));    particle.setPosition(cc.p(x, y));    particle.setPositionType(1);    particle.setDuration(3);    this.rootNode.addChild(particle);}

At the same time, add the particle function at the end of the touch;

 

 

MainLayer.prototype.onTouchesEnded = function (touches, event) {    cc.log(onTouchesEnded);    var loc = touches[0].getLocation();    this.monsterMove(loc.x, loc.y);    this.createParticle(around, loc.x, loc.y);}

Click Run. Everything is OK;

 

 

 

 

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.