Welcome Guest [Log In] [Register]
Welcome to Brackenwood. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Basic A.I
Topic Started: Feb 28 2006, 05:08 AM (405 Views)
clayt
Member Avatar
I'm not crazy... ask my toaster!
hey, im looking for some Basic A.I on a little game that im making. What i need is:

- the enemie will like patrol so he will be walking left to right.
- when the player gets close to the enemie the enemie will attack.
- if u move away he will stand still untill ur more close again and he will then attack.
- and if i attack he dies
- and i want all these to be sprites so it will be like 'gotoandPlay(die);' etc...

Thanks if anyone can help me out! :lol:

What i have so far on the Enemie is:

Code:
 
onClipEvent (enterFrame) {
if (_root.character._x>_x) {
_x += 1;
}
}
onClipEvent (enterFrame) {
if (_root.character._x<_x) {
_x -= 1;
}
}
onClipEvent (enterFrame) {
if (_root.character._y>_y) {
_y += 1;
}
}
onClipEvent (enterFrame) {
if (_root.character._y<_y) {
_y -= 1;
}
}
onClipEvent (load) {
if (_root.red.hitTest(_root.character)) {
gotoAndPlay("Munch");
}
}


Which is really crap to be honest lol...

And whats on the Player is:

Code:
 
onClipEvent(load){
stop();
xSpeed=3;
weapon="Ready";
Scale=100;
Height = _height;
jump = 0;
jumpheight = 13;
}
onClipEvent(enterFrame){
//Walk with weapon
//If Right key is down and the player is holding
//the weapon, move to the right, go to frame 2
//and set _xscale to the positive value of Scale
if(Key.isDown(Key.RIGHT)&&weapon=="Ready"){
 _x+=xSpeed
 _xscale=Scale
 gotoAndStop(2);
}
//Same but for left (Note : we add "else" so it
//won't work if both left and right keys are down)
else if(Key.isDown(Key.LEFT)&&weapon=="Ready"){
 _x-=xSpeed
 _xscale=-Scale
 gotoAndStop(2);
}
//Stop moving when player isn't holding the weapons
if(!Key.isDown(Key.RIGHT)&&!Key.isDown(Key.LEFT)&&weapon=="Ready"){
 gotoAndStop(1);
}
//If the blades are out (weapon = "Ready") and the
//hero is not moving, go to his Stand frame with
//the blades out
if(!Key.isDown(Key.RIGHT)&&!Key.isDown(Key.LEFT)&&weapon=="Ready"){
 gotoAndStop(1);
}
//Walk with the blades, same as normal walk but walk slowier
if(Key.isDown(Key.RIGHT)&&weapon=="Ready"){
 _x+=xSpeed/2
 _xscale=Scale
 gotoAndStop(2);
}
else if(Key.isDown(Key.LEFT)&&weapon=="Ready"){
 _x-=xSpeed/2
 _xscale=-Scale
 gotoAndStop(2);
}
//If weapons are ready and Control is down, attack
//and set "weapon" to "Attacking"
if(Key.isDown(Key.CONTROL)&&weapon=="Ready"){
 gotoAndStop(3);
 weapon="Attacking"
}
}


Thanks again if anyone can help me!
Offline Profile Quote Post Goto Top
 
crumb
Rufum Ru Sudily!
clayt
Feb 28 2006, 05:08 AM

