有人可以告诉我哪里出错了吗?我一直在看这个一个小时,我不明白为什么不是这个.x未定义。
它在某处:
var canvas, canvasContext;
var blueCar = new carClass();
window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
colorRect(0,0,canvas.width,canvas.height,'red');
colorText(" LOADING...",canvas.width/3,canvas.height/2,'white',"30px Arial")
loadImages();
}
function imageLoadingDoneSoStartGame(){
var framesPerSecond = 30;
setInterval(updateAll, 1000/framesPerSecond);
setUpInput();
bluecar.reset();
}
function updateAll() {
moveAll();
drawAll();
}
function moveAll() {
blueCar.move();
carTrackHandling();
}
function drawAll() {
console.log(bluecar.x);
drawTracks();
blueCar.draw(blueCar);
}
这里:
function carClass(){
this.x = 75;
this.y = 75;
this.ang = 0;
this.speed = 0;
//reset car 1
this.reset = function() {
for(var eachRow=0;eachRow<TRACK_ROWS;eachRow++) {
for(var eachCol=0;eachCol<TRACK_COLS;eachCol++) {
var arrayIndex = rowColToArrayIndex(eachCol, eachRow);
if(trackGrid[arrayIndex] == TRACK_BLUE_START) {
trackGrid[arrayIndex] = TRACK_ROAD;
this.ang = -Math.PI/2;
this.x= eachCol * TRACK_W + TRACK_W/2;
this.y= eachRow * TRACK_H + TRACK_H/2;
}//end of player start if
}//end if col for
}//end if row for
}//end of car reser function
//move car 1
this.move = function() {
this.speed *= GROUND_SPEED_DECAY_MULT
if(keyHeld_Gas){
this.speed += DRIVE_POWER;
}
if(keyHeld_Reverse){
this.speed -= REVERSE_POWER;
}
if(Math.abs(this.speed) > MIN_SPEED_TO_TURN){
if(keyHeld_TurnLeft){
this.ang -= TURN_RATE;
}
if(keyHeld_TurnRight){
this.ang += TURN_RATE;
}
}
this.x += Math.cos(this.ang) * this.speed;
this.y += Math.sin(this.ang) * this.speed;
// carAng += 0.02
}
this.draw = function(){
drawBitmapCenteredWithRoatation(blueCarPic, this.x,this.y, this.ang);
}
}
或在这里:
function carTrackHandling(wichCar) {
var carTrackCol = Math.floor(wichCar.x / TRACK_W);
var carTrackRow = Math.floor(wichCar.y / TRACK_H);
var trackIndexUnderCar = rowColToArrayIndex(carTrackCol, carTrackRow);
if(carTrackCol >= 0 && carTrackCol < TRACK_COLS &&
carTrackRow >= 0 && carTrackRow < TRACK_ROWS) {
if(isObstacleAtColRow( carTrackCol,carTrackRow )) {
//fixes bug
wichCar.x -= Math.cos(wichCar.ang) * wichCar.speed;
wichCar.y -= Math.sin(wichCar.ang) * wichCar.speed;
//reduse speed and bounces car when hits a wall
wichCar.speed*= -0.5;
} // end of track found
} // end of valid col and row
} // end of carTrackHandling func
我得到的确切错误是:
其中Track.js:是:var carTrackCol = Math.floor(wichCar.x / TRACK_W);
Uncaught TypeError: Cannot read property 'x' of undefined
at carTrackHandling (Track.js:34)
at moveAll (Main.js:29)
at updateAll (Main.js:23)
任何帮助都会被发现,并告诉我我做错了什么,谢谢。
答案 0 :(得分:0)
您的功能定义为function carTrackHandling(wichCar) {
。它需要一个参数,这是你想要处理的汽车(我假设)。
如果您使用moveAll()
功能,则不会将汽车传递给您的功能。当您的函数尝试访问x
whichCar
时,它会抛出错误,因为它未定义(因为它没有通过任何汽车)。如下图所示,将您想要处理的汽车通过moveAll()
功能:
carTrackHandling();
应该是:
blueCar.move();
carTrackHandling(blueCar);
答案 1 :(得分:0)
看起来你没有将任何内容传递给function moveAll() {
blueCar.move();
carTrackHandling();
}
并且期望传递一个对象。
看起来你在这里没有参数调用它。
whichCar
然后,当您使用此函数时function carTrackHandling(wichCar) {
var carTrackCol = Math.floor(wichCar.x / TRACK_W);
...
}
将被定义为导致您看到的错误。
model.outputs[0].save('CNTK_model2.pb')