# Move Sprite left/right when button touched

So basically, I want the Sprite to move when I press a button, so left button will move the character left and right button will move the character right. I am an absolute beginner and I think the forums are a good way to help. Thanks!

Unfortunately, we can’t provide individual tuition to every beginner, but we have a wiki link at the top with lots of help to get you started.

I also have some ebooks here. I recommend starting with Lua first, to get a basic feel for the language, and then Codea.

Ok, well thanks for the ebooks.

@CalebAnder - one more thing. You are welcome to post questions, but please keep all your questions about the same project, in one thread as far as possible, to avoid cluttering the forum. (You can edit the original post in your thread if you need to, to change the title).

Ok, will do!

@CalebAnder Here’s an example. You dont need buttons, but you can add them when you learn how.

EDIT: Basically, the whole screen is being used as a button here.

``````displayMode(FULLSCREEN)

function setup()
speed=0
direction=100
x=WIDTH/2
y=HEIGHT/2
end

function draw()
background(40, 40, 50)
fill(255)
text("touch the right or left side of the screen",WIDTH/2,HEIGHT-50)
sprite("SpaceCute:Beetle Ship",x,y,direction,100)
x=x+speed
if x>WIDTH+50 then
x=-50
end
if x<-50then
x=WIDTH+50
end
end

function touched(t)
if t.x<WIDTH/2 then
speed=-3
direction=-100
else
speed=3
direction=100
end
end
``````

@dave1707 thank you very much, but is there a way that when you press the right/left side of the screen it moves but when you let go it stops?

@CalebAnder why not read the in-app documentation on touch and see if you can work out how to do that yourself? Hint: you need your code to do something when touch.state is ENDED

@yojimbo2000 where would I find that?

At the top of this page where it says `Reference`. You can also get to a Reference when you start the Codea editor. Press the icon in the upper left corner of the screen. Also, when you’re editing a program, you can get to the reference by touching the key on the upper left of the keyboard that has the image of an eye.

Thanks @dave1707 you are extremely helpful. I looked at the reference but it doesn’t answer what I need. Your method is perfect for touch movement but i want the Sprite to stop moving when you let go. Thanks Dave!

In the Reference under touch, you’ll see a bunch of options like id, x, y, prevX, prevY. There is an option called state, which can be BEGAN, MOVING, ENDED. So when you touch the screen, BEGAN is true. If you slide your finger on the screen, then MOVING is true. When you lift your finger from the screen, then ENDED is true. So in my example above, in the touched function, I didn’t use any of that since it was just a simple example, but you could use an if statement to check if any of those 3 conditions are true. Since you’re just starting out, all of this is confusing, but over time it will all seem simple.

Thanks @dave1707, you are always there to help. I appreciate it as I have just started using codea, so thank you very much Dave!