onClipEvent (load) {
if (_root.red.hitTest(_root.character)) {
gotoAndPlay("Munch");

lol, why is my name in your script? :blink:
Offline Profile Quote Post Goto Top
 
bracks
Member Avatar
Brackenwood Lightweight
you could try a macromedia tutorial
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
Quote:
 
- the enemie will like patrol so he will be walking left to right.


ok most of the time in games like mario or those, an enemie has a patrol at a platform it means that he will go from left to right (as you pointed out) the best thing to do that is making transparent wall objects in wich if the enemie touches it he will change its direction. You can achieve that with something like this:

Code:
 
onClipEvent(load){
   speedx=3;
   side = 1; //1 for right 0 for left
}
onClipEvent(enterFrame){
  if (this.hitTest(TransparentWall)){
     if (side == 1)
        side = 0;
     else
        side = 1;
  }
  if (side == 1)
     this._x+=speedx;
  else
     this._x-=speedx;
}


so this way you just need to add some transparent walls and that will work, maybe you would want some flying enemies that goes up and down, you just need to change the _x propertie for _y and add your transparent walls :lol:

Quote:
 
- when the player gets close to the enemie the enemie will attack.


now the enemie needs to be checking the character position and if he is near then he will go towards him (I didnt thought of any other way of attack :lol: ). So we already have an onEnterFrame event so we can add our new code there

Code:
 

onClipEvent(enterFrame){
  if (this.hitTest(TransparentWall)){
     if (side == 1)
        side = 0;
     else
        side = 1;
  }
  if (_root.Character._x == (this._x - 9))
     side = 2;  //Now  2 for character at his left and 3 for character at his right
  else if (_root.Character._x == (this._x + 9))
     side = 3;
  if (side == 1)
     this._x+=speedx;
  else if (side == 0)
     this._x-=speedx;
  else if (side == 2)
     this._x-=speedx+1;
  else if (side==3)
     this._x+=speed+1;
}


so in this case the enemie will go towards the character until he finds a transparent wall, we are adding 1 to speedx cos we want it to look like he is running ;)

Quote:
 
- if u move away he will stand still untill ur more close again and he will then attack.


I was thinking that in most of the games the enemie keeps moving even if he doesnt sees the character, maybe you meant that he stands there a few seconds waiting if the character returns right? well it is best seen if he trys to catch you until he cant for example jump something (a transparent wall would be there) ;)

Quote:
 
- and if i attack he dies


well all the above code had to be at the enemie code, but now we want our character to attack, an action related to our character. I dont know if he shots or what he does so Ill assume that always that he attacks the enemie will die

Code:
 
onClipEvent(enterFrame){
//Walk with weapon
//If Right key is down and the player is holding
......
......
//If weapons are ready and Control is down, attack
//and set "weapon" to "Attacking"
if(Key.isDown(Key.CONTROL)&&weapon=="Ready"){
gotoAndStop(3);
weapon="Attacking";
_root.enemie.gotoAndPlay("dying");
}
}

and at enemie's frames we'll need to have a frame called dying and at the end of the dying animation (optional) we'll have our deleting enemmie code

Code:
 
unloadMovie(this);


In case he shots and that the enemmie can dodge or something reply it please :D

Quote:
 
- and i want all these to be sprites so it will be like 'gotoandPlay(die);' etc...


...done hahaha :P

maybe I didnt get something clear so please point it out if that happened
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
munch
Feb 28 2006, 07:38 PM
clayt
Feb 28 2006, 05:08 AM

