Corona SDK Ui Button for Main Menu - lua

I'm kinda new in using Corona SDK and I'm having a little problem on my main menu buttons. Whenever I press the buttons, it doesn't move or change the view; the buttons just disappear in the title screen.
module(..., package.seeall)
local ui = require ("ui")
local ui = require ("director")
local assetPath = "assets/"
local mainGroup = display.newGroup()
function new(params)
local titleScreen = display.newImageRect(assetPath .. "Law of Magic.jpg",
display.contentWidth, display.contentHeight)
titleScreen.x = display.contentWidth / 2
titleScreen.y = 265
mainGroup:insert(titleScreen)
director:changeScene("titleScreen")
local newgame = ui.newButton{ default = assetPath .. "new game.png",
onRelease = function(event) director:changeScene("New game") end,}
newgame.x = display.contentWidth / 2
newgame.y = 445
mainGroup:insert(newgame)
local continue = ui.newButton{ default = assetPath .. "continue.png",
onRelease = function(event) director:changeScene("Continue") end,}
continue.x = display.contentWidth / 2
continue.y = 447
mainGroup:insert(continue)
local option = ui.newButton{ default = assetPath .. "option.png",
onRelease = function(event) director:changeScene("Option") end,}
option.x = display.contentWidth / 2
option.y = 449
mainGroup:insert(option)
local help = ui.newButton{ default = assetPath .. "help.png",
onRelease = function(event) director:changeScene("Help") end,}
help.x = display.contentWidth / 2
help.y = 451
mainGroup:insert(help)
local exitgame = ui.newButton{ default = assetPath .. "exit game.png",
onRelease = function(event) director:changeScene("Exit game") end,}
exitgame.x = display.contentWidth / 2
exitgame.y = 453
mainGroup:insert(exitgame)
return mainGroup
end

First I see you declare local ui two times.
Second you should use storyboard since it is supported by corona and you are new to coronaSDk

Also for this: director:changeScene("New game")
It's going to try and go to a scene, you are putting the name of the scene file. In this case, it's looking for a file called:
New Game.lua
in the same folder as your main.lua file. I personally would avoid filenames with spaces in them, and while the simulator isn't case sensitive, devices are and you have to make sure the filenames case matches what you are coding.
Also I'm concerned about this line: director:changeScene("titleScreen")
It would try to change scenes before you ever get to setting up your buttons.

Change this line
local ui = require ("director")
to
local director = require ("director")
And yeah make sure the file names are correct.

Related

Corona sdk audio not playing

I have been trying to test out some stuff in Corona sdk because I want to make a math game that will have levels of math for children up to adults. Here is the code I have (It is just a test)
Main.lua:
display.setStatusBar(display.HiddenStatusBar)
local ButtonClickedSound = audio.loadSound( "Button Clicked Sound.wav")
local FailSound = audio.loadSound( "Fail Sound.wav" )
local SucessSound = audio.loadSound( "Sucess Sound.mp3" )
-- Physics
local physics = require('physics')
physics.start()
local FailSoundChannel = audio.play(FailSound)
-- local gameBg = display.newImage('\Images\Background')
Config.lua:
application =
{
content =
{
width = 320,
height = 480,
scale = "letterbox"
},
}
I have a sound in my file and everything is correctly named. Do you know what the problem is?
display.setStatusBar(display.HiddenStatusBar)
local ButtonClickedSound = audio.loadSound( "Button Clicked Sound.wav")
local FailSound = audio.loadSound( "Fail Sound.wav" )
local SucessSound = audio.loadSound( "Sucess Sound.mp3" )
-- Physics
physics = require('physics')
physics.start()
local function delay()
local FailSoundChannel = audio.play(FailSound)
end
timer.performWithDelay(100, delay, 1)
try this and see. And more over check whether the file names and path are correct.

Why is Corona not reloading the scene?

I try to make a card game that will change scenes when the player touches on the card but the initial page does not load again.
My code follows:
main.lua
local storyboard = require("storyboard")
local background = display.newImage("Icon-72.png")
storyboard.gotoScene("level1")
level1.lua
local storyboard = require("storyboard")
local level1 = storyboard.newScene()
function level1:createScene( event )
print("level 1 create scene")
local group = self.view
local x = 3
group:insert(display.newText(x,40,50))
-- body
local card = display.newImage("Icon-Small.png")
card.x = 50 ; card.y = 150
group:insert(card)
function card:touch(event )
display.remove(card)
storyboard.gotoScene("level2")
end
card:addEventListener("touch",card)
end
function level1:enterScene( event )
local group = self.view
local card = display.newImage("Icon-Small.png")
card.x = 50 ; card.y = 150
group:insert(card)
function card:touch(event )
display.remove(card)
storyboard.gotoScene("level2")
end
card:addEventListener("touch",card)
-- body
end
level1:addEventListener("createScene",level1)
level1:addEventListener("enterScene",level1)
return level1
level 2 :
local storyboard = require("storyboard")
local level2 = storyboard.newScene()
function level2:createScene( event )
print("level2 create")
local group = self.view
storyboard.purgeScene("level1")
storyboard.gotoScene("level1")
end
level2:addEventLister("createScene",level2)
return level2
You have a typo at the bottom of level2.lua:
level2:addEventLister("createScene",level2)
Should be:
level2:addEventListener("createScene",level2)

Corona update and my code got error and i didn't modify anything

Ok so i have a huge problem, i make a game and i worked in past for this project and for a while i didn't modify anything in code , and today when i tried to open it in Corona Simulator it gives me this error , it said something about the menu.lua and director class, is possible that the problem to be the director class? i mean i have to take an update of that or something like that , i will attach a image of the error i get.
Here is my code from Menu
module(..., package.seeall)
local director = require("director")
local sprite = require("sprite")
new = function( params )
local menuDisplay = display.newGroup()
local background = display.newImage("bgmenu.jpg")
background.x = 240
background.y = 160
local spriteSheet = sprite.newSpriteSheet("sprite1.png", 54,70)
local monsterSet = sprite.newSpriteSet(spriteSheet, 1, 7)
sprite.add(monsterSet, "running", 1, 6, 600, 0)
local monster = sprite.newSprite(monsterSet)
monster:prepare("running")
monster:play()
monster.x = 60
monster.y = 200
local monster2 = sprite.newSprite(monsterSet)
monster2:prepare("running")
monster2:play()
monster2.x = 420
monster2.y = 200
monster2.xScale = monster2.xScale * -1
local title = display.newImage("title.png")
title.x = 240
title.y = 80
local playButton = display.newImage("playButton.png")
playButton.x = 240
playButton.y = 220
menuDisplay:insert(background)
menuDisplay:insert(monster)
menuDisplay:insert(monster2)
menuDisplay:insert(title)
menuDisplay:insert(playButton)
local function buttonListener( event )
director:changeScene( "game", "downFlip" )
return true
end
-
playButton:addEventListener("touch", buttonListener )
return menuDisplay
end
i really need to fix this until friday becase is a project for a competition.
here is a link of error i get : http://imgur.com/0S6x6uc
You have minus sign before playButton:addEventListener("touch", buttonListener ) which prevents the file from compiling. That's probably why Corona doesn't load your file.

Corona SDK - Pop Up Window (with Director Class)

