Be a Supporter!

Child Class Constructor Not Called

  • 379 Views
  • 6 Replies
New Topic Respond to this Topic
mikeMarek
mikeMarek
  • Member since: Jan. 24, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Child Class Constructor Not Called May. 26th, 2012 @ 09:10 PM Reply

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
caseymacneil
caseymacneil
  • Member since: Nov. 19, 2008
  • Offline.
Forum Stats
Member
Level 06
Programmer
Response to Child Class Constructor Not Called May. 26th, 2012 @ 10:24 PM Reply

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()
Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to Child Class Constructor Not Called May. 27th, 2012 @ 04:37 AM Reply

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.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Child Class Constructor Not Called May. 27th, 2012 @ 06:18 AM Reply

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

mikeMarek
mikeMarek
  • Member since: Jan. 24, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Child Class Constructor Not Called May. 27th, 2012 @ 01:09 PM Reply

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
mikeMarek
mikeMarek
  • Member since: Jan. 24, 2008
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Child Class Constructor Not Called May. 27th, 2012 @ 02:07 PM Reply

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
Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to Child Class Constructor Not Called May. 27th, 2012 @ 04:42 PM Reply

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..