Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:无法设置属性';vX';未定义的_Javascript_Jquery_Html - Fatal编程技术网

Javascript 未捕获类型错误:无法设置属性';vX';未定义的

Javascript 未捕获类型错误:无法设置属性';vX';未定义的,javascript,jquery,html,Javascript,Jquery,Html,嗨,伙计们,我又一次在学校的游戏代码中遇到了一个漏洞。。。所以在这里,我试图添加我的“玩家火箭”,这是我刚刚在书中添加的所有生成小行星下的代码 player.vX = 0; player.vY = 0; if (player.moveRight) { player.vX = 3; }; if (player.moveUp) { player.vY = -3; }; if (player.moveDown

嗨,伙计们,我又一次在学校的游戏代码中遇到了一个漏洞。。。所以在这里,我试图添加我的“玩家火箭”,这是我刚刚在书中添加的所有生成小行星下的代码

    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();
现在这个错误我一直得到它

未捕获类型错误:无法设置未定义的属性“vX”

我翻阅了这本书,想知道我是否必须在某个地方定义它,但我没有遗漏任何东西。现在我对这一点还是很陌生,但我看到我使用
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;
};

如果有人能帮助我,更好地理解这一点,那就太好了!谢谢你

当你说
var-player时,您正在将
player
设置为未定义。您需要创建
Player()
的新实例。你可以这样做

var player=new Player(xCoord,yCoord);
这本书显然是错误的,如果它认为启动与构造函数名称相似的东西将实例化一个新的玩家


要了解更多关于对象的信息,您应该看看您在哪里创建了Player类的实例??如果该代码来自一本书,请将该书扔出您的窗口并找到另一本。您能将其张贴在fiddle上,以便我们可以直接看到所有这些画布的错误吗?thanks@Misters你所说的玩家等级是什么意思?@ChristophHa只是它以前从未使用过fiddle,所以请让我知道这个链接是否有效。你可以在fiddle上查看一下完整的代码。我只是不知道我会如何设置你刚才告诉我的。