I need a help with an AI problem

  • ibrabdo
  • Novice
  • Novice
  • User avatar
  • Posts: 29

Post 3+ Months Ago

I'm a cartoon designer and animator and lately I learn how to use the actionscript2 (unfortunately everyone is using actionscript3 now).

I'm making a flash game using actionscript2 about a guy who's running away from another guy (the player control the first one); I made the basic physics by doing the gravity and making the player {instance name: Man} jumps on objects, and about his movement on the X axis I made him stand still in the middle of the screen and the VelX of the player makes the background, the items and the collisions moving:

Code: [ Select ]
if(Key.isDown(Key.LEFT)){ VelX = -15; }
if(Key.isDown(Key.RIGHT)){ VelX =15; }
  1. if(Key.isDown(Key.LEFT)){ VelX = -15; }
  2. if(Key.isDown(Key.RIGHT)){ VelX =15; }


Then I made the items and the objects create randomly.
And after that I started to do the guy who follows the player {instance name: BadMan} by giving him the VelX of the player to keep his position in the background (VelX1) plus his velocity (VelX2) multiplied the direction to let him follow the player (dir)(dir is 1 or -1):

Code: [ Select ]
VelX1 = - _root.Man.VelX;
VelX2 = 5 * dir;
PosX += (VelX1 + VelX2);
  1. VelX1 = - _root.Man.VelX;
  2. VelX2 = 5 * dir;
  3. PosX += (VelX1 + VelX2);


With that I made the badman follow the player on the X axis.
And in the end I started to face the big problem: how the badman gonna follow the player when he jumps (Y axis), knowing that the objects are on many levels and they created randomly without a stable _X (because it’s moving by the VelX of the player when we changing his direction).
So I thought that I have made sequence collisions creates one by one each time the player jumps (key1, key2, key3, …) and let the Badman ignore the player and follow these collisions when its exist and when he hit its jump with the same velocity, acceleration, and force of the player (the same condition of the player) and when Badman finish his moves (moving from a collision to another) he can continue following the player:

Code: [ Select ]
if( _parent.key1._x > _parent.Badman._x        //to check the position of Badman from key1
&& d1 == 1        // to select witch key the bad man have to go to
&& _parent.key1._y > 0        // to check if key1 is exist
|| _parent.key2._x > _parent.Badman._x && d2 == 1 && _parent.key2._y > 0
||_parent.key3._x > …. [Ext]){
dir = 1 ;        // the direction of Badman to reach the locat key}
// and dir= -1 if _parent.key1._x < _parent.Badman._x
 
if( _parent.key1.hitTest(_parent.Badman.dam)){        // when Badman hit key1
_parent.Badman.VelY = _parent.Man.VelY;        // Badman take the jump force of Man
_parent.Badman.VelX2 = 15 * dir;        // Badman take the velocity of Man
d1 = 0; d2 = 1;        // make Badman change his direction to the next key (key2)
_parent.key1._y = -200 ;}        // make this key out of reach until the player call it again
 
if( _parent.key2.hitTest( ….[Ext]
  1. if( _parent.key1._x > _parent.Badman._x        //to check the position of Badman from key1
  2. && d1 == 1        // to select witch key the bad man have to go to
  3. && _parent.key1._y > 0        // to check if key1 is exist
  4. || _parent.key2._x > _parent.Badman._x && d2 == 1 && _parent.key2._y > 0
  5. ||_parent.key3._x > …. [Ext]){
  6. dir = 1 ;        // the direction of Badman to reach the locat key}
  7. // and dir= -1 if _parent.key1._x < _parent.Badman._x
  8.  
  9. if( _parent.key1.hitTest(_parent.Badman.dam)){        // when Badman hit key1
  10. _parent.Badman.VelY = _parent.Man.VelY;        // Badman take the jump force of Man
  11. _parent.Badman.VelX2 = 15 * dir;        // Badman take the velocity of Man
  12. d1 = 0; d2 = 1;        // make Badman change his direction to the next key (key2)
  13. _parent.key1._y = -200 ;}        // make this key out of reach until the player call it again
  14.  
  15. if( _parent.key2.hitTest( ….[Ext]


The code is working, but I thought if I made that the Badman will take the same moves of the player and he can reach him when he jumps (sometime its work), but I faced a many bugs and problem like sometime the Badman jumps in the wrong direction (he take dir after he jumps) or when sometimes the player and Badman have the same _Y on different levels so the Badman will stuck.

Some of my friends said (wisely) that I have to use a GRAPH or PATH FINDING SYSTEM but I didn't get it and I don't think they do.

I need some Ai in my code but I don't know how to do it, so PLEASE HELP…
And if anyone have an idea about what I have to do or want to check my code I will be glad to send him my .fla source on the email and I will promise to send him the final game when I finish it... and thx.
Moderator Remark: added code tags
  • ibrabdo
  • Novice
  • Novice
  • User avatar
  • Posts: 29

Post 3+ Months Ago

Can anyone give me an advice for what I have to do, so I can think in other solutions to solve this problem?

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 15 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.