This is a tutorial for using my Puggle library and the Corona SDK. if you are not a Corona user ( and why not? ) then this will not be of much interest to you.

Controlling object, such as the player, using the keyboard, mouse, or a controller, is naturally very important for most games, at least non-touch based ones anyway.

With Puggle, the first thing you’ll want to do is register some actions, these are things like “moveLeft”, “jump”, “fire”, or whatever else is appropriate for your game.

For this tutorial, we’re just going to register some movement actions like so:

puggle.input:register( "move-up", { "w", "up" } )
puggle.input:register( "move-down", { "s", "down" } )
puggle.input:register( "move-left", { "a", "left" } )
puggle.input:register( "move-right", { "d", "right" } )

With those registered, you’ll now want to ‘listen’ for them and then act accordingly. There are many ways to do this but to keep things simple we’ll just use an enterFrame handler.

The first part is to actually listen for the action, like so:

if puggle.input:isButtonPressed( "move-left" ) then

end

Then, inside that you’ll want to move the player. Again, there are approximately 1,302,503 different ways to do this but for the purpose of this tutorial we’ll keep it very simple.

player.x = player.x - 1

This will move the player object one pixel left every frame, but we can improve it by using a speed variable, assigned previously in the code, rather than a magic number:

player.x = player.x - speed

And finally we can also utilise another Puggle system to get the game’s current deltatime to ensure the movement is smooth.

player.x = player.x - ( speed * puggle.time:delta() )

Putting this all together with the other 3 directions and you now have this.

You can use the WASD or arrow keys to slowly move our circle object around the screen.

Sometimes you’ll need to know if a button was just pressed this frame, such as for a ‘jump’ action, so for this we’ll register a new action.

In our case it’s going to be to change the colour, simply because things like jumping is beyond the scope of this tutorial.

puggle.input:register( "changeColour", { "space" } )

Then, in the enter frame handler with the rest of the code you’ll want to have this:

if puggle.input:wasButtonJustReleased( "changeColour" ) then

end

With this line to actually change the colour:

player:setFillColor( puggle.random:inRange( 10 ) * 0.1, puggle.random:inRange( 10 ) * 0.1, puggle.random:inRange( 10 ) * 0.1 )

All this is doing is getting three random values between 0 and 1 for the r, g, b values of the colour we want, and then setting it as the fill colour for our circle.

The code for this project can be found here.