2016-03-02 99 views
0

我有称为管道存储对象被存储功能

function pipe() 
{ 
    this.x = 800; 
    this.width = 50; 
    this.y = 450; 
} 

自定义对象当试图在数组中它不是存储对象,但功能存储管道。

function loop() 
{ 
    var flappy = new player(); 
    for(var i = 0; i < 3; ++i) 
    { 
     var p = new pipe(); 
     p.x = (1 + i) * 266; 
     pipes.push(pipe); 
    } 
    interval = window.setInterval(function() { updateLogic(flappy) } , 20); 
} 

如果我尝试访问管道中管道的属性,它说未定义。

var pipe = pipes[0]; 
console.log(pipe.x); 
+0

'pipe'将返回函数表达式(body)。你有没有在任何地方定义'var pipes = []'? – Rayon

回答

2

你把pipes.push(pipe)而不是pipes.push(p)

+0

这将是罪魁祸首。谢谢 – Programatic