Jump to content

Extend DisplayObjectContainer


foofel
 Share

Recommended Posts

Hey,

 

i am pretty new to javascript/pixi and am not sure what i am doing wrong. I try to extend a DisplayObjectContainer (like i would to it in flash) and add stuff to it. The code looks like this:

function Floor(world){	function init()	{		createGraphics();	}	function createGraphics()	{		var graphics = new PIXI.Graphics();		graphics.beginFill(0xFFFF00);		graphics.drawCircle(0, 0, 50);		graphics.endFill();		this.addChild(graphics); // <- this throws	}	init();}Floor.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);Floor.prototype.constructor = Floor;

but it look like my clas is not inheriting the DisplayObjectContainer functions, this.addChild() thwos an undefined error. :(. What am i missing?

Link to comment
Share on other sites

hmm doens't seem to work, direct c&p:

function Floor(){	PIXI.DisplayObjectContainer.call( this );	function init()	{		createGraphics();	}	function createGraphics()	{		var graphics = new PIXI.Graphics();		graphics.beginFill(0xFFFF00);		graphics.drawCircle(0, 0, 50);		graphics.endFill();		this.addChild(graphics);	}	init();}Floor.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);Floor.prototype.constructor = Floor;

results in an "Uncaught TypeError: undefined is not a function", still on the addChild line.

Link to comment
Share on other sites

function Floor()

{

PIXI.DisplayObjectContainer.call( this );

this.init();

}

Floor.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);

Floor.prototype.constructor = Floor;

Floor.prototype.init = function() {

this.createGraphics();

}

Floor.prototype.createGraphics = function() {

var graphics = new PIXI.Graphics();

graphics.beginFill(0xFFFF00);

graphics.drawCircle(0, 0, 50);

graphics.endFill();

this.addChild(graphics);

}

Link to comment
Share on other sites

Or alternatively

function Floor(){	PIXI.DisplayObjectContainer.call( this );	this.init = function ()	{		this.createGraphics();	}	this.createGraphics = function ()	{		var graphics = new PIXI.Graphics();		graphics.beginFill(0xFFFF00);		graphics.drawCircle(0, 0, 50);		graphics.endFill();		this.addChild(graphics);	}	this.init();}Floor.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);Floor.prototype.constructor = Floor;

You basically had a scope problem there. Inside your createGraphics function the keyword 'this' referred to the createGraphics function itself, not the Floor object. You can avoid this issue by making the createGraphics function a property of Floor or its prototype as shown above. When set up that way the keyword 'this' points to the object that owns the createGtaphics method.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...