0
votes

I'm creating a game in Flash CS4 and using Actionscript 3.0.

I'm adding all my graphics via my AS3 code. I've added my hero (A movie clip instance) in the main game class. In my enemy class (the movieclip) I want to make a generic AI which will check to see how close the hero is to the enemy. I tried various ways but I'm not sure how to properly access the X position of my hero from the code in my enemy movie clip.

I tried accessing it using thing such as: hero.x, GAMENAME.hero.x and parent.hero.x but keep getting errors. I'm not too sure what I need to do here.


My question was: "How do I access my main class that is built when flash makes my project". I still don't know how but I did create a basic enemy AI the same way you would think but instead of controlling it all via the enemy class I had to send values to methods in the enemy class via my main class that was created with my project.

1
AI sure seems like very complex subject to start learning with. You should put more info about the structure of your project, because right now what you have is impossible to guess.sharvey
Can you show us some code sample on how and where you are adding your hero and enemies? If have added hero to the stage, you should be able to get it's position with hero.x and hero.y.David

1 Answers

0
votes

You could check the distance from your hero's x position to his/her enemy.x position.

Try something like this...

var dx:Number = item.x-item2.x;
var dy:Number = item.y-item2.y;

if (Math.sqrt(dx*dx+dy*dy) < what_ever_distance) {
   //do something
}