新人学习教程,每次怪兽和星星的距离明显没到达设定值,但是打印出来的距离确在设定值之内,星星也消失了,这是怎么回事,求教呀~~~
以下是代码
cc.Class({
extends: cc.Component,
properties: {
//预制件星星
psesd: {
default: null,
type: cc.Prefab
},
//小怪兽
skss:{
default: null,
type: cc.Node
},
//背景
ground:{
default: null,
type: cc.Node
}
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.Players = this.skss;//主角小怪兽
this.groundY = this.ground.y + this.ground.height/2;//获取地平面位置
this.shengcheng();
},
// 生成预制物体星星
shengcheng(){
this.tPrefab = cc.instantiate(this.psesd);
this.tPrefab.parent = this.node;
this.tPrefab.setScale(0.5, 0.5)
this.tPrefab.setPosition(this.getNewStarPosition());
},
//随机生成星星的坐标
getNewStarPosition: function () {
var randX = 0;
// 根据地平面位置和主角跳跃高度,随机得到一个星星的 y 坐标
var randY = this.groundY + Math.random() * this.Players.y + 50;
// 根据屏幕宽度,随机得到一个星星 x 坐标
console.log(this.node.width)
var maxX = this.node.width/2;
randX = (Math.random() - 0.5) * 2 * maxX;
// 返回星星坐标
return cc.v2(randX, randY);
},
start () {
},
update (dt) {
//计算星星与小怪兽之间的距离
var playerPos =this.tPrefab.getPosition()
var dist = this.Players.position.sub(playerPos).mag();
if(dist<50){
//消除星星
this.tPrefab.destroy();
//重新生成随机星星
this.shengcheng()
}
},
});