请大家帮帮忙 为什么我在摘星星进行到“添加主角碰触收集星星”这一步就总提示:“this.game is undefined”
请给出详细代码,或者说明
您好,
我觉得很有可能能是您按照教程来的时候在添加主角碰触收集星星的行为这一步忘了在Game.js的spawnNewStar这一步忘了添加
newStar.getComponent('Star').game = this;
这句函数。
代码是完全复制进去的 因为之前自己敲的时候也是到这一步就出现这个情况 所以我想完全复制试试 结果还是这样
我确定我是添加了的 因为我完全复制的
我怀疑是不是官网的代码有错误 我都有输入啊
那是不是你缺少了绑定节点呀
什么意思 ?
我还是怀疑问题出在这一步 这里是直接输入到spawnNewStar下面吗
你直接下载完整版的代码看看哪里写的不一样吧。
或者你把你的代码全传上来,光看这个错误也只能知道this.game没有定义啊
应该是你没有将 Game 组件添加到 Canvas 节点上吧。或者你下载官方的项目检查下每个组件的属性是不是都在编辑器中设置对了。
// scripts\Game.js
cc.Class({
‘extends’: cc.Component,
properties: {
// 这个属性引用了星星预制资源
starPrefab: {
'default': null,
type: cc.Prefab
},
// 星星产生后消失时间的随机范围
maxStarDuration: 0,
minStarDuration: 0,
// 地面节点,用于确定星星生成的高度
ground: {
'default': null,
type: cc.Node
},
// player 节点,用于获取主角弹跳的高度,和控制主角行动开关
player: {
'default': null,
type: cc.Node
}
},
// use this for initialization
onLoad: function onLoad() {
// 获取地平面的 y 轴坐标
this.groundY = this.ground.y + this.ground.height / 2;
// 生成一个新的星星
this.spawnNewStar();
},
spawnNewStar: function spawnNewStar() {
// 使用给定的模板在场景中生成一个新节点
var newStar = cc.instantiate(this.starPrefab);
// 将新增的节点添加到 Canvas 节点下面
this.node.addChild(newStar);
// 为星星设置一个随机位置
newStar.setPosition(this.getNewStarPosition());
//将Game组件的实例传入星星组件
newStar.getComponent('Star').game = this;
},
getNewStarPosition: function getNewStarPosition() {
var randX = 0;
// 根据地平面位置和主角跳跃高度,随机得到一个星星的 y 坐标
var randY = this.groundY + cc.random0To1() * this.player.getComponent('Player').jumpHeight + 50;
// 根据屏幕宽度,随机得到一个星星 x 坐标
var maxX = this.node.width / 2;
randX = cc.randomMinus1To1() * maxX;
// 返回星星坐标
return cc.p(randX, randY);
}
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
cc._RFpop();
},{}],“Player”:[function(require,module,exports){
“use strict”;
cc._RFpush(module, ‘fc997vQ/B1N+bbDtR9ap4IF’, ‘Player’);
// scripts\Player.js
cc.Class({
“extends”: cc.Component,
properties: {
// 主角跳跃高度
jumpHeight: 0,
// 主角跳跃持续时间
jumpDuration: 0,
// 最大移动速度
maxMoveSpeed: 0,
// 加速度
accel: 0
},
setJumpAction: function setJumpAction() {
// 跳跃上升
var jumpUp = cc.moveBy(this.jumpDuration, cc.p(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
// 下落
var jumpDown = cc.moveBy(this.jumpDuration, cc.p(0, -this.jumpHeight)).easing(cc.easeCubicActionIn());
// 不断重复
return cc.repeatForever(cc.sequence(jumpUp, jumpDown));
},
setInputControl: function setInputControl() {
var self = this;
// 添加键盘事件监听
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
// 有按键按下时,判断是否是我们指定的方向控制键,并设置向对应方向加速
onKeyPressed: function onKeyPressed(keyCode, event) {
switch (keyCode) {
case cc.KEY.a:
self.accLeft = true;
self.accRight = false;
break;
case cc.KEY.d:
self.accLeft = false;
self.accRight = true;
break;
}
},
// 松开按键时,停止向该方向的加速
onKeyReleased: function onKeyReleased(keyCode, event) {
switch (keyCode) {
case cc.KEY.a:
self.accLeft = false;
break;
case cc.KEY.d:
self.accRight = false;
break;
}
}
}, self.node);
},
// use this for initialization
onLoad: function onLoad() {
// 初始化跳跃动作
this.jumpAction = this.setJumpAction();
this.node.runAction(this.jumpAction);
// 加速度方向开关
this.accLeft = false;
this.accRight = false;
// 主角当前水平方向速度
this.xSpeed = 0;
// 初始化键盘输入监听
this.setInputControl();
},
// called every frame, uncomment this function to activate update callback
update: function update(dt) {
// 根据当前加速度方向每帧更新速度
if (this.accLeft) {
this.xSpeed -= this.accel * dt;
} else if (this.accRight) {
this.xSpeed += this.accel * dt;
}
// 限制主角的速度不能超过最大值
if (Math.abs(this.xSpeed) > this.maxMoveSpeed) {
// if speed reach limit, use max speed with current direction
this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed);
}
// 根据当前速度更新主角的位置
this.node.x += this.xSpeed * dt;
}
});
// },
cc._RFpop();
},{}],“Star”:[function(require,module,exports){
“use strict”;
cc._RFpush(module, ‘e823ag6D0BDOo3EWk9jIy1o’, ‘Star’);
// scripts\Star.js
cc.Class({
“extends”: cc.Component,
properties: {
// 星星和主角之间的距离小于这个数值时,就会完成收集
pickRadius: 0
},
// use this for initialization
onLoad: function onLoad() {},
getPlayerDistance: function getPlayerDistance() {
//根据player节点位置判断距离
var playerPos = this.game.player.getPosition();
//根据两点位置计算两点之间距离
var dist = cc.pDistance(this.node.position, playerPos);
return dist;
},
onPicked: function onPicked() {
//当星星被收集时,调用Game脚本中的接口,生成一个新的星星
this.game.spawnNewStar();
//然后销毁当前星星节点
this.node.destroy();
},
// called every frame, uncomment this function to activate update callback
update: function update(dt) {
//每帧判断和主角之间的距离是否小于收集距离
if (this.getPlayerDistance() < this.pickRadius) {
//调用收集行为
this.onPicked();
return;
}
}
});
// },
cc._RFpop();
},{}]},{},[“Game”,“Star”,“Player”])
请帮帮忙 谢谢
建议把整个工程打个压缩包上传。虽然没复制过教程的例程,但是按着写过并没什么问题.
另外 初学的话比较建议用浏览器,调试方便
你的代码没问题,毕竟都是复制的。我说的是编辑器中的操作问题,请参见我上一条回复。
保存脚本后将Game组件添加到层级编辑器中的Canvas节点上(选中Canvas节点后,拖拽脚本到 属性检查器 上,或点击 属性检查器 的 添加组件 按钮,并从 用户自定义脚本 中选择 Game,接下来从资源管理器中拖拽star Prefab 资源到Game组件的Star Prefab属性中。这是我们第一次为属性设置引用,只有在属性声明时规定type为引用类型时(比如我们这里写的cc.Prefab类型),才能够将资源或节点拖拽到该属性上。
接下来从层级编辑器中拖拽ground和Player 节点到组件中相同名字的属性上,完成节点引用。
请问这里是不是按照括号里面的办法 我都是这样设置的 怎么就是提示:“this.game is undefiend”
已经解决 好像是Player节点的位置放错了 谢谢大家的帮助