Basic X-Y controls with buttons?

I started to *try* to learn Corona and LUA today and i have a sample code to control my player to move right and left, but I want to make a game where I have 4 directional buttons and I can control/move my player left-right-up-down.

I searched everywhere, but I couldn’t find anything on this.

Can somebody help me? [import]uid: 6587 topic_id: 5635 reply_id: 305635[/import]

There is joystick class here: http://developer.anscamobile.com/code/joystick

But if you just want a simple mechanism you can try this:

[lua]function move() – Moves sprite when called by frame event listener
if sprite.direction == “left” then sprite.x = sprite.x -2 – Move sprite according to direction which was set
elseif sprite.direction == “right” then sprite.x = sprite.x +2
elseif sprite.direction == “up” then sprite.y = sprite.y - 2
elseif sprite.direction == “down” then sprite.y = sprite.y + 2
end
end

function activate( event ) – Called whenever a button is pressed
local pos = event.target.pos – Get the position of the button which was pressed
print( pos )

if event.phase == “began” then – If the button has just been pressed
sprite.direction = pos – Set the sprite to the direction of the button
display.getCurrentStage():setFocus(event.target) – Keep event focus on the button
Runtime:addEventListener( “enterFrame”, move) – Start the event listenere to move the sprite
elseif event.phase == “ended” or event.phase == “cancelled” then – If the button is no longer pressed
Runtime:removeEventListener( “enterFrame”, move) – Remove the event listener
end
end

left = display.newRect( 20, 60, 50, 50 ) – Create a left button
left:addEventListener( “touch”, activate ) – Add a touch event listener
left.pos = “left” – Give it an ID so we can know which button was pressed

right = display.newRect( 140, 60, 50, 50 ) – Create a right button
right:addEventListener( “touch”, activate ) – Add a touch event listener
right.pos = “right” – Give it an ID so we can know which button was pressed

up = display.newRect( 80, 0, 50, 50 ) – Create a right button
up:addEventListener( “touch”, activate ) – Add a touch event listener
up.pos = “up” – Give it an ID so we can know which button was pressed

down = display.newRect( 80, 120, 50, 50 ) – Create a right button
down:addEventListener( “touch”, activate ) – Add a touch event listener
down.pos = “down” – Give it an ID so we can know which button was pressed

sprite = display.newRect( 0,0,20,20 ) – Create the sprite
sprite.x = display.contentWidth * 0.5
sprite.y = display.contentHeight * 0.5
sprite:setFillColor( 255,0,0 )
sprite.direction = “none”[/lua] [import]uid: 11393 topic_id: 5635 reply_id: 19358[/import]

Thank you very much!

I found another code on the forum as well, but with these two I can already start to understand :slight_smile:

I think, I understand the basic logic behind LUA already a bit :slight_smile: [import]uid: 6587 topic_id: 5635 reply_id: 19391[/import]

Hm…

I added this code to my game and it works, however the player moves only in the direction, which I was clicking first, no matter which button I click after that.

If I click right first, the player moves only to the right with every button, when I click up first, then it moves only up and so on… [import]uid: 6587 topic_id: 5635 reply_id: 19494[/import]

I expanded this code and it seems to work now (player image facing right or left based on movement direction):

[code]local function move() – Moves sprite when called by frame event listener
if player.direction == “left” then
player.x = player.x -2 – Move sprite according to direction which was set
if player.xScale == 1 then
player.xScale = -1 – mirrors image if not facing left
end
elseif player.direction == “right” then
player.x = player.x +2
if player.xScale == -1 then
player.xScale = 1 – mirrors image if not facing right
end

elseif player.direction == “up” then player.y = player.y - 2
elseif player.direction == “down” then player.y = player.y + 2

end
end

local function activate( event ) – Called whenever a button is pressed
local this = event.target
local phase = event.phase
local pos = event.target.pos – Get the position of the button which was pressed
print( pos )

if “began” == phase then – If the button has just been pressed
player.direction = pos – Set the sprite to the direction of the button
display.getCurrentStage():setFocus( this , event.pos )
this.isFocus = true – Keep event focus on the button
Runtime:addEventListener( “enterFrame”, move) – Start the event listenere to move the sprite
elseif this.isFocus then
if “ended” == phase or “cancelled” == phase then – If the button is no longer pressed
– End Focus
display.getCurrentStage():setFocus( this , nil )
this.isFocus = false
Runtime:removeEventListener( “enterFrame”, move) – Remove the event listener
end
end
end

local left = display.newImage( “left.png”,404,255 ) – Create a left button
left.pos = “left” – Give it an ID so we can know which button was pressed
left:addEventListener( “touch”, activate ) – Add a touch event listener

local right = display.newImage( “right.png”,440,255 ) – Create a right button
right.pos = “right” – Give it an ID so we can know which button was pressed
right:addEventListener( “touch”, activate ) – Add a touch event listener

local up = display.newImage( “up.png”,3,244 ) – Create a right button
up.pos = “up” – Give it an ID so we can know which button was pressed
up:addEventListener( “touch”, activate ) – Add a touch event listener

local down = display.newImage( “down.png”,3,282 ) – Create a right button
down.pos = “down” – Give it an ID so we can know which button was pressed
down:addEventListener( “touch”, activate ) – Add a touch event listener
player = display.newImage (“player.png”,0,0) – Create the player
physics.addBody(player) – adds physic to the player
player.x = 48
player.y = 144
player.myName = “Player” – adds a name for the player (collision)
player.isFixedRotation = true – player doesn’t rotate on collision
player.direction = “none”
localGroup:insert(player)[/code] [import]uid: 6587 topic_id: 5635 reply_id: 19781[/import]