这是完整代码
cc.Class({
extends: cc.Component,
properties: {
starPrefab:{
default: null,
type:cc.Prefab,
},
maxStarDuration: 0,
minStarDuration: 0,
ground:{
default: null,
type:cc.Node,
},
player: {
default: null,
type:cc.Node,
}
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
//获取地面的y坐标
var groundY = this.ground.y + this.ground.height / 2;
this.spawnNewStar();
},
spawnNewStar: function () {
var newStar = cc.instantiate(this.starPrefab);
this.node.addChild(newStar);
newStar.setPosition(this.getNewStarPosition());
newStar.getComponent(‘star’).game = this;
},
getNewStarPosition: function () {
var randX = 0;
// 根据地平面位置和主角跳跃高度,随机得到一个星星的 y 坐标
var randY = this.groundY + Math.random() * this.player.getComponent('player').jumpHeight;
console.log(randY);
var maxX = this.node.width / 2;
randX = (Math.random() - 0.5) * 2 * maxX;
// 返回星星坐标
return cc.v2(randX, randY);
},
start () {
},
update(dt) {
},
});