Forum Topic: Interacting with enemies

(124 views • 11 replies)

This topic is 1 page long.

<< < > >>
None

Rudder21

Reply To Post Reply & Quote

Posted at: 6/29/09 09:14 PM

Rudder21 LIGHT LEVEL 03

Sign-Up: 11/27/08

Posts: 31

So the problem I have right now is I'm using some code I was taught by a friend of mine to generate enemies. How its set up is that that enemy is put into a movieclip and then a new variable is made to be a display object of each child of that movieclip. So that way I only have to tell all of them to do something once. Unfortunately when I want only one enemy to do something, they all do it. How can I make the one enemy i'm influencing be the only one to do something, when the only way I can reference the enemies references them all?


None

Archon68

Reply To Post Reply & Quote

Posted at: 6/29/09 09:19 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,374

At 6/29/09 09:14 PM, Rudder21 wrote: So the problem I have right now is I'm using some code I was taught by a friend of mine to generate enemies. How its set up is that that enemy is put into a movieclip and then a new variable is made to be a display object of each child of that movieclip. So that way I only have to tell all of them to do something once. Unfortunately when I want only one enemy to do something, they all do it. How can I make the one enemy i'm influencing be the only one to do something, when the only way I can reference the enemies references them all?

What do you mean by "referencing them all"? If you're giving them separate instance names, like you should be doing, then you can access each of them individually.

I code in AS2, in case I forgot to mention it.

BBS Signature

None

Rudder21

Reply To Post Reply & Quote

Posted at: 6/29/09 09:30 PM

Rudder21 LIGHT LEVEL 03

Sign-Up: 11/27/08

Posts: 31

I have around ten enemies on the screen at a given time, and new ones will pop up within seconds of the last one. This is what I want, and making a new instance name for everyone of them. and having to write those conditions for each one would be a huge huge HUGE time consumer


None

Woadraiders

Reply To Post Reply & Quote

Posted at: 6/29/09 09:31 PM

Woadraiders DARK LEVEL 11

Sign-Up: 11/11/07

Posts: 561

Show me the enemy creation code.

Mibbygames.com
panterA

BBS Signature

None

Rudder21

Reply To Post Reply & Quote

Posted at: 6/29/09 09:40 PM

Rudder21 LIGHT LEVEL 03

Sign-Up: 11/27/08

Posts: 31

var enemy: Enemy; /*enemy is my animation*/
addEventListener(TimerEvent.Timer,genera teEnemies);
addEventListener(Event.ENTER_FRAME, updateEnemies);
function generateEnemies(e:TimerEvent): void {
enemy = new EnemyChuters;
enemy.x = Math.round(Math.random()*550);
enemy.y = 550;
enemyClip.addChild(enemy);
}
}
function updateEnemies(e:Event): void {
for (var i: int = 0; i < enemyClip.numChildren; i++) {
var E:DisplayObject = enemyClip.getChildAt(i);
/*So if I want my enemies to all do the same thing, i tell E to do it e.g.
if I wanted them all to move foward id say E.x++;*/
}
// end code
Its good for making a bunch of them appear on the screen at random or at select times. the problem
is interacting with only one of them


None

Woadraiders

Reply To Post Reply & Quote

Posted at: 6/29/09 10:09 PM

Woadraiders DARK LEVEL 11

Sign-Up: 11/11/07

Posts: 561

When you make an enemy, add it to an array. When you want to do something to a specific enemy, find it in the array.

Mibbygames.com
panterA

BBS Signature

None

polym

Reply To Post Reply & Quote

Posted at: 6/29/09 10:20 PM

polym LIGHT LEVEL 10

Sign-Up: 10/02/07

Posts: 801

Both of you who posted are fucking retards

You need to add an event listener for the movie clips as they're created

That way, you can handle it using a single function that will account for each movie clip


None

Calipe

Reply To Post Reply & Quote

Posted at: 6/29/09 11:31 PM

Calipe LIGHT LEVEL 30

Sign-Up: 02/28/04

Posts: 448

At 6/29/09 10:20 PM, polym wrote: Both of you who posted are fucking retards

You need to add an event listener for the movie clips as they're created

That way, you can handle it using a single function that will account for each movie clip

What's wrong with you? Please watch your language.
Don't be so quick to judge people's intellectuality based on your criteria of how things should be done. It would be perfectly acceptable to push the instances into an array for later retrieval.
What made you think your way is the only way?


None

Archon68

Reply To Post Reply & Quote

Posted at: 6/30/09 07:43 AM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,374

At 6/29/09 10:20 PM, polym wrote: Both of you who posted are fucking retards

Don't be so quick to judge.

At 6/29/09 09:14 PM, Rudder21 wrote: Unfortunately when I want only one enemy to do something, they all do it.

From his post that sounds like exactly what he wants. Then, he could just use a for loop to access them all.

I code in AS2, in case I forgot to mention it.

BBS Signature

None

henke37

Reply To Post Reply & Quote

Posted at: 6/30/09 08:04 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,612

As far as I can see, he already have a loop to iterate on all enemies.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

Rudder21

Reply To Post Reply & Quote

Posted at: 6/30/09 08:08 AM

Rudder21 LIGHT LEVEL 03

Sign-Up: 11/27/08

Posts: 31

Would any1 mind showing me what the code might look like for the array method? I just need a visual of how it would look.


None

XBigTK13X

Reply To Post Reply & Quote

Posted at: 6/30/09 09:42 PM

XBigTK13X LIGHT LEVEL 28

Sign-Up: 08/04/05

Posts: 370

How you do this depends largely on what you are trying to accomplish. If it is something like shooting at a target, then you can use a distance conditional statement to dictate when the enemies fire. For example:

var dist:Number = Math.sqrt(Math.pow(Object1.x-Enemy.x, 2)+Math.pow(Object1.y-Enemy.y, 2)); //Uses the distance formula to calc distance
if(dist<50){
     //do stuff here
}

All times are Eastern Standard Time (GMT -5) | Current Time: 11:08 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!