-
-
Intermediate Game Dev
Just check where the hero's _x and _y is, and then move untill the _x and _y are equal. Won't take too long to do with a bit of thinking.
-
Is there a code that I could get? I'm not very smart on figuring out those things yet. I'm only 13
-
Don't worry, I'm 15! I can help!
This is one way to do it:
For each zombie you could put the code in their enterFrame thing:
Code:
onClipEvent(enterFrame) //or "onEnterFrame=function()" depending on where you put the code
{
this.xdist=_root.hero._x-this._x; //the distance between the _x positions of the two
this.ydist=this._y-_root.hero._y; //the distance between the _y positions of the two
this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist)); //total distance between the two using Pythagorean Theorem
this.xadd=this.totaldist/this.xdist; //number to add to the _x of the zombie
this.yadd=this.totaldist/this.ydist; //number to add to the _y of the zombie
this._x+=this.xadd; //add it to the _x
this._y+=this.yadd; //add it to the _y
}
You need to replace "hero" with the name of the hero or character in your game.
Other than trigonometry, I think that's the only way to make them move at a constant speed towards the enemy.
Btw- I would strongly recommend that you create the zombies in code rather than on the stage, however, which is where the side note "onEnterFrame=function()" comes from. Use a "for" loop in the main timeline to create the zombies.
VERY IMPORTANT: In order for this second method to work, you need to find the zombie movieClip in the library, right click on it, choose linkage, and give it an identifyer of "zombie", for use in code
This is how it would be done (in the main timeline code):
Code:
for(i=1; i<=number; i++) //replace "number" with the number of zombies you wish to create
{
thezombie=attachMovie("zombie","zombie"+i,_root.getNextHighestDepth(),{_x:Math.random()*550,_y:Math.random()*400}); //creates the zombie(s) and sends them to a random position on the stage
thezombie.onEnterFrame=function()
{
this.xdist=_root.hero._x-this._x; //the distance between the _x positions of the two
this.ydist=this._y-_root.hero._y; //the distance between the _y positions of the two
this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist)); //total distance between the two using Pythagorean Theorem
this.xadd=this.totaldist/this.xdist; //number to add to the _x of the zombie
this.yadd=this.totaldist/this.ydist; //number to add to the _y of the zombie
this._x+=this.xadd; //add it to the _x
this._y+=this.yadd; //add it to the _y
}
}
Voila.
-
Yay! Go little kids! But then the code that I'm trying which you just provided above... I'm not sure how it was supposed to work but I followed your instructions but it didn't work for me
-
Intermediate Game Dev
^And what didn't EXACTLY work?
-
Well what didn't work well for me was that the considered... ZOmbiE is multipied by 400 or 550 covering everything in the screenie!
-
Intermediate Game Dev
well, just sete the variable
"number"
to something like 10.
-
In the line of code where is says:
Code:
this.totaldist=Math.sqrt((xdist*xdist)+(ydist*ydist));
there should be a "this." before every xdist and ydist, my bad.
-
Okay it works now Thanks everyone
-
I just played around with the codes so it worked!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|