0

I cannot get this to work:

var global_variables = {
    players: []
}; 

var player = function(properties){
    this.width = properties.width;                            
    this.height = properties.height;
    this.position = properties.position;
    this.position.x = properties.position.x;
    this.position.y = properties.position.y;
}

function load_players(){
    global_variables.players.push(
        [
            new player(
                {
                    width: 10,
                    height: 10,
                    position: {
                        x: 50,
                        y: 50
                    },
                    colour: '#ff0000'
                }
            )
        ]
    );
}


 function init(){ 
    var ctx = load_canvas();
    load_players();

    for(a in global_variables.players){
        var _this = global_variables.players[a];
        alert(_this.colour);
    }
}

alert(_this.colour) just alerts undefined. Any ideas?

2
  • load_players was not called? Commented Sep 17, 2012 at 21:32
  • I updated the code to represent where i would call it... And again. hehe Commented Sep 17, 2012 at 21:33

3 Answers 3

2
  1. You were trying to loop before calling init
  2. You were pushing your player instance wrapped in an array. You just wanted to push the player.
  3. Don't use for ... in ... on an array. Use a regular loop or use forEach like I did.
  4. You were never actually setting the colour property to your instance.

http://jsfiddle.net/GLsR2/

Here's the code from the fiddle:

var global_variables = {
    players: []
}; 

var player = function(properties){
    this.width = properties.width;                            
    this.height = properties.height;
    this.position = properties.position;
    this.position.x = properties.position.x;
    this.position.y = properties.position.y;
    this.colour = properties.colour;
}

function load_players(){
    global_variables.players.push(
        new player(
                {
                    width: 10,
                    height: 10,
                    position: {
                        x: 50,
                        y: 50
                    },
                    colour: '#ff0000'
                }
            )
      );
}


init();

global_variables.players.forEach(function(player) {
   alert(player.colour); 
});

function init(){ 
    load_players();
}
Sign up to request clarification or add additional context in comments.

Comments

2

You get undefined from _this.colour for two reasons:

  1. You don't set a colour property in your constructor
  2. Rather than pushing each new player into the array you are pushing a one-element array containing the new player. That is, you're creating an array of arrays.

Add this to the constructor:

    this.colour = properties.colour;

And then remove the square brackets from in the load_players() function:

function load_players(){
    global_variables.players.push(        
            new player({
                    width: 10,
                    height: 10,
                    position: {
                        x: 50,
                        y: 50
                    },
                    colour: '#ff0000'
            })
    );
}

Demo: http://jsfiddle.net/ZACnC/

2 Comments

Oh god.. the colour not being set.. how embarrassing.. thank you so much for taking the time to out my noobish school boy error of being a noob. toolateforcode.com
I changed it to: global_variables.players = [ new player(... Thanks once again
0

When you are pushing on to global_variables.players, you are pushing an array containing the new Player object, not the object itself.

You don't need the [] when using .push.

function load_players(){
    global_variables.players.push(
        new player({
            width: 10,
            height: 10,
            position: {
                x: 50,
                y: 50
            },
            colour: '#ff0000'
        })
    );
}

P.S. Don't use for...in for arrays. Just use a normal for.

for(var a = 0, len = global_variables.players.length; a < len; a++){
    var _this = global_variables.players[a];
    alert(_this.colour);
}

P.P.S. You need to add this.colour = properties.colour; to your player constructor.

Comments

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.