温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

十 手游开发神器 cocos2d-x editor 之触摸事件

发布时间:2020-07-13 22:01:08 来源:网络 阅读:458 作者:makeapp628 栏目:游戏开发

这一节 我将实现让小怪物跟随我的触摸方向移动,同时触摸的地方产生一个四周发散的效果


效果如下:

十 手游开发神器  cocos2d-x  editor 之触摸事件

十 手游开发神器  cocos2d-x  editor 之触摸事件


代码下载:http://www.kuaipan.cn/file/id_25348935635744782.htm?source=1



打开MainLayer.js,把onDidLoadFromCCB函数修改如下,让触摸可用;

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

在文件底部加入 触摸开始,触摸移动,触摸结束函数;

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"); }


再创建小怪物的根据点(x,y)移动的函数;

MainLayer.prototype.monsterMove = function (x, y) {     this.monster.stopAllActions();     cc.AnimationCache.getInstance().addAnimations("Resources/snow_frame.plist");//添加帧动画文件     var action0 = cc.Sequence.create(cc.MoveTo.create(5, cc.p(x, y)));  //向前移动     var actionFrame = cc.Animate.create(cc.AnimationCache.getInstance().getAnimation("monster"));   //获取帧动画     var action1 = cc.Repeat.create(actionFrame, 90000);     var action2 = cc.Spawn.create(action0, action1); //同步动画     this.monster.runAction(action2); }

触摸结束时,加入monsterMove函数,这时触摸一个点,小怪物会立刻移动到该位置;

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

按照之前博客教过的在particles目录下创建一个发散粒子,现在在触摸的地方加入发散效果,效果周期为3秒,3秒后消失;

十 手游开发神器  cocos2d-x  editor 之触摸事件


再次打开MainLayer.js,加入创建粒子的函数

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

同时在触摸结束时加入粒子函数;

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

点击运行;一切OK;


下一篇文章 我会介绍cocos2d-x  editor的音乐和音效       笔者(李元友)

资料来源:cocos2d-x  editor


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI