Here is the working widget code:
[lua]
–
– main.lua
– Get pixel width and height of device you are running on –
local Screen_Width = display.pixelWidth
local Screen_Height = display.pixelHeight
local titleScreenGroup
local titleScreen
local startButton
–local Player – I’ve tried with and without this declaration
–local Player_StartX
local widget = require (“widget”)
– Variables to control flow of game –
local Game_Over = 0
----------------------------------------FUNCTION SECTION----------------------------------
-----------------------------------INITIALIZE GRAPHICS------------------------------------
– This section is where we load the graphic images used in our game and set their –
– initial positions. –
function Show_Title_Screen()
display.setStatusBar(display.HiddenStatusBar)
titleScreenGroup = display.newGroup()
titleScreen = display.newImage(“titlescreen.png”, 0, 0, true)
titleScreen.x = (display.contentWidth / 2)
titleScreen.y = (display.contentHeight / 2)
startButton = widget.newButton
{
left = 60,
top = Screen_Height / 2,
label = “Start”,
labelAlign = “center”,
font = “Arial”,
fontSize = 18,
labelColor = { default = {0,0,0}, over = {255, 255, 255} },
onEvent = onButtonEvent
}
– startButton = display.newImage(“startbutton.png”)
– startButton.x = Screen_Width / 2
– startButton.y = Screen_Height - (Screen_Height / 2)
– startButton.name = “startButton”
titleScreenGroup:insert(titleScreen)
titleScreenGroup:insert(startButton)
startButton:addEventListener(“tap”, loadGame);
end
–function loadGame(event)
– if event.target.name == “startbutton” then
– transition.to(titleScreenGroup, { time = 500, alpha = 0, onComplete = Graphics_Initialization});
– startButton:removeEventListener(“tap”, loadGame)
– end
–end
--------------------- THE BELOW CODE WORKS FOR WIDGETS ------
function loadGame(onEvent)
if onEvent.target == startButton then
transition.to(titleScreenGroup, {time = 500, alpha=0,
onComplete = Graphics_Initialization});
startButton:removeEventListener(“tap”, loadGame)
end
end
function Graphics_Initialization ()
– Player Set-Up –
Player = display.newImage (“crate.png”)
Player.x = 100
Player.y = (Screen_Height - Player.contentHeight * 2)
Player.name = “player”
– Background Set-Up –
end
function movePlayer (event)
local phase = event.phase
–local Player_StartX
–local Player_StartY
if “began” == phase then
Player_StartX = event.x - Player.x
–Player_StartY = event.y - Player.y
else if “moved” == phase then
Player.x = event.x - Player_StartX
if Player.x < (Player.contentWidth *.5) then
Player.x = (Player.contentWidth * .5) end
if Player.x > (Screen_Width - (Player.contentWidth *.5)) then
Player.x = (Screen_Width - (Player.contentWidth *.5)) end
–Player.y = event.y - Player_StartY
end
end
end
function main ()
Show_Title_Screen()
–Graphics_Initialization()
Runtime:addEventListener(“touch”, movePlayer)
end
main()
[/lua]