How do I...

For questions about using Classic.

Post » Fri Jan 01, 2010 3:39 am

I want to have an object that will function as an enemy. When the players object comes within an x number of pixel radius, the object will chase the player. When the player escapes that radius, the object will cease following until the player reenters.

So basically, an enemy has a personal space, and when you invade that personal space, it chases you until you manage to leave it.

How exactly do I do this?

Thanks in advance.

-Sil
B
3
G
3
Posts: 18
Reputation: 911

Post » Fri Jan 01, 2010 6:22 am

You could use system compare with the distance() expression
System compare distance( enemy.x, enemy.y, player.x, player.y) < x amount
>enemy move at angle towards player, or what ever movement your using.
Image ImageImage
B
165
S
49
G
147
Posts: 8,047
Reputation: 96,586

Post » Fri Jan 01, 2010 11:13 am

As newt said, you need to compare the distance of the objects. Don't forget to run that compare under For Each Enemy. So basically:

For each Enemy
-> Distance(enemy.X,enemy.Y,player.X,player.Y) < attackdistance
== ATTACK!
B
18
S
8
G
4
Posts: 137
Reputation: 3,196

Post » Sat Jan 02, 2010 1:58 am

Thanks, worked like a charm.
B
3
G
3
Posts: 18
Reputation: 911

Post » Sat Jan 02, 2010 7:24 am

Another (and faster) way is to instead of using the for loop and the system compare, is to use the compare condition in the enemy object. This is just like the system compare, except the comparison is evaluated for each instance of enemy, picking the ones that meet the equation. So then you would use:

-> Distance(.X,.Y,player.X,player.Y) < attackdistance
== ATTACK!
B
4
S
2
G
5
Posts: 641
Reputation: 3,011


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest