0

Ok so here's my code;

function stdTile(gameObjectargs)
{   
    gameObjectargs.parent=this;
    this.gameObject=new gameObject(gameObjectargs);
}

Basically all I wanna do is pass 'This' to gameObject, so I can refer back to the stdTile when I use new stdTile({}); but for some reason every time I call it its sets gameObject.parent to window any help?

::EDIT::

I dont understand why my code didnt appear but I made a fiddle http://jsfiddle.net/Wjmta/

5
  • your question is unclear, please include a fiddle. Commented Sep 15, 2013 at 5:05
  • im not sure what you trying to do, but 1 this is a keyword 2 functions cannot create variable's to be used outside of them, i cantt see your code and dont understand your question, but this may help. Commented Sep 15, 2013 at 5:08
  • Could you explain the way you're trying to access the parent property after instanciation? A code sample would be useful. Commented Sep 15, 2013 at 5:24
  • just typing my new stdTile object into the console then navigating into the gameObject property, and then to the parent property which is equal to window Commented Sep 15, 2013 at 5:28
  • The only way I can reproduce that behavior is omitting the new keyword when attempting to create the stdTile instance. Commented Sep 15, 2013 at 5:35

1 Answer 1

1

Your code should work as expected; here's an example:

function gameObject(args)
{
    this.parent = args.parent;
    this.args = args;
}

function stdTile(gameObjectargs)
{   
    gameObjectargs.parent=this;
    this.gameObject=new gameObject(gameObjectargs);
}

var tile = new stdTile({});
console.log(tile.gameObject.parent === tile); // true
Sign up to request clarification or add additional context in comments.

3 Comments

I got the same result without changing anything in the original code. I guess the new keyword is missing somewhere... Furthermore, why this variable property (obj.parent.variable)?
Oh wow, turns out everything was fine except in function gameObject I had this.parent=parent instead of this.parent=args.parent I had no idea parent returned the window object, or is it a keyword? anyways thanks for your help everyone!:)
obj.parent.variable was just an easy way of shortening down my long code Its actually a collision detection system for a game I'm working on, the gameObjects are sorted through to find which ones collide with certain areas then they're stored in an array and there needs to be a way to tell which object they belong to:)

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.