I am new to game development and I am trying to create a simply GUI framework for my current project. I am currently using the Director Class 1.4 for my scene management. I have got my project o change scenes, but now I want to create a pop up window. I just want the pop up window to show up over the current scene I'm on. Below is the code for my main.lua and menu.lua (my initial scene). If anyone could help me out, I would really appreciate it. Please try to be as specific as possible, because I am very new to Corona and programming in general.
main.lua
_W = display.contentWidth
_H = display.contentHeight
local director = require ("director");
local mainGroup = display.newGroup();
local function main()
mainGroup:insert(director.directorView);
director:changeScene("menu");
return true;
end
main();
menu.lua
module(..., package.seeall)
function new()
local localGroup = display.newGroup();
local bg = display.newImage("Images/background1.PNG");
local myText = display.newText("Merchant", 0, 0, native.systemFont, 24)
myText:setTextColor(255, 255, 255)
myText:setReferencePoint(display.CenterReferencePoint);
myText.x = _W/2; myText.y = _H/2;
local hero_btn = display.newImage("Images/weaponcraft.PNG", 25, 25);
hero_btn:setReferencePoint(display.BottomLeftReferencePoint);
hero_btn.x = 252; hero_btn.y = 475;
hero_btn.scene = "heroMain";
local craft_btn = display.newImage("Images/smithing.PNG", 25, 25);
craft_btn:setReferencePoint(display.BottomLeftReferencePoint);
craft_btn.x = 7; craft_btn.y = 475;
craft_btn.scene = "craftMain";
local inventory_btn = display.newImage("Images/inventory1.png");
inventory_btn:setReferencePoint(display.CenterReferencePoint);
inventory_btn.x = _W/2; inventory_btn.y = 430;
--inventory_btn.scene = "inventory";
function changeScene(e)
if(e.phase == "ended") then
director:changeScene(e.target.scene);
end
end
localGroup:insert(bg);
localGroup:insert(hero_btn);
localGroup:insert(craft_btn);
hero_btn:addEventListener("touch", changeScene);
craft_btn:addEventListener("touch", changeScene);
return localGroup;
end
You can use the native.showAlert function. This shows a popup dialog box.
Something like this:
local function onComplete( event )
local action = event.action
if "clicked" == event.action then
if 1 == event.index then
end
end
local alert = native.showAlert( "You are in scene1!", "Congratulations!", { "OK" }, onComplete )
This show a dialog box with a "You are in scene1!" title, "Congratulations!" subtitle, and a "OK" button that closes the dialog box when you click(or tap) it.
Put these code in the front of your scene, and change the native.showAlert properties to the words you want.
You can create your custom pop up and can handle it by your own I am putting the sample code.
--function to create a dialog to be shown on the game over
local function gameOverAlertScreen()
--Display item used in the popup screen
local alertBox , restartBtn, levelBtn, soundOnOffSwitch, quitOnOffSwitch, imageSheetSoundOnOff, imageSheetQuitOnOff
--initial constans used for positioning the display items
local startTX = -400
local startTY = halfH
local btnInitailY = halfH -50
local btnMargin = 10
--cancel the game pausse timer
if(gameTimer_main) then
timer.cancel(gameTimer_main)
gameTimer_main = nil
end
local optionSoundSheet =
{
-- The params below are required
width = 51,height = 51,numFrames = 2,-- The params below are optional; used for dynamic resolution support
sheetContentWidth = 102, -- width of original 1x size of entire sheet
sheetContentHeight = 51 -- height of original 1x size of entire sheet
}
local optionQuitSheet =
{
-- The params below are required
width = 51,height = 51,numFrames = 2,-- The params below are optional; used for dynamic resolution support
sheetContentWidth = 102, -- width of original 1x size of entire sheet
sheetContentHeight = 51 -- height of original 1x size of entire sheet
}
isFirstTime=true
--pauseScreen()
dialog=true
alertBox = display.newImage("image/popup_dialog.png")
alertBox.x = halfW; alertBox.y = halfH
alertBox:setReferencePoint(display.CenterReferencePoint)
--creating the restart button fot the popup dialog
restartBtn = widget.newButton{
defaultFile="image/replay_btn.png",overFile = "image/replay_btn_hover.png",
isEnable=true,onRelease = onReplayBtnGameOverRelease -- event listener function
}
restartBtn.x = halfW
restartBtn.y = btnInitailY
--creating the level button
levelBtn = widget.newButton{
defaultFile="image/menu.png",overFile = "image/menu_hover.png",
isEnable=true, onRelease = onLevelBtnGameOverRelease -- event listener function
}
levelBtn.x = halfW
levelBtn.y = restartBtn.y + restartBtn.height + btnMargin
--creating the sound on off switch
imageSheetSoundOnOff = graphics.newImageSheet( "image/sound.png", optionSoundSheet )
soundOnOffSwitch = widget.newSwitch
{
left = screenW * 0.18,top = screenH * 0.73, style = "checkbox", sheet = imageSheetSoundOnOff,
initialSwitchState = soundOn,frameOff = "2",frameOn = "1",onRelease = onSoundButtonClicked, onPress = onSoundButtonClicked,
}
--creating the quit on off switch
imageSheetQuitOnOff = graphics.newImageSheet( "image/vibration.png", optionQuitSheet )
quitOnOffSwitch = widget.newSwitch
{
left = screenW * 0.7,top = screenH * 0.735,style = "checkbox",sheet = imageSheetQuitOnOff,onPress = onViberationButtonClicked,
initialSwitchState = vibrationOn,frameOff = "2",frameOn = "1",
}
--soundOnOffSwitch:setState({ isOn = soundOn })
--quitOnOffSwitch:setState({ isOn = vibrationOn})
--create/position logo/title image on upper-half of the screen
local titleLogo = display.newImageRect( "image/gameover.png",144,30)
titleLogo:setReferencePoint( display.CenterReferencePoint )
titleLogo.x = halfW
titleLogo.y = btnInitailY - 65
if popupGameOverGroup == nil then
popupGameOverGroup = display.newGroup()
end
-- inserting the buttons and the alert dialog to the popup group
popupGameOverGroup:insert(alertBox)
popupGameOverGroup:insert(restartBtn)
popupGameOverGroup:insert(levelBtn)
popupGameOverGroup:insert(soundOnOffSwitch)
popupGameOverGroup:insert(quitOnOffSwitch)
popupGameOverGroup:insert(titleLogo)
transition.from(popupGameOverGroup, {time =1000,x=startTX,y=titleLogo.y,xScale = 1, yScale = 1,
transition = easing.inOutExpo})
localGroup:insert(popupGameOverGroup)
showAdd()
end

