# Patrolling Sprites

Get help using Construct 2

### » Sun Sep 11, 2011 2:47 am

What would be some basic logic to make a sprite patrol back and forth?

I keep making conflicting conditions that just make the sprite freeze :(
B
3
S
2
G
1
Posts: 7
Reputation: 789

### » Sun Sep 11, 2011 12:56 pm

The idea is to have the sprite moving from point A to point B, and then coming back to point A.

Your sprite should have an instance variable "dir" that would stand for the direction the sprite is going. (example, patroling from left to right, up to down, etc...)
Naming and type of the variable is up to you (number or text).

For this example, let's consider a number, where 0 is the sprite going to the right, 1 is going to the left.

+ Is Sprite.dir = 0
++ Is Sprite.X < destination.x
=> Sprite.X + 1
++ Is Sprite.X >= destination.x
=> Sprite.dir = 1
=> destination.x = new destination

+ Is Sprite.dir = 1
++ Is Sprite.X > destination.x
=> Sprite.X - 1
++ Is Sprite.X <= destination.x
=> Sprite.dir = 0
=> destination.x = new destination

It is up to you the way you set your destination.
You can also use the same logic to test the Y position.

Hope it is clear enough and helps you, let me know.Kyatric2011-09-11 12:57:34
New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
243
S
85
G
39
Posts: 6,945
Reputation: 56,934

### » Sun Sep 11, 2011 2:39 pm

Worked like a charm. I could not wrap my head around what I needed to do.

Thanks for the help!
B
3
S
2
G
1
Posts: 7
Reputation: 789

### » Sun Sep 11, 2011 11:16 pm

Hey guys, I decided to try this out and I couldn't figure out what Kyatric meant - probably because I'm bad at reading events written this way.

What I did instead was make 2 guide objects that my sprite had to collide with to change directions.

Here is the capx:
http://www.mediafire.com/?kceexxcvq5il5s8

This works really well, but I would appreciate it if someone could edit this capx to represent what Kyatric is talking about :)

-D
B
14
S
3
G
2
Posts: 37
Reputation: 1,893

### » Sun Sep 11, 2011 11:43 pm

New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
243
S
85
G
39
Posts: 6,945
Reputation: 56,934

### » Mon Sep 12, 2011 12:13 am

Thank you :)
B
14
S
3
G
2
Posts: 37
Reputation: 1,893