Jump to content


Photo

Enemy Ai


  • Please log in to reply
11 replies to this topic

#1 Eistee21

Eistee21

    GMC Member

  • New Member
  • 64 posts

Posted 12 February 2009 - 04:51 PM

I made an enmy that has an Ai that should create an arrow if the player gets near to it, but it keeps creating arrows all the time.
What can i do to make it create an arrow every 2 seconds?
i´ve got this step event:
[codebox]if distance_to_object(obj_playerb)<300{
instance_create(obj_orcarch.x,obj_orcarch.y,orcarr
ow)
}[/codebox]

Edited by Eistee21, 12 February 2009 - 07:04 PM.

  • 0

#2 Diptoman

Diptoman

    GMC Member

  • GMC Member
  • 1742 posts
  • Version:GM8

Posted 12 February 2009 - 05:02 PM

Alarms!
Add this:
if distance_to_object(obj_playerb)<300 && alarm[0]<=0
{instance_create(obj_orcarch.x,obj_orcarch.y,orcarrow)
alarm[0]=room_speed*2;}
And then create an alarm0 event and put a random comment in it (whatever you want).

Edited by dmsuper, 12 February 2009 - 05:04 PM.

  • 0

#3 Pregnant_Cow

Pregnant_Cow

    GMC Member

  • New Member
  • 49 posts

Posted 12 February 2009 - 05:03 PM

Honestly, I don't see a problem with it. Although I like to organize my scripts so they make a little more sense. Something like this.

if distance_to_object(obj_playerb)<300
 {
 instance_create(obj_orcarch.x,obj_orcarch.y,orcarrow)
 }

But it looks just fine, I don't know what's going on.


EDIT: Do you mean it doesn't shoot till it's close enough, but when it gets close enough it sends a stream of them? If so, then they got your answer.

Edited by Pregnant_Cow, 12 February 2009 - 05:06 PM.

  • 0

#4 Eistee21

Eistee21

    GMC Member

  • New Member
  • 64 posts

Posted 12 February 2009 - 05:07 PM

I created a alarm 0 but if I make the steps 0 it keeps shooting arrows everytime if i make it 1 it doesnt shoot arrows.

Edit: if i get close enough it shoots a mass of arrows

(sorry for bein a noob ;))

Edited by Eistee21, 12 February 2009 - 05:09 PM.

  • 0

#5 Kyon

Kyon

    Creator of Dirkels

  • New Member
  • 4237 posts

Posted 12 February 2009 - 05:09 PM

Create:
can_attack = true

step:
if can_attack = true
{
if distance_to_object(obj_playerb)<300
{
instance_create(obj_orcarch.x,obj_orcarch.y,orcarrow)
can_attack = false
alarm[0] = room_speed*2;
}
}

alarm[0]:
can_attack = true

easy ;)

Edited by Kyon, 12 February 2009 - 05:10 PM.

  • 0

#6 Eistee21

Eistee21

    GMC Member

  • New Member
  • 64 posts

Posted 12 February 2009 - 05:24 PM

thank you, worked perfectly.
  • 0

#7 Kyon

Kyon

    Creator of Dirkels

  • New Member
  • 4237 posts

Posted 12 February 2009 - 05:25 PM

np
  • 0

#8 Eistee21

Eistee21

    GMC Member

  • New Member
  • 64 posts

Posted 12 February 2009 - 05:42 PM

Can i do it the same way with my player if i want him to can only attack every 2 seconds?
  • 0

#9 Kyon

Kyon

    Creator of Dirkels

  • New Member
  • 4237 posts

Posted 12 February 2009 - 06:06 PM

yes, can_attack in your player object shall not effect the orc archer object...

Why dont you put:
image_angle=direction

if (place_free(x, y + 1)) 
{
	gravity = 0.4; 
	}
	else 
	{
	gravity = 0;  
}


vspeed = min(12, vspeed);

in the step event of the arrow

and in the step event of orc archer:
if can_attack = true
{
if distance_to_object(obj_playerb) < 300
{
var shoot;
shoot=instance_create(x,y,obj_arrow);
shoot.direction=point_direction(x,y,obj_playerb.x,obj_playerb-20.y);
shoot.speed=15
can_attack = false
alarm[0] = room_speed*2
}
}

so now it automaticly shoot the arrow towards a bit above obj_playerb,
and the arrow has gravity, (for the nice arrow effects...)

I use this code for my game too ;)

np
  • 0

#10 Eistee21

Eistee21

    GMC Member

  • New Member
  • 64 posts

Posted 12 February 2009 - 06:14 PM

if I start the game now this error appears:

[codebox]FATAL ERROR in
action number 1
of Step Event
for object obj_orcarch:

COMPILATION ERROR in code action
Error in code at line 7:
shoot.direction=point_direction(x,y,obj_playerb.x,
obj_playerb-20.y);

at position 67: Symbol , or ) expected.[/codebox]
  • 0

#11 Kyon

Kyon

    Creator of Dirkels

  • New Member
  • 4237 posts

Posted 12 February 2009 - 06:50 PM

if I start the game now this error appears:

FATAL ERROR inaction number 1of  Step Eventfor object obj_orcarch:COMPILATION ERROR in code actionError in code at line 7:   shoot.direction=point_direction(x,y,obj_playerb.x,obj_playerb-20.y);at position 67: Symbol , or ) expected.


hhmm... This is a bit weird...
But the -20 away, maybe it works now? and the ;
and ofcource be shure you didnt use the enter in your script... ;)

Edited by Kyon, 12 February 2009 - 06:50 PM.

  • 0

#12 Eistee21

Eistee21

    GMC Member

  • New Member
  • 64 posts

Posted 12 February 2009 - 07:03 PM

Ok now it worked ^^
Thank you
I think this is solved
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users