Posted: 27th Aug 2011 0:14
Hi Everyone,

Enjoying AGK. I am stuck with a few things which I have searched for solutions for in the manuals, online help, examples and so on as well as trying various things myself without any luck as yet and any help would be appreciated.

I have a game in which I have a sprite moving left to right across the top of the screen using a simple script :
SetSpritePosition ( compad, x#, 95 )
//add 0.5 to the variable x#
x# = x# + 1.0

This works just fine.

The sprite is stopped at the right hand side of the screen and I have it looping back to the left and repeating from left to right again continuously from the left hand side.

When it gets to the right hand side of the screen - does anyone know if there is a way to have the sprite reverse direction from its position at the right hand side so that it goes from left to right and the right to left and continues in that loop?

I hope some of that makes some kind of sense.

Thanks and happy game making

Posted: 27th Aug 2011 2:29
Set a direction variable, say it equals 0 for right and 1 for left, or vice versa. When the sprite hits the edge, just change the direction value. In your code to move the sprite just add in a check for the direction and set the movement variable accordingly, eg x=x+1 or x=x-1.
Probably quicker to have just put some code, but you should get the idea.
Posted: 27th Aug 2011 11:16
I'd do something like this:
+ Code Snippet
moveSpeed = 1

// psuedocode
if sprite hits edge of screen Then moveSpeed = -moveSpeed

x# = x# + moveSpeed

This starts your sprite moving to the right at a speed of 1. When it hits the right edge, moveSpeed becomes -1 so the sprite moves left. When it hits the left edge, moveSeed becomes 1 and it starts moving right again.