Mobile Controls
From GiderosMobile
Here you will find various resources to help you create games and apps in Gideros Studio.
note: You may have to provide your own assets (fonts, gfx, …).
Description
This class allows you to control your player, on your mobile device, using buttons.
It has 4 buttons: right, left, A, B.
note: this class uses the UI_Buttons#Button_with_Text class, but you are free to use any other kind of buttons.
Sample image:
Mobile Controls Class
Mobile = Core.class(Sprite)
function Mobile:init(xplayer1)
self:myButtons(xplayer1)
end
-- BUTTONS/TOUCH HANDLER
function Mobile:myButtons(xplayer1)
--function ButtonText:init(text, tfont, tcolor, tscalex, tscaley, upState, downState)
local mybtnright = ButtonText.new(">", nil, 0xffffff, 12, 8)
mybtnright:setPosition(myappwidth - mybtnright:getWidth() / 2, myappheight)
self:addChild(mybtnright)
local mybtnleft = ButtonText.new("<", nil, 0xffffff, 12, 8)
mybtnleft:setPosition(myappwidth - mybtnleft:getWidth() / 2 - mybtnright:getWidth() - 32, myappheight)
self:addChild(mybtnleft)
local mybtnA = ButtonText.new("A", nil, 0xffffff, 8, 8)
mybtnA:setPosition(mybtnA:getWidth() / 2, myappheight)
self:addChild(mybtnA)
local mybtnB = ButtonText.new("B", nil, 0xffffff, 8, 8)
mybtnB:setPosition(mybtnB:getWidth() / 2 + mybtnA:getWidth() + 8, myappheight - 24)
self:addChild(mybtnB)
-- buttons listenners
-- right
mybtnright:addEventListener(Event.TOUCHES_BEGIN, function(e)
if mybtnright:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyright = true
xplayer1.iskeyleft = false
end
end)
mybtnright:addEventListener(Event.TOUCHES_END, function(e)
if mybtnright:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyright = false
end
end)
mybtnright:addEventListener(Event.TOUCHES_CANCEL, function(e)
if mybtnright:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyright = false
end
end)
mybtnright:addEventListener(Event.TOUCHES_MOVE, function(e)
if mybtnleft:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyright = false
xplayer1.iskeyleft = true
end
end)
-- left
mybtnleft:addEventListener(Event.TOUCHES_BEGIN, function(e)
if mybtnleft:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyleft = true
xplayer1.iskeyright = false
end
end)
mybtnleft:addEventListener(Event.TOUCHES_END, function(e)
if mybtnleft:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyleft = false
end
end)
mybtnleft:addEventListener(Event.TOUCHES_CANCEL, function(e)
if mybtnleft:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyleft = false
end
end)
mybtnleft:addEventListener(Event.TOUCHES_MOVE, function(e)
if mybtnright:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyleft = false
xplayer1.iskeyright = true
end
end)
-- A (down)
mybtnA:addEventListener(Event.TOUCHES_BEGIN, function(e)
if mybtnA:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeydown = true
end
end)
mybtnA:addEventListener(Event.TOUCHES_END, function(e)
if mybtnA:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeydown = false
end
end)
mybtnA:addEventListener(Event.TOUCHES_CANCEL, function(e)
if mybtnA:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeydown = false
end
end)
mybtnA:addEventListener(Event.TOUCHES_MOVE, function(e)
if mybtnB:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeydown = false
xplayer1.iskeyup = true
end
end)
-- B (jump)
mybtnB:addEventListener(Event.TOUCHES_BEGIN, function(e)
if mybtnB:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyup = true
end
end)
mybtnB:addEventListener(Event.TOUCHES_END, function(e)
if mybtnB:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyup = false
end
end)
mybtnB:addEventListener(Event.TOUCHES_CANCEL, function(e)
if mybtnB:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyup = false
end
end)
mybtnB:addEventListener(Event.TOUCHES_MOVE, function(e)
if mybtnA:hitTestPoint(e.touch.x, e.touch.y) then
xplayer1.iskeyup = false
xplayer1.iskeydown = true
end
end)
end
Usage
In this example we have a class variable called player1. We pass it as an argument to the Mobile class.
-- mobile controls
local mymobile = Mobile.new(self.player1)
self:addChild(mymobile)