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 :(
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
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.
Develop games in your browser. Powerful, performant & highly capable.
Worked like a charm. I could not wrap my head around what I needed to do.
Thanks for the help!
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:
This works really well, but I would appreciate it if someone could edit this capx to represent what Kyatric is talking about :)
Here you go
Thank you :)