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]