球与球的碰撞检测java测试_cocos 躲避球游戏(2) --资源导入和碰撞检测

效果如下

37c6bbe1fd867a46a34943a7ef870311.gif

复制图片资源

网上随便找个图片就行, 先拖进左下角资源管理器, 然后从资源管理器中拖到canvas结点下, 并修改名称

dee166621d19910b94d952f218e0ee7c.png

添加拖拽脚本

两个飞机的逻辑相同, 所以可以使用同一个脚本

onLoad() {

console.error("plane", this, this.node);

let mouseDown = false;

this.node.on(cc.Node.EventType.MOUSE_DOWN, (event) => {

mouseDown = true;

});

this.node.on(cc.Node.EventType.MOUSE_MOVE, (event) => {

if (!mouseDown) return;

//获取鼠标距离上一次点的信息

let location = event.getLocation();

this.node.position = this.node.parent.convertToNodeSpaceAR(location);

});

//当鼠标抬起的时候恢复状态

this.node.on(cc.Node.EventType.MOUSE_UP, (event) => {

mouseDown = false;

});

},

1104e8127e491dcb5527c09cbbb779a4.png

开启碰撞检测

使用box即可, 会自动设置为图片大小

2a00ac2f4048dd5c42c1c20fc364d46d.png

碰撞发生后, 将事件抛出去, 在GameScene层做接受和处理

cc.Class({

extends: cc.Component,

properties: {

},

onLoad() {

console.error("plane", this, this.node);

let mouseDown = false;

this.node.on(cc.Node.EventType.MOUSE_DOWN, (event) => {

mouseDown = true;

});

this.node.on(cc.Node.EventType.MOUSE_MOVE, (event) => {

if (!mouseDown) return;

//获取鼠标距离上一次点的信息

let location = event.getLocation();

this.node.position = this.node.parent.convertToNodeSpaceAR(location);

});

//当鼠标抬起的时候恢复状态

this.node.on(cc.Node.EventType.MOUSE_UP, (event) => {

mouseDown = false;

});

const manager = cc.director.getCollisionManager();

manager.enabled = true;

},

// 碰撞产生的时候

onCollisionEnter(other, self) {

console.error("onCollisionEnter", other, self);

this.node.dispatchEvent(new cc.Event.EventCustom("hitStart", true));

},

// 当碰撞产生后,碰撞结束前的情况下,每次计算碰撞结果后调用

onCollisionStay: function (other, self) {

console.log("on collision stay");

this.node.dispatchEvent(new cc.Event.EventCustom("hitStay", true));

},

// 当碰撞结束后调用

onCollisionExit: function (other, self) {

console.log("on collision exit");

this.node.dispatchEvent(new cc.Event.EventCustom("hitEnd", true));

},

start() {},

// update (dt) {},

});

gameScene接受事件并响应, 设置label表示当前状态

onLoad() {

this.backBtn.node.on(

cc.Node.EventType.TOUCH_START,

this.backBtnHandle.bind(this)

);

this.node.on("hitStay", () => {

console.log("相撞", this.label.string, this.label.node.string);

this.label.string = "碰撞中";

});

this.node.on("hitEnd", () => {

console.log("相撞", this.label.string, this.label.node.string);

this.label.string = "碰撞结束";

});

this.node.on("hitStart", () => {

console.log("相撞", this.label.string, this.label.node.string);

this.label.string = "开始碰撞";

});

},

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值