00:00
00:00
Newgrounds Background Image Theme

Neptuno84 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Child Class Constructor Not Called

728 Views | 6 Replies
New Topic Respond to this Topic

Hello, fellow Newgrounders. I was hoping someone would be able to help me with a current issue in my game development.

So, basically, I have two classes. NPC and Player. NPC is a generic class that holds functions that in-game players and enemies can both use, but for player and AI-controlled purposes, can allow me to make seperate classes. First, a little bit of code for example:

public class NPC extends MovieClip
{
	public function NPC():void
	{
		trace("NPC::NPC()");
		init();
	}
	public function init():void
	{
		trace("NPC::init()");
	}
	public function main():void
	{
	}
}
public class Player extends MovieClip
{
	public function Player():void
	{
		trace("Player::Player()");
		init();
	}
	override public function init():void
	{
		trace("Player::init()");
	}
	override public function main():void
	{
	}
}

A little bit of explanation. The init() function sets up any variables and settings for the class when it is first created, while the main() function is run every frame in a root EnterFrame listener. The Player class is supposed to override the init() and main() functions from the NPC class.

The trace() functions are there just to demonstrate what scope and what function is being called. My main "Player" movieclip is linked to the Player class and sub-class NPC. The player is created as an object in my Document class and is handled there as such.

Anyways, my problem is that the NPC (parent) class constructor is being called, which then calls the init() function. The Player (child) class constructor is not called at all upon declaration. Any thoughts?

tl;dr child class constructor isn't called but the parent class constructor is. I just gave some code examples and explanations for further insight.

I really do appreciate any help or comments you guys can leave. Thanks for anything/everything!
Mike


BBS Signature

Response to Child Class Constructor Not Called 2012-05-26 22:24:34


are you initializing the variables?
ex:

not initialized

var player:Player
var randomNPC:NPC

initialized

var player:Player = new Player()
var randomNPC:NPC = new NPC()

Response to Child Class Constructor Not Called 2012-05-27 04:37:06


At 5/26/12 09:10 PM, mikeMarek wrote: The Player (child) class constructor is not called at all upon declaration. Any thoughts?

Constructors are called upon initialization, not declaration.

Response to Child Class Constructor Not Called 2012-05-27 06:18:52


Being a bit pedantic, but isn't it called instantiation when dealing with classes?

Response to Child Class Constructor Not Called 2012-05-27 13:09:18


At 5/26/12 10:24 PM, caseymacneil wrote: are you initializing the variables?

Yeah I'm initializing them. I just cut back most of those classes since they're rearing on a couple hundred lines a piece now :P

The problem is, when I initialize the Player class, the NPC constructor gets called (naturally, since it IS the parent class to Player), yet the trace() statement I had placed in the Player constructor does not get called. I'm thinking it has something to do with how I linked it in the library. I put the movieclip's linked class as Player, and it's linked sub-class as NPC, not the usual flash.display.MovieClip.

Argh... The subtleties of AS3 OOP :P


BBS Signature

Response to Child Class Constructor Not Called 2012-05-27 14:07:24


Wow. Am I dumb or what?

So, I guess we could mark this thread as RESOLVED.

The problem was within the linkage. I have my main game engine folder I am working on, for now contained in folder 'engine,' and all user-created classes go into 'instance'.

I was setting the movieclip linkage as "engine.Player" and not "instance.Player" so Flash automatically created a class for me in the 'engine' folder that was empty, instead of wanting to point to the actual class.

It's silly mistakes like these that hold me back. Just like in math, I'd always forget a plus/minus sign somewhere.... LOL

Cheers for everyone's replies though!
Mike


BBS Signature

Response to Child Class Constructor Not Called 2012-05-27 16:42:28


At 5/27/12 06:18 AM, Sam wrote: Being a bit pedantic, but isn't it called instantiation when dealing with classes?

yeah, yes it is. All this talk about initialization is putting me on the wrong track.
Man those words are really alike to me..