Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Take Control of Your Game: One-Touch Continuous Movement in Codea Lua On Your iPad

Power Up Your Gameplay Experience with Seamless Controls!

Hey there, aspiring game developers! Are you ready to add even more flair to your sprite movements? We’re back with another tutorial that lets you control a sprite’s continuous movement with just a single touch.

Picture this: A rocket gliding through space, dodging meteors while you control its every move with a simple tap. Sounds great, right? Let’s get to it!

Step 1: Setting Up the Environment

First, as always, set up your Codea environment for fullscreen viewing:

viewer.mode = FULLSCREEN

Step 2: Declare Your Sprite Variables

We need to initialize the sprite’s coordinates and movement speed.

local spriteX, spriteY = WIDTH/2, HEIGHT/2

local movingRight = false

local movingLeft = false

local spriteSpeed = 5

Step 3: Drawing the Sprite

Draw the sprite at its starting position:

function draw()

background(40, 40, 50)

sprite(asset.builtin.Space_Art.Red_Ship, spriteX, spriteY)

end

Step 4: Adding Continuous Touch Controls

We’ll modify the `touched()` function to move the sprite continuously with a single touch.

function touched(touch)

if touch.state == BEGAN then

if touch.x > spriteX then

movingRight = true

movingLeft = false

else

movingLeft = true

movingRight = false

end

elseif touch.state == ENDED then

movingRight = false

movingLeft = false

end

end

Step 5: Update Sprite Position

Finally, update the sprite’s position based on the touch input.

function draw()

background(40, 40, 50)

if movingRight then

spriteX = spriteX + spriteSpeed

elseif movingLeft then

spriteX = spriteX – spriteSpeed

end

sprite(asset.builtin.Space_Art.Red_Ship, spriteX, spriteY)

end

Complete Code Listing

Here’s the full code listing with all the modifications:

viewer.mode = FULLSCREEN

local spriteX, spriteY = WIDTH/2, HEIGHT/2

local movingRight = false

local movingLeft = false

local spriteSpeed = 5

function draw()

background(40, 40, 50)

if movingRight then

spriteX = spriteX + spriteSpeed

elseif movingLeft then

spriteX = spriteX – spriteSpeed

end

sprite(asset.builtin.Space_Art.Red_Ship, spriteX, spriteY)

end

function touched(touch)

if touch.state == BEGAN then

if touch.x > spriteX then

movingRight = true

movingLeft = false

else

movingLeft = true

movingRight = false

end

elseif touch.state == ENDED then

movingRight = false

movingLeft = false

end

end

Wrap Up

And there you go! With just a single touch, you can now move your sprite continuously along the X-axis. This opens up a whole new range of possibilities, like fluid side-scrollers or dodging games!

Keep dreaming, keep coding, and who knows—your next game could be the next big hit!

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.