onClipEvent (load) {
if (_root.red.hitTest(_root.character)) {
gotoAndPlay("Munch");

lol, why is my name in your script? :blink:

lol... because it was going to be where the enemie 'munches' on the character (eats him)... lol... and munch was the first thing that came into my mind lol.

Thank Smoscar... but oh god i hate AS lol... nothing ever works! :angry:

When the enemie is patroling he just moves the left becuase 'slide = 0;' when he hits the wall he just carrys on moving :(

And the attack i already had working but the enemie wont die if i dont code it to like i have put...

Code:
 
if (bullet.hitTest (enemie)){
  _root.enemie.gotoAndPlay('dying');


But it doesnt work, can you help me out please ;)

Cheers :D
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
Quote:
 
When the enemie is patroling he just moves the left becuase 'slide = 0;' when he hits the wall he just carrys on moving


and by that you mean that even if the enemie is chasing the character, he will immediatly return?? and that you want the enemie waits for him a few seconds?? :worry:

Quote:
 

if (bullet.hitTest (enemie)){
  _root.enemie.gotoAndPlay('dying');


try with _root.enemie.gotoAndPlay("dying"); and check your spelling in the frame name :lol:
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
smoscar_01
Mar 1 2006, 05:32 AM
and by that you mean that even if the enemie is chasing the character, he will immediatly return?? and that you want the enemie waits for him a few seconds?? :worry:

No... the enemie just patrols left to right then when i get near he attacks me lol... sorry if i wasnt clear... your questions was kinda confusing so ill just tell u what i need lol.


Quote:
 

if (bullet.hitTest (enemie)){
  _root.enemie.gotoAndPlay('dying');

try with  _root.enemie.gotoAndPlay("dying"); and check your spelling  :lol:


i did do it with '_root.enemie.gotoAndPlay("dying");' and it doesnt do any thing thats why i added 'if (bullet.hitTest (enemie)){' lol.... so i dunno what to do.

Can you still help me out? Or are you stuck like me?
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
are you writing this code inside a object?? I you are then try this

Code:
 
if (_root.bullet.hitTest (_root.enemie)){
 _root.enemie.gotoAndPlay('dying');
}


Quote:
 
No... the enemie just patrols left to right then when i get near he attacks me lol... sorry if i wasnt clear... your questions was kinda confusing so ill just tell u what i need lol.


this will work :P

<edit> no... wait...sorry
Code:
 
....
if (_root.Character._x >= (this._x - 9) && !_root.Character._x > this._x)
    side = 2;
 else if (_root.Character._x <= (this._x + 9) && !_root.Character._x < this._x)
    side = 3;
 ....
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
smoscar_01
Mar 1 2006, 05:59 AM
are you writing this code inside a object?? I you are then try this

Code:
 
if (_root.bullet.hitTest (_root.enemie)){
 _root.enemie.gotoAndPlay('dying');
}

Ok cheers, i will try it out and no im not putting the code inside and object.. its on a MovieClip.

Quote:
 

this will work :P

<edit> no... wait...sorry
Code:
 
....
if (_root.Character._x >= (this._x - 9))
    side = 2;
 else if (_root.Character._x <= (this._x + 9))
    side = 3;
 ....


Ok i will try that in a sec, but can you tell me what this part is doing:

Code:
 
(this._x - 9))
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
lol a MovieClip is an object pal (movieclips, graphics, buttons...) :lol:

Code:
 
(_root.Character._x >= (this._x - 9) && !_root.Character._x > this._x


what you are doing in the code above is get this interval

x>= enemies position - 9 and x<= enemie's position

so lets say that '#' is the enemie sprite and '$' is the character sprite

$ ---------#

if $ approaches the lines (that equals nine) then # sees him :lol:

you tell -9 because you want the enemie to attack you when you are at a distance of nine of him
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
Quote:
 
lol a MovieClip is an object pal (movieclips, graphics, buttons...) :P


hehe i know yeah... i thought you meant as in side the movieclip like on a frame thats inside the MC lol.. sorry :worry:.

Oh god... this is so frustrating... could you take alook at the file n see if you can tell me whats going wrong cuz i cant work it out thanks alot :D

Ive attached the file! No i havn't cuz i can't attach files lol.. do you have MSN or Email addresse and ill send it you?
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
yes men send it to smoscar.01@gmail.com Ill have to go dinner cos my brother is at town, but when I get home Ill send it to you or do you have msn??
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
ok cheers... yeah i have MSN my email is clayt_12345678910@hotmail.com add me if want to! :D

I have to go for a meeting for work in about 15 mins and i will be back about 11 o clock if you dont live in the UK that would be in 2 hours i will send you the file then.
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
ok Ill hurry then :lol:
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
hehe ok cool :lol:

Have a nice dinner! :P
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
ok I havnt received your email :worry:
Offline Profile Quote Post Goto Top
 
crumb
Rufum Ru Sudily!
clayt
Mar 1 2006, 04:20 AM

lol, why is my name in your script? :blink:

lol... because it was going to be where the enemie 'munches' on the character (eats him)... lol... and munch was the first thing that came into my mind lol.
[/QUOTE]
yes forgot it was a common word
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
smoscar_01
Mar 1 2006, 08:03 AM
ok I havnt received your email :worry:

I know sorry i had to dash didnt want to be late for my work meeting ya see lol :(

I will send it now!

Quote:
 
yes forgot it was a common word


Ha ha yes... indeed... its kind of a funny lil' word isnt it lol...he he :P
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
OK clayt Ive already sent it to your email :lol:
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
ok cheers... thanks alot... its working accept for the attack and for the enemie to attack lol
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
Its good that it helped you :lol:

the enemie attack choice is up to you ;)
Offline Profile Quote Post Goto Top
 
pez
Brackenwood Lightweight
this isn't an AS-related tip but thought i'd point it out anyway, it's spelt enemy ;)
Offline Profile Quote Post Goto Top
 
smoscar_01
Member Avatar
Mid-Level ActionScripter
no pez the name of the enemy its enemie :P

hahaha it was my mistake english is not my first language :lol:
Offline Profile Quote Post Goto Top
 
clayt
Member Avatar
I'm not crazy... ask my toaster!
ok cool.... so how do i do that then? sorry if im asking too much but i dont know an awful lot of AS.
Offline Profile Quote Post Goto Top
 
bracks
Member Avatar
Brackenwood Lightweight
this tread is interesting :rolleyes: ...is there a way to script a more real AI??
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
Go to Next Page
« Previous Topic · Actionscript · Next Topic »
Add Reply