Load a lua file when button is touched

how can i make my game load another lua file when i click on a button, lets say i click something that says “play game”, how can i make it load my game, or if i click “options” how can i make it load options. etc. thanks [import]uid: 10827 topic_id: 4216 reply_id: 304216[/import]

The TabBar sample in the SDK (Interface/TabBar) shows one way to do this. Look at the loadScreen() and showScreen() functions in main.lua.

Tim [import]uid: 8196 topic_id: 4216 reply_id: 13137[/import]

I am trying to incorporate that tutorial with my current file… however the click events are not working and it wont let me require another page… the app doesnt run with the viewController requirement in there, if I comment it out everything shows up (but still doesnt link)

Any help would be GREAT!!!

Ashlee

– activate multitouch so multiple touches can press different buttons simultaneously
system.activate( “multitouch” )

local ui = require(“ui”)

local viewController = require(“viewController”)

local mainView, tabView, currentScreen, tabBar
–left, top, width, height, cornr radius–
–nav btn1
local nav0 = display.newRoundedRect(20, 80, 280, 40, 10)
nav0:setFillColor(236,0,140)
nav0=display.newText(“Communication Design”, 70, 85, “Verdana-Bold”, 15)

local nav1 = display.newRoundedRect(20, 130, 280, 40, 10)
nav1:setFillColor(0,174,239)
nav1=display.newText(“Foundation”, 70, 135, “Verdana-Bold”, 15)

local nav2 = display.newRoundedRect(20, 180, 280, 40, 10)
nav2:setFillColor(43,182,115)
nav2=display.newText(“Fashion Design”, 70, 185, “Verdana-Bold”, 15)

local nav3 = display.newRoundedRect(20, 230, 280, 40, 10)
nav3:setFillColor(146,39,143)
nav3=display.newText(“Photography”, 70, 235, “Verdana-Bold”, 15)

local nav4 = display.newRoundedRect(20, 280, 280, 40, 10)
nav4:setFillColor(237,28,36)
nav4=display.newText(“Product Design”, 70, 285, “Verdana-Bold”, 15)

local function loadScreen(newScreen)
if currentScreen then
currentScreen:cleanUp()
end
currentScreen = require(newScreen).new()
tabView:insert(currentScreen)

return true
end

local function showScreen(event)
local t = event.target
local phase = event.phase

if phase == “ended” then
if t.id == 1 then
loadScreen(“screen1”)
–[[elseif t.id == 2 then
loadScreen(“screen2”)
elseif t.id == 3 then
loadScreen(“screen3”)
–]]
end
tabBar.selected(t)
end

return true
end

local function init()
–Create a group that contains the entire screen and tab bar
mainView = display.newGroup()

–Create a group that contains the screens beneath the tab bar
tabView = display.newGroup()
mainView:insert(tabView)

loadScreen(“screen1”)

tabBar = viewController.newTabBar{
–background = “tabBar.png”, --tab bar background
–tabs = {“Play”, “News”, “About”}, --names to appear under each tab icon
onRelease = showScreen --function to execute when pressed
}
mainView:insert(tabBar)

tabBar.selected()

return true
end


– Displays App title
title = display.newText( “PPG Test”, 0, 30, “Verdana-Bold”, 20 )
title.x = display.contentWidth/2 – center title
title:setTextColor( 255,255,255 )

– Determine if running on Corona Simulator

local isSimulator = “simulator” == system.getInfo(“environment”)

– Multitouch Events not supported on Simulator

if isSimulator then
–msg = display.newText( “”, 0, 380, “Verdana-Bold”, 14 )
msg.x = display.contentWidth/2 – center title
–msg:setTextColor( 255,255,0 )
end
[import]uid: 13009 topic_id: 4216 reply_id: 14770[/import]

http://developer.anscamobile.com/code/director-class-10 [import]uid: 7356 topic_id: 4216 reply_id: 14771[/import]

This is a GREAT video/resource! 1 thing though… Do you know how to clear buttons after I switch to a new screen? This is what Ive got, and Im looking around for what it might be, however, im at a loss…
module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

– Background
local background = display.newImage(“images/background1.png”)
localGroup:insert(background)

– Title
local title = display.newText(“PPG Test2”, 0, 0, native.systemFontBold, 16)
title:setTextColor( 255,255,255)
title.x = 160
title.y = 20
localGroup:insert(title)

– Buttons

local nav0 = display.newRoundedRect(20, 80, 280, 40, 10)
nav0:setFillColor(236,0,140)
nav0=display.newText(“Communication Design”, 70, 85, “Verdana-Bold”, 15)

local nav1 = display.newRoundedRect(20, 130, 280, 40, 10)
nav1:setFillColor(0,174,239)
nav1=display.newText(“Foundation”, 70, 135, “Verdana-Bold”, 15)


–local bt01 = display.newImage(“images/bt_moveFromLeft.png”)
local function nav0t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“none”)
end
end
nav0:addEventListener(“touch”,nav0t)

