Screens

Hi
I have updated the TabBar sample to fit my requirements to display screens. The following, trying to do a simple display a screen on a back group is touched. However, for some reasons, the system displays error on the addEventListeners

– Main
function loadScreen(newScreen)
if currentScreen then
currentScreen:cleanUp()
end
currentScreen = require(newScreen).new()
mainView:insert(currentScreen)
return true
end
local function init()
–Create a group that contains the entire screen and tab bar
mainView = display.newGroup()
loadScreen(“screen1”)
return true
end

–Start the program!
init()
– Screen 1

module(…, package.seeall)

function new()
local g = display.newGroup()

local background = display.newImage(“b.png”)
g:insert(background)

local message = display.newText(“Screen 1”, 0, 0, native.systemFontBold, 16)
message:setTextColor(0, 0, 0)
message.x = display.contentWidth*0.5
message.y = display.contentHeight*0.5
g:insert(message)

background:addEventListener(“touch”, loadScreen(“screen2”))

function g:cleanUp()
g:removeSelf()
end

return g
end
[import]uid: 11038 topic_id: 3570 reply_id: 303570[/import]

You cannot pass additional arguments to an EventListener. The only thing that gets passed to the function is the Event object itself.

So you will need to do something along these lines:

[code]
background:addEventListener(“touch”, touchBackground)

function touchBackground(event)
loadScreen(“screen2”)
end
[/code] [import]uid: 8444 topic_id: 3570 reply_id: 10763[/import]

Thanks for your reply.

I have fixed the event however it still not showing the screen2 .

function new()
local g = display.newGroup()
local background = display.newImage(“b.png”)
g:insert(background)
local message = display.newText(“Screen 1”, 0, 0, native.systemFontBold, 16)
message:setTextColor(0, 0, 0)
message.x = display.contentWidth*0.5
message.y = display.contentHeight*0.5
g:insert(message)
background:addEventListener(“touch”, sreenLoad)
function sreenLoad ( event )
loadScreen(“screen2”)
end

function g:cleanUp()
g:removeSelf()
end

return g
end [import]uid: 11038 topic_id: 3570 reply_id: 10770[/import]

it seems like you have the “sreenLoad” function nested inside the new() function…

make sure each function closes properly, i.e:

function new()
function body
end

function sreenLoad(event)
function body
end

function g:cleanUp()
g:removeSelf()
end

make sure all of your functions are separate. [import]uid: 8444 topic_id: 3570 reply_id: 10771[/import]

thanks
it seems i copied the wrong code. However, i’ve done this and moved the functions outside the main.
The problem is after moving the functions outside the simulator displays a black screen which means something wrong.

in addition, another problem occurs when i use the addlistener event it displays another black screen

I really just need to do 3 screens with an option to move between them :frowning: its really killing me [import]uid: 11038 topic_id: 3570 reply_id: 10775[/import]