Scenes and groups trouble using Lua and Corona SDK

My buttons and scene changes are bugging, the buttons on my titlescreen scene bellow both work and direct to the appropriate screens, but they will only do it once. So I cannot naviagte to options, then back to the the title screen, then back to options again - and I cannot work out why?
Here is my titlescreen file:
module(..., package.seeall)
local assetPath = "assets/"
local mainGroup = display.newGroup()
function new()
local ui = require("ui")
local titleScreen = display.newImageRect(assetPath .. "mainMenu.png", display.contentWidth, display.contentHeight)
titleScreen.x = display.contentWidth / 2
titleScreen.y = display.contentHeight / 2
mainGroup:insert(titleScreen)
local onPlayTouch = function( event )
if event.phase == "release" then
director:changeScene("gameScreen")
end
end
local playButton = ui.newButton{
defaultSrc = assetPath .. "playnowbtn.png",
defaultX = 222,
defaultY = 62,
overSrc = assetPath .. "playnowbtn-over.png",
overX = 222,
overY = 62,
onEvent = onPlayTouch
}
playButton.x = display.contentWidth / 2
playButton.y = 50;
mainGroup:insert( playButton )
local onOptionTouch = function( event )
if event.phase == "release" then
director:changeScene("optionsScreen")
end
end
local optionButton = ui.newButton{
defaultSrc = assetPath .. "playnowbtn.png",
defaultX = 222,
defaultY = 62,
overSrc = assetPath .. "playnowbtn-over.png",
overX = 222,
overY = 62,
onEvent = onOptionTouch
}
optionButton.x = display.contentWidth / 2
optionButton.y = 190;
mainGroup:insert( optionButton )
return mainGroup
end
My options file looks like this :
module(..., package.seeall)
function new()
local assetPath = "assets/"
local localGroup = display.newGroup()
local background = display.newImage (assetPath .."optionsScreen.png")
localGroup:insert(background)
local onBackTouch = function( event )
if event.phase == "release" then
director:changeScene("titleScreen")
end
end
local backButton = ui.newButton{
defaultSrc = assetPath .. "playnowbtn.png",
defaultX = 222,
defaultY = 62,
overSrc = assetPath .. "playnowbtn-over.png",
overX = 222,
overY = 62,
onEvent = onBackTouch
}
backButton.x = display.contentWidth / 2
backButton.y = display.contentHeight / 2
localGroup:insert(backbutton)
return localGroup
end
Now the button is displayed on the options scene and responds to touch, but does not direct back to the titlescreen.
I think I am getting confused with groups and only assigning images to scenes not the whole game?
Can anyone help me out, Thanks.
EDIT:
I'm also getting these runtime errors when clicking the buttons.
Runtime error
/Users/Lewis/Desktop/proj/optionsScreen.lua:30: ERROR: table expected. If this is a function call, you might have used '.' instead of ':'
stack traceback:
[C]: ?
[C]: in function 'insert'
/Users/Lewis/Desktop/proj/optionsScreen.lua:30: in function 'new'
/Users/Lewis/Desktop/proj/director.lua:118: in function 'loadScene'
/Users/Lewis/Desktop/proj/director.lua:415: in function 'changeScene'
/Users/Lewis/Desktop/proj/titlescreen.lua:67: in function 'onEvent'
/Users/Lewis/Desktop/proj/ui.lua:94: in function
?: in function
Runtime error
/Users/Lewis/Desktop/proj/director.lua:151: attempt to call field 'unloadMe' (a nil value)
stack traceback:
[C]: in function 'unloadMe'
/Users/Lewis/Desktop/proj/director.lua:151: in function '_listener'
?: in function
?: in function
I don't use director.lua myself so I'm not 100% sure, but in your options.lua you put the following two lines inside the new() function:
local assetPath = "assets/"
local localGroup = display.newGroup()
However in your titlescreen.lua those lines are above the new() function, and I think that's how it needs to be.
In general you should make your indentation consistent, so that it's easy to notice which code is inside which code-blocks.
In your error print-out, it's asking for an unload me function.
Try adding this to your code (above the return statement) and see if it makes a difference:
function unloadMe()
print( "test" )
end
And see if that gets rid of the error. Another option is to ditch ui.lua and use the new widget library's widget.newButton() function instead. Here's the doc page for that (syntax is almost the same as what you already have, so shouldn't be much changes necessary):
http://developer.anscamobile.com/reference/index/widgetnewbutton

Resources