cocos creator 键盘监听事件 UI creama 组件的使用笔记

1.实现左右移动,空格跳跃 ,屏幕跟随主角

2.键盘监听事件,creama UI组件

3.记得开启碰撞引擎,绑到对应脚本就行了

 cc.systemEvent.on(type, callback, target);

//hero.js

// 键盘,水平移动,左右

// 跳跃为空给 cc.Class({ extends: cc.Component, properties: { }, // use this for initialization onLoad: function () { // 代码获取 this.body = this.node.getComponent(cc.RigidBody); // end cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.on_key_down.bind(this), this); cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.on_key_up.bind(this), this); this.input_flag = 0; }, jump: function() { var v = this.body.linearVelocity; v.y = 600; this.body.linearVelocity = v; }, // -1, 1 walk: function(dir) { var v = this.body.linearVelocity; v.x = 300 * dir; this.body.linearVelocity = v; this.node.scaleX = dir; }, on_key_down: function(e) { switch(e.keyCode) { case cc.KEY.left: this.input_flag = -1; break; case cc.KEY.right: this.input_flag = 1; break; case cc.KEY.space: this.jump(); break; } }, on_key_up: function(e) { switch(e.keyCode) { case cc.KEY.left: this.input_flag = 0; break; case cc.KEY.right: this.input_flag = 0; break; case cc.KEY.space: break; } }, // called every frame, uncomment this function to activate update callback update: function (dt) { if (this.input_flag !== 0) { this.walk(this.input_flag); } }, });

creama 主要是转换世界坐标,target 只跟随x

//creama.js

cc.Class({

extends: cc.Component, properties: { target: { default: null, type: cc.Node, }, }, onLoad: function () { }, update: function (dt) { if (this.target === null) { return; } // target到哪里, camera跟到哪里 var wpos = this.target.convertToWorldSpaceAR(cc.p(0, 0)); var pos = this.node.parent.convertToNodeSpaceAR(wpos); this.node.x = pos.x; }, });



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值