00:00
00:00
Newgrounds Background Image Theme

UmanaJose 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!

reference an MC in different class

539 Views | 5 Replies
New Topic Respond to this Topic

(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?

Response to reference an MC in different class 2012-05-12 01:22:12


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

Response to reference an MC in different class 2012-05-12 11:37:24


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?

Response to reference an MC in different class 2012-05-12 12:05:30


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

Response to reference an MC in different class 2012-05-12 12:39:09


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.

Response to reference an MC in different class 2012-05-12 15:59:51


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