-
I'm working on the code for the enemies of my game
I just can't get this code to work
It's fairly simple:
Find out hero's x location as xspot
move to xspot - when enemy x = xspot
get next xspot
here's the code
Code:
onClipEvent (load) {
// find where hero is
xspot = _root.hero._x;
}
onClipEvent (enterFrame) {
if(this._x < xspot) { // move right
this._x += 2;
}
if(this._x > xspot) { // move left
this._x -= 2;
}
if(this._x == xspot) { // arrived at xspot - get new xspot
xspot = _root.hero._x;
}
}
This is just the first part, but I just can't
figure it out. Once the MC gets to xspot it stays there.
It's not going to the next xspot.
I did a trace on xspot and the value never changes.
Anyone know what I'm doing wrong?
Thanks
-
SaphuA
:)
the onload event only works when you load the enemy MC ...
So it only track's the hero's position once and then he doesn't anymore... unless you change it into the enterFrame even't...
Code:
onClipEvent (enterFrame) {
xspot = _root.hero._x;
if(this._x < xspot) { // move right
this._x += 2;
}
if(this._x > xspot) { // move left
this._x -= 2;
}
if(this._x == xspot) { // arrived at xspot - get new xspot
xspot = _root.hero._x;
}
}
-
Yeah, but i don't want to track the hero's movement
I want to get the x position - store it and move the MC to it
when the MC gets to xspot - THEN it picks a NEW xspot
the problem with the code seems to be with this line
Code:
if(this._x == xspot) { // arrived at xspot - get new xspot
xspot = _root.hero._x;
}
I want the enemy to be kind of dumb
this code should change the value of xspot when
the mc gets to xspot position
-
Zakarus's Tale Developer
lazypays is right the reason the value never changes is because it only checks it only gets the xpot once, whent he mc loads. You would need something like this...
Code:
onClipEvent(load) {
xspot = _root.hero._x;
}
onClipEvent(enterFrame) {
if(this._x < xspot) { // move right
this._x += 2;
}
if(this._x > xspot) { // move left
this._x -= 2;
}
if(this._x == xspot) { // arrived at xspot - get new xspot
xspot = _root.hero._x;
}
}
yeah that should work...
Have fun
~Zakarus2001
//////////////ADDITION//////////////////
I think another problem would be that if the xspot is something like 2.002 or something, the enemie will never get tot hat spot, because it only moves in integers of 2.. so maybe you should do something like...
Code:
if (this._x >= xspot-1 && this._x <= xspot+1) {
xspot = _root.hero._x;
}
that should fix it
-
Yeah, that worked perfectly
Thanks
joe
-
Who needs pants?
yeh
xspot = _root.hero._x;
needs to be under onClipEvent(enterFrame) {
because it needs to be refreshed every frame
-
When you know are.
How would you set the AI to activate if you are within a certain range of the enemy? Like 100 pixels or something.
-
Well the AI was for a shooting game I'm making and
I'm still working on it.
My idea was to get the x and y position of the hero MC
-Then find out which distance was FARTHER Away,
and then move the enemy MC to that specific position
(either x or y, but not both)
When the enemy MC reaches that specific X or Y
position, it picks a new position based on hero MC.
Since the enemy MC is shooting at you, he only has to line
up on one axis to get a clean shot. By moving him to
the farther x or y position he doesn't appear to move at
an angle straight at you.
does that make sense?
anyway here's a quick example of what I was working on
it's not working exactly right, but it's getting close
-use the arrow keys to move and space bar to shoot
http://www.lazypays.com/flash/sample_ai.html
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
|