2013-12-14 3 views
0

안녕하세요. 다시 게임 코드에서 학교용 결함이 발생했습니다. 이제 "Player Rocket"을 추가하려고합니다. 코드입니다. 이 책은 내가 생성 된 모든 소행성 아래에 추가했다. Uncaught TypeError : 정의되지 않은 'vX'속성을 설정할 수 없습니다.

player.vX = 0; 
    player.vY = 0; 

    if (player.moveRight) { 
     player.vX = 3; 
    }; 

    if (player.moveUp) { 
     player.vY = -3; 
    }; 

    if (player.moveDown) { 
     player.vY = 3; 
    }; 

    player.x += player.vX; 
    player.y += player.vY; 

    context.fillStyle = 'rgb(255, 0, 0)'; 
    context.beginPath(); 
    context.moveTo(player.x+player.halfWidth, player.y); 
    context.lineTo(player.x-player.halfWidth, player.y-player.halfHeight); 
    context.lineTo(player.x-player.halfWidth, player.y+player.healfHeight); 
    context.closePath(); 
    context.fill(); 

이제 오류가 나는 곳 어떤이를 정의했다하지만 난 아무것도 그리워하지 않은 경우 나는 책을 통해 검토 한

Uncaught TypeError: Cannot set property 'vX' of undefined

보고 ... 그것은이가 계속. 지금 나는 아직도 이것에 정말로 새롭다. 그러나 나를 만나는 유일한 지점은 player.vX 다.

var player; 
var Player = function(x, y){ 
    this.x = x; 
    this.y = y; 
    this.width = 24; 
    this.height = 24; 
    this.halfWidth = this.width/2; 
    this.halfHeight = this.height/2; 
    this.moveRight = false; 
    this.moveUp = false; 
    this.moveDown = false; 

    this.vX = 0; 
    this.vY = 0; 
}; 

누구든지 나를 도와 줄 수 있다면 좋을 것입니다. 고맙습니다!!!

+0

를 살펴한다? – Misters

+2

해당 코드가 책에서 나온 경우 책을 창 밖으로 던져 다른 책을 찾으십시오. – adeneo

+0

당신은이 수수께끼에 그것을 게시 할 수 있습니까? 그래서 우리는이 모든 캔버스 것들로 직접 오류를 볼 수 있습니까? 감사합니다 – xhallix

답변

0

var player;라고 말하면 player을 정의되지 않음으로 설정합니다. Player()의 새 인스턴스를 만들어야합니다. 이 생성자와 유사한 이름을 가진 무언가를 시작하는 새로운 플레이어의 인스턴스를 생각하는 경우가

var player=new Player(xCoord,yCoord); 

그 책을하여이 작업을 수행 할 수있는 것은, 명백히 잘못이다. 당신이 플레이어 클래스의 인스턴스를 생성 않은 경우

객체에 대한 자세한 내용은, 당신은 MDN's tutorial

+0

수 있습니다. 바이올린에 전체 코드가 있는지 살펴보십시오. 나는 방금 내가 말한 것을 어떻게 설정하는지 알지 못한다. http://jsfiddle.net/MK6d5/ – user3100660

관련 문제