刚入门cocoscreator,打算做一个贪吃蛇的项目来练练手,没想到以为很简单的一个贪吃蛇会遇到这么多问题。
计划使用2d物理刚体(RigidBody2D)组件的来完成得分的逻辑。
// 注册碰撞体回调的代码
this.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
// 使用时需主要,要为对应节点增加RigidBody2D组件,并设置enabledContactListener = true,才能正常回调
this.getComponent(RigidBody2D).enabledContactListener = true
注册完碰撞事件后,接下来就是完成对应的得分逻辑,我的逻辑处理是,在玩家吃到食物后,首先是增加蛇身长度,然后将吃到的食物重新移动到随机的一个位置 ,代码如下:
/**
* 碰撞事件回调
* @param selfCollider 吃到的食物
* @param otherCollider 蛇头
* @param contact
*/
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次
console.log("eat")
if (otherCollider.node.name=='Head'){
var snake = otherCollider.node.parent.getComponent(Snake)
snake.addNewTail()
}
selfCollider.node.getComponent(Food).moveToRandomPosition()
}
/**
* 移动自身到一个随机位置
*/
moveToRandomPosition(){
var width = this.gameObj.getComponent(Game).canvasWidth
var height = this.gameObj.getComponent(Game).canvasHeight
this.node.setPosition(v3(math.random() * width - width / 2, math.random() * height - height / 2))
}
以上代码存在的问题是,虽然蛇身能正常追加长度,但是moveToRandomPosition并未改变当前食物的位置,通过反复测试,个人猜测在creator的物理回调事件中的selfCollider: Collider2D中,对该Collider2D对附加到的节点组件属性更改并不会生效真实node,多次尝试发现失败后,尝试另一种思路:在碰撞事件回调函数中,不立即进行moveToRandomPosition,改为已经被吃掉的食物设置一个flag,标志其是否需要重新移动到随机位置,然后在update中判断flag来完成移动,代码如下:
/**
* 碰撞事件回调
* @param selfCollider 吃到的食物
* @param otherCollider 蛇头
* @param contact
*/
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次
...
selfCollider.node.getComponent(Food).alive = false
}
update (deltaTime: number) {
// 判断flash是否需要重新移动到随机位置
if (!this.alive){
this.moveToRandomPosition()
this.alive = true
}
}