0

I'm getting my feet wet with AS3, Flixel and component/entity systems (yes, all at the same time), and my entities (which subclass FlxSprite) aren't being added correctly (i.e., not at all). I've just spent a good two hours nailing down the offending line. If I remove it, the rest of the game chugs along happily.

What's wrong with this code?

public function addComponent(c:Component):void                                       
{                                                                                    
  var type:String = Object(c).constructor.toString();                                
  FlxG.log("type=" + type);                                                          
  this._components[type] = c; // The evil line                                       
  FlxG.log("now type=" + _components[type]);                                         
  c.setData(this);                                                                   
}                                                                                    

components is an Object field being used as a map/dictionary. type gets set to [class PlayerComponent]. Based on googling, this is valid and should work as intended.

Based on the output from the console, it's just bailing after that line--not crashing entirely. What's going on? More details gladly offered upon request.

Cheezmeister
  • 4,895
  • 3
  • 31
  • 37
  • 1
    Make sure you are using the debug Flash player. Without it, exceptions (crashes) occur silently. – Sunil D. Dec 26 '12 at 07:03
  • that's the 1st time I see this way to get the object's class name ( I mean the constructor.toString() method). I preferred using Class(getDefinitionByName(getQualifiedClassName(obj))); – yannicuLar Jan 02 '13 at 08:15

1 Answers1

0

I'm not certain about Component - not my forte - but I do know that FlxGroup and its children (which include FlxState) have a method called add() which adds children to them.

So if you have an FlxSprite, the correct way (in flixel) to add it to the chain of things to update/draw is to use that; you can add it directly to your state or to a group that is a child of the state.

Function docs: http://flixel.org/docs/org/flixel/FlxGroup.html#add()

That Guy
  • 103
  • 1
  • 8
  • I should point out that this is useful information but may not be useful to what you're experiencing, a side effect of not grasping `Component` strongly. – That Guy Aug 01 '15 at 00:05