Be a Supporter!

reference an MC in different class

  • 293 Views
  • 5 Replies
New Topic Respond to this Topic
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
reference an MC in different class 2012-05-12 01:06:12 Reply

(have I asked this before?)

Okay, so my document class places my bullets and my enemy on the stage with addChild(). Easy enough.

I want my bullet to remove itself if it hits the enemy and I think I want this code to be placed in my Bullet class.

The problem is how the HECK do I reference the enemy child from the Bullet class? The Bullet class didn't create the enemy, so how would it be able to recognize it?

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to reference an MC in different class 2012-05-12 01:22:12 Reply

You don'tn tell the parent class to do all this junk.

if (_bullet.hitTestObject(_enemy))
{
_bullet.removeSelf();
_enemy.removeSelf();
}

Child objects shouldn't be able to access parent objects, it's a general rule of programming structure.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to reference an MC in different class 2012-05-12 11:37:24 Reply

At 5/12/12 01:22 AM, MintPaw wrote: You don'tn tell the parent class to do all this junk.

if (_bullet.hitTestObject(_enemy))
{
_bullet.removeSelf();
_enemy.removeSelf();
}

Child objects shouldn't be able to access parent objects, it's a general rule of programming structure.

By that do you mean that adding the objects to the stage (the player and bullets) should be done in their own classes?

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to reference an MC in different class 2012-05-12 12:05:30 Reply

No, not quite.

var _bullet:Bullet = new Bullet();
var _enemy:Enemy = new Enemy();
addChild(_bullet);
addChild(_enemy);
addEventListener(Event.ENTER_FRAME, update);

function update(e: event):void
{
if (_bullet.hitTestObject(_enemy)
{
//Et cetera
}
}

As a quick example.

Also, you shouldn't be adding thing to the stage, it's kinda hard to manage, you can actually add things to the doc class by just using "addChild()" instead of "stage.addChild()"
Honestly you should probably go deeper and add things to another MovieClip within the game, just to keep everything in one manageable place.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to reference an MC in different class 2012-05-12 12:39:09 Reply

At 5/12/12 12:05 PM, MintPaw wrote: No, not quite.

var _bullet:Bullet = new Bullet();
var _enemy:Enemy = new Enemy();
addChild(_bullet);
addChild(_enemy);
addEventListener(Event.ENTER_FRAME, update);

function update(e: event):void
{
if (_bullet.hitTestObject(_enemy)
{
//Et cetera
}
}

As a quick example.

Also, you shouldn't be adding thing to the stage, it's kinda hard to manage, you can actually add things to the doc class by just using "addChild()" instead of "stage.addChild()"
Honestly you should probably go deeper and add things to another MovieClip within the game, just to keep everything in one manageable place.

Wait, okay, so I addChild them from the document class(not from the stage), I give them their attributes from their own classes (speed, positioning, etc..), and then I code their interactions from the document class?

Am I finally getting it right?

And don't add them to the stage.. Okay.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to reference an MC in different class 2012-05-12 15:59:51 Reply

At 5/12/12 12:39 PM, Barzona wrote: Am I finally getting it right?

Yes, and there's not really a big disadvantage to adding things to the stage, there's just advantages to adding things in deeper DisplayObjects, most would argue that it's better to have a container in the doc class to house your game. Here's some examples.

_gameContainer = new Sprite();
addChild(_gameContainer) // _gameContainer is a Sprite in the doc class
_enemy1 = new Enemy();
_enemy2 = new Enemy();
_enemy3 = new Enemy();
_enemy4 = new Enemy();

_gameContainer.addChild(_enemy1);
_gameContainer.addChild(_enemy2);
_gameContainer.addChild(_enemy3);
_gameContainer.addChild(_enemy4); // The enemies all in the game container.

_gameContainer.rotation = 180; // This will turn the game upside-down without effecting other elements such as the HUD.

removeChild(_gameContainer);
_gameContainer = new Sprite();
addChild(_gameContainer); // All the enemies were also removed. This resets the game as long as you remove all the events that may be tied to the enemies.

If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature