# How do I test if moving left or right.

Get help using Construct 2

### » Fri Jan 06, 2017 11:50 pm

I am attempting to make A.I. for the enemies in my game. The enemies have line of sight and platform behaviours. I need to know if there is a way to test if they are moving left or right. I thought that there would be a test for X vector in platform conditions but I didn't see it. Please help because I can't really progress with their A.I. until I figure this out.
B
14
S
3
Posts: 55
Reputation: 912

### » Sat Jan 07, 2017 12:17 am

If you want an enemy to follow you, give the enemy an instance variable like var_walking. Then in your events you can set up something like this:

Set the variable:
if enemy has line of sight to player
if enemy.x < player.x+1 -> set var_walking 0
if enemy.x > player.x -> set var_walking 1

Make the event happen:
if var_walking = 0 -> simulate control left
if var_walking = 1 -> simulate control right

You can also just do walking with the same variable and events if you don't want the enemy to follow the player. Something like this should work set up when the enemy doesn't have line of sight:

On collision with obstacle -> set enemy var_walking to 1-enemy.var_walking

I hope that makes sense.
B
62
S
20
G
57
Posts: 1,077
Reputation: 36,531

### » Sat Jan 07, 2017 9:42 pm

currypuff wrote:I am attempting to make A.I. for the enemies in my game. The enemies have line of sight and platform behaviours. I need to know if there is a way to test if they are moving left or right. I thought that there would be a test for X vector in platform conditions but I didn't see it. Please help because I can't really progress with their A.I. until I figure this out.

The expression 'Sprite.Platform.VectorX' gives you what you look for.

To use it in a condition.

Pick the Enemy (anyhow anyway)
System > Compare two values ... first = Enemy.Platform.VectorX ...... comparisation = < ...... second = 0
.............actions run if it goes left
Else
System > Compare two values ... first = Enemy.Platform.VectorX ...... comparisation = > ....... second = 0
.............actions run if it goes right
Else
.............actions run if it goes not right and not left
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Sun Jan 08, 2017 11:53 am

Thanks 99instances2go.
That's exactly what I needed, just skipped my mind that I could do that.
B
14
S
3
Posts: 55
Reputation: 912

### » Thu Jan 12, 2017 2:08 pm

After trying some stuff I've hit a problem. I can successfully test if a single enemy is walking and animate accordingly but with multiple enemies it doesn't work.
The enemies are made of a base 'body' where a hair, top and pants sprite are pinned (multiple sprites for different colours and styles of enemies.) The body is controlled by the platform behaviour while the clothes and hair are pinned to the body. I can't just test the x velocity of the body for clothes and hair as it just takes it from the first spawned enemy. (e.g. If the first enemy is at a wall not moving another enemy which is moving will play a still animation for its clothes and hair.)
Is there a way to link the speed of one sprites to its own components, or is there an easier way to go about this. (btw the body and its clothes and hair are in a container together.)
B
14
S
3
Posts: 55
Reputation: 912