localGroup:insert(nav0)

–[[local bt02 = display.newImage(“images/bt_moveFromRight.png”)
local function bt02t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“moveFromRight”)
end
end
bt02:addEventListener(“touch”,bt02t)
bt02.x = 240
bt02.y = 80
localGroup:insert(bt02)

local bt03 = display.newImage(“images/bt_overFromLeft.png”)
local function bt03t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“overFromLeft”)
end
end
bt03:addEventListener(“touch”,bt03t)
bt03.x = 85
bt03.y = 150
localGroup:insert(bt03)

local bt04 = display.newImage(“images/bt_overFromRight.png”)
local function bt04t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“overFromRight”)
end
end
bt04:addEventListener(“touch”,bt04t)
bt04.x = 240
bt04.y = 150
localGroup:insert(bt04)

local bt05 = display.newImage(“images/bt_overFromTop.png”)
local function bt05t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“overFromTop”)
end
end
bt05:addEventListener(“touch”,bt05t)
bt05.x = 85
bt05.y = 220
localGroup:insert(bt05)

local bt06 = display.newImage(“images/bt_overFromBottom.png”)
local function bt06t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“overFromBottom”)
end
end
bt06:addEventListener(“touch”,bt06t)
bt06.x = 240
bt06.y = 220
localGroup:insert(bt06)

local bt07 = display.newImage(“images/bt_flip.png”)
local function bt07t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“flip”)
end
end
bt07:addEventListener(“touch”,bt07t)
bt07.x = 85
bt07.y = 290
localGroup:insert(bt07)

local bt08 = display.newImage(“images/bt_downFlip.png”)
local function bt08t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“downFlip”)
end
end
bt08:addEventListener(“touch”,bt08t)
bt08.x = 240
bt08.y = 290
localGroup:insert(bt08)

local bt09 = display.newImage(“images/bt_fade.png”)
local function bt09t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“fade”,“green”)
end
end
bt09:addEventListener(“touch”,bt09t)
bt09.x = 85
bt09.y = 360
localGroup:insert(bt09)

local bt10 = display.newImage(“images/bt_fade.png”)
local function bt10t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“fade”,255,255,255)
end
end
bt10:addEventListener(“touch”,bt10t)
bt10.x = 240
bt10.y = 360
localGroup:insert(bt10)
–]]

– MUST return a display.newGroup()
return localGroup
end
[import]uid: 13009 topic_id: 4216 reply_id: 14772[/import]

Calling removeSelf() on a button just like a normal display object seems to work but I’m wondering about this too and would love confirmation about this. [import]uid: 12108 topic_id: 4216 reply_id: 14779[/import]

I got it working when I replaced the roundedRectangle with a .png… Would you have any idea why a png would work instead of a rectangle?

here is the working code:

local bt03 = display.newImage(“images/btn1.png”)
local function bt03t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“overFromLeft”)
end
end
bt03:addEventListener(“touch”,bt03t)
bt03.x = 140
bt03.y = 100
localGroup:insert(bt03)


local bt03b = display.newImage(“images/btn1.png”)
local function bt03bt ( event )
if event.phase == “ended” then
director:changeScene(“screen1”,“overFromRight”)
end
end
bt03b:addEventListener(“touch”,bt03bt)
bt03b.x = 140
bt03b.y = 300
localGroup:insert(bt03b)

–And the Broken rectangle code
local function nav0t ( event )
if event.phase == “ended” then
director:changeScene(“screen2”,“none”)
end
end
nav0:addEventListener(“touch”,nav0t)

localGroup:insert(nav0)


local navb = display.newRoundedRect(20, 135, 280, 40, 10)
navb:setFillColor(236,0,140)
navb=display.newText(“Back”, 70, 135, “Verdana-Bold”, 15)

localGroup:insert(navb)
– Touch to go back
local function navb ( event )
if event.phase == “ended” then
director:changeScene(“screen1”,“none”)
end
end
background:addEventListener(“touch”,navb)

[import]uid: 13009 topic_id: 4216 reply_id: 14803[/import]

@ashleebeggs

Insert your objects to the group that is returned from the new() function that each scene has.

In your broken code :
[lua]local navb = display.newRoundedRect(20, 135, 280, 40, 10)
navb:setFillColor(236,0,140)
navb=display.newText(“Back”, 70, 135, “Verdana-Bold”, 15)

localGroup:insert(navb)
– Touch to go back
local function navb ( event )[/lua]

…there are 3 different uses of “navb” here. Use a different name for your rect and text. The function declaration should be:
[lua]local function navb:touch(event)[/lua]

Tip1: Invest 2 days on Lua and 2-3 days on the Corona documentation (guides + api ) before starting with your game. It’s going to be much easier/quicker this way.

Tip2: Bellow the “Post new comment” edit box there are some tags that you can use. To nicely append lua code open a [text][lua][/text] tag, place your code and then close it with [text][/lua][/text].
[import]uid: 7356 topic_id: 4216 reply_id: 14805[/import]