0

I have a custom object called pipe

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

When I try storing pipes in an array its not storing the object, but the function.

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);
}

If I try accessing a property of a pipe in pipes it says undefined.

var pipe = pipes[0];
console.log(pipe.x);
1
  • pipe will return function expression(body). Have you defined var pipes = [] anywhere ? Commented Mar 2, 2016 at 5:13

1 Answer 1

2

You put pipes.push(pipe) instead of pipes.push(p).

Sign up to request clarification or add additional context in comments.

1 Comment

This would be the culprit. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.