// Learn cc.Class:
// - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
const MOVE_LEFT = 1;
const MOVE_RIGHT = 2;
const MOVE_UP = 3;
cc.Class({
extends: cc.Component,
properties: {
maxSpeed: 12,
},
onLoad: function () {
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
let canvas = cc.find("/Canvas");
canvas.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
canvas.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
this.moveFlags = 0;
this.canJump = 0;
},
start: function () {
this.body = this.getComponent(cc.RigidBody);
},
onKeyDown(event) {
switch (event.keyCode) {
case cc.macro.KEY.a:
case cc.macro.KEY.left:
this.moveFlags |= MOVE_LEFT;
this.updateMotorSpeed();
break;
case cc.macro.KEY.d:
case cc.macro.KEY.right:
this.moveFlags |= MOVE_RIGHT;
this.updateMotorSpeed();
break;
case cc.macro.KEY.w:
case cc.macro.KEY.up:
this.canJump |= MOVE_UP;
this.updateJump();
break;
}
},
onKeyUp(event) {
switch (event.keyCode) {
case cc.macro.KEY.a:
case cc.macro.KEY.left:
this.moveFlags &= ~MOVE_LEFT;
// this.updateMotorSpeed();
break;
case cc.macro.KEY.d:
case cc.macro.KEY.right:
this.moveFlags &= ~MOVE_RIGHT;
// this.updateMotorSpeed();
break;
case cc.macro.KEY.w:
case cc.macro.KEY.up:
this.canJump &= ~MOVE_UP;
// this.updateJump();
break;
}
},
onTouchStart: function (event) {
let touchLoc = event.touch.getLocation();
if (touchLoc.x < cc.winSize.width / 2) {
this.moveFlags |= MOVE_LEFT;
}
else {
this.moveFlags |= MOVE_RIGHT;
}
this.body.applyForceToCenter(cc.Vec2(0, touchLoc.y))
// this.updateMotorSpeed();
},
onTouchEnd: function (event) {
let touchLoc = event.touch.getLocation();
if (touchLoc.x < cc.winSize.width / 2) {
this.moveFlags &= ~MOVE_LEFT;
}
else {
this.moveFlags &= ~MOVE_RIGHT;
}
// this.updateMotorSpeed();
},
updateMotorSpeed() {
if (!this.body)
return;
var desiredSpeed = 0;
if ((this.moveFlags & MOVE_LEFT) == MOVE_LEFT)
desiredSpeed = -this.maxSpeed;
else if ((this.moveFlags & MOVE_RIGHT) == MOVE_RIGHT)
desiredSpeed = this.maxSpeed;
this.body.angularVelocity = desiredSpeed;
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
if (this.moveFlags) {
this.updateMotorSpeed();
}
// if (this.canJump) {
// this.updateJump();
// }
},
updateJump: function () {
console.log('jump')
// 获取移动速度
let velocity = this.body.linearVelocity;
velocity.y = this.maxSpeed * 10;
// 设置移动速度
this.body.linearVelocity = velocity;
// this.body.applyForce(cc.v2(0, this.maxSpeed * 10), cc.v2(this.x, this.y + 300), true);
// this.body.applyForceToCenter(cc.Vec2(0, this.maxSpeed));
}
// update(dt) { },
}
);
Flybird是一款采用Cocos游戏引擎进行开发的游戏;.zip
需积分: 5 180 浏览量
2024-04-19
18:26:37
上传
评论
收藏 256KB ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/d7864c98224046ec9a7f43fa6207c84e_qq_51320133.jpg!1)
生瓜蛋子
- 粉丝: 3830
- 资源: 6140
最新资源
- ASP.NET公文管理系统的设计与实现(源码)
- 操作系统原理与设计Chapter 2: OS Structure
- torch-2.3.1-cp312-cp312-manylinux2014-aarch64.whl
- CSR8675蓝牙芯片 CSR内部培训资料教材资料.zip
- 43-2-每日英语听力 10.9.2会员版_鹿蜀 【20240530更新】.apk
- 期末大作业基于EasyX和C语言的可视化学生成绩管理系统(95分以上)
- 数字电路芯片74系列芯片datasheet技术手册资料总汇合集(241个).zip
- CSDNApp_226.apk
- CCNA实训2022.pka
- 金融分析期末作业.ipynb
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)