(Weird?) things happens when changing screen.

Hi everybody. Right now i’m coding a little shop app.
The money system is setted up and everything except one thing is looking fine. I’m using ICE wich i havnt use before, so im a bit of a newbie on that part. Anyhow, my issue is following:

When i click “Btn” and director has changed sceen to “Shop.lua” the ball and the text(that informs you how much money you got on your “account”) wont be “removed”. You can still se them on the other screen(wich has almmost no code on its lua file). I know that the Ball is a global object but i dont want to reset its state. If i change the “_G.Lvl = 2” to “_G.Lvl = 0” i wont see the ball on the shop screen but i cant see it on the MainScreen eighter. (Thats logic). Anyhow i know that this has something to do with Ice since i’ve coded a lot of test apps before and never had this issus until now(fitst time im using ice). So please tell me how to fix this.
Why wont the text remove?

Thanks in advance:)
//Philip

BTW: Here is my code :wink:

[lua] module(…, package.seeall)

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

require (“physics”)
require “ui”
require ( “ice” )
physics.start()
physics.setGravity(2)

display.setStatusBar( display.HiddenStatusBar )

local Background = display.newImage( “Background.png”)
Background.x = 512
Background.y = 384

local Grass = display.newImage( “Grass.png”)
Grass.x = 512
Grass.y = 704
physics.addBody (Grass, “static”, {bounce=0.0, density=1.0})

local Ground = display.newImage( “Ground.png”)
Ground.x = 512
Ground.y = 626

local MoneyBar = display.newImage( “MoneyBar.png”)
MoneyBar.x = 828
MoneyBar.y = 55

local moneys = ice:loadBox( “moneys” )

local GetMoneyFive = function( event )
moneys:increment( “cash”, 5 )
moneys:save()
end

local GetMoneySeven = function( event )
moneys:increment( “cash”, 7 )
moneys:save()
end

local GetMoneyTen = function( event )
moneys:increment( “cash”, 10 )
moneys:save()
end

_G.Lvl = 2

if _G.Lvl == 1 then
local RedChar = display.newImage (“Red_ball_Char.png”)
RedChar.x = display.contentWidth /2
RedChar.y = display.contentHeight /2
physics.addBody (RedChar, {bounce=0.6, density=1.0})
RedChar:addEventListener( “tap”, GetMoneyFive )
end

if _G.Lvl == 2 then
local GreenChar = display.newImage (“Green_ball_Char.png”)
GreenChar.x = display.contentWidth /2
GreenChar.y = display.contentHeight /2
physics.addBody (GreenChar, {bounce=0.6, density=1.0})
GreenChar:addEventListener( “tap”, GetMoneySeven )
end

if _G.Lvl == 3 then
local BlueChar = display.newImage (“Blue_ball_Char.png”)
BlueChar.x = display.contentWidth /2
BlueChar.y = display.contentHeight /2
physics.addBody (BlueChar, {bounce=0.6, density=1.0})
BlueChar:addEventListener( “tap”, GetMoneyTen )
end

MoneyS = display.newText( “0”, 0, 0, “Helvetica”, 60 )
MoneyS.x = MoneyBar.x
MoneyS.y = MoneyBar.y

local onEnterFrame = function( event )
MoneyS.text = moneys:retrieve( “cash” ) or 0
end

Runtime:addEventListener( “enterFrame”, onEnterFrame )

Btn = ui.newButton{
default = “ShopBtn.png”,
over = “ShopBtn_C.png”,
x = 102,
y = 708,
}

local function ShopS (event)
if event.phase == “ended” then
director:changeScene (“ShopScreen”, “fade”)
end
end

Btn:addEventListener (“touch”, ShopS)

localGroup:insert(Background)
localGroup:insert(Grass)
localGroup:insert(Ground)
localGroup:insert(Btn)
localGroup:insert(MoneyBar)

return localGroup
end
[import]uid: 119384 topic_id: 24074 reply_id: 324074[/import]

*Bump* Can’t somebody help me:/? [import]uid: 119384 topic_id: 24074 reply_id: 98507[/import]

You never add MoneyS and BlueChar to localGroup so they don’t get removed when director changes scenes.
[import]uid: 19626 topic_id: 24074 reply_id: 98511[/import]

Wow, thanks Rob! I didnt know that i should add text into a local group. Well as the old saying; You learn something new every day:)

The Text disapears but I’m getting weird errors when i try to insert the characters into my local group:/

This is how the terminal looks like :

Runtime error
c:\users\phi\desktop\shop\MainScreen.lua:107: ERROR: table expected. If
this is a function call, you might have used ‘.’ instead of ‘:’
stack traceback:
[C]: ?
[C]: in function ‘insert’
c:\users\phi\desktop\shop\MainScreen.lua:107: in fuRuntime error
c:\users\phi\desktop\shop\director.lua:134: ERROR: table expected. If th
is is a function call, you might have used ‘.’ instead of ‘:’
stack traceback:
[C]: ?
[C]: in function ‘insert’
c:\users\phi\desktop\shop\director.lua:134: in functi

[import]uid: 119384 topic_id: 24074 reply_id: 98516[/import]

did you use localGroup.insert() instead of localGroup:insert()? [import]uid: 31262 topic_id: 24074 reply_id: 98519[/import]

No, i didnt:/ [import]uid: 119384 topic_id: 24074 reply_id: 98524[/import]

Are you able to post your revised code? [import]uid: 31262 topic_id: 24074 reply_id: 98579[/import]

Make sure you are inserting them BELOW where you have added them in your code; I know you should see a nil error in that case but I know I’ve seen this sort of thing at least once in that scenario. [import]uid: 52491 topic_id: 24074 reply_id: 98694[/import]

Oh yeah, im aware of that, but im still getting errors :confused:
I cant see the problem:/

Here’s my code:

[lua] module(…, package.seeall)

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

require (“physics”)
require “ui”
require ( “ice” )
physics.start()
physics.setGravity(2)

display.setStatusBar( display.HiddenStatusBar )

local Background = display.newImage( “Background.png”)
Background.x = 512
Background.y = 384

local Grass = display.newImage( “Grass.png”)
Grass.x = 512
Grass.y = 704
physics.addBody (Grass, “static”, {bounce=0.0, density=1.0})

local Ground = display.newImage( “Ground.png”)
Ground.x = 512
Ground.y = 626

local MoneyBar = display.newImage( “MoneyBar.png”)
MoneyBar.x = 828
MoneyBar.y = 55

local moneys = ice:loadBox( “moneys” )

local GetMoneyFive = function( event )
moneys:increment( “cash”, 5 )
moneys:save()
end

local GetMoneySeven = function( event )
moneys:increment( “cash”, 7 )
moneys:save()
end

local GetMoneyTen = function( event )
moneys:increment( “cash”, 10 )
moneys:save()
end
Lvl = 2

if Lvl == 1 then
local RedChar = display.newImage (“Red_ball_Char.png”)
RedChar.x = display.contentWidth /2
RedChar.y = display.contentHeight /2
physics.addBody (RedChar, {bounce=0.6, density=1.0})
RedChar:addEventListener( “tap”, GetMoneyFive )
end

if Lvl == 2 then
local GreenChar = display.newImage (“Green_ball_Char.png”)
GreenChar.x = display.contentWidth /2
GreenChar.y = display.contentHeight /2
physics.addBody (GreenChar, {bounce=0.6, density=1.0})
GreenChar:addEventListener( “tap”, GetMoneySeven )
end

if Lvl == 3 then
local BlueChar = display.newImage (“Blue_ball_Char.png”)
BlueChar.x = display.contentWidth /2
BlueChar.y = display.contentHeight /2
physics.addBody (BlueChar, {bounce=0.6, density=1.0})
BlueChar:addEventListener( “tap”, GetMoneyTen )
end

MoneyS = display.newText( “0”, 0, 0, “Helvetica”, 60 )
MoneyS.x = MoneyBar.x
MoneyS.y = MoneyBar.y

local onEnterFrame = function( event )
MoneyS.text = moneys:retrieve( “cash” ) or 0
end

Runtime:addEventListener( “enterFrame”, onEnterFrame )

Btn = ui.newButton{
default = “ShopBtn.png”,
over = “ShopBtn_C.png”,
x = 102,
y = 708,
}

local function ShopS (event)
if event.phase == “ended” then
director:changeScene (“ShopScreen”, “fade”)
end
end

Btn:addEventListener (“touch”, ShopS)

localGroup:insert(Background)
localGroup:insert(Grass)
localGroup:insert(Ground)
localGroup:insert(Btn)
localGroup:insert(MoneyBar)
localGroup:insert(MoneyS)

–If i insert this, im getting weird errors:
localGroup:insert(RedChar)
localGroup:insert(BlueChar)
localGroup:insert(GreenChar)


return localGroup
end
[import]uid: 119384 topic_id: 24074 reply_id: 98767[/import]

Could be scoping issue.

Either break out the *Char declarations from the if statement:

[lua]local GreenChar
if Lvl == 2 then
GreenChar = display.newImage (“Green_ball_Char.png”)
GreenChar.x = display.contentWidth /2
GreenChar.y = display.contentHeight /2
physics.addBody (GreenChar, {bounce=0.6, density=1.0})
GreenChar:addEventListener( “tap”, GetMoneySeven )
end
if GreenChar then localGroup:insert(GreenChar) end[/lua]

Or instead move the insert to the if statement itself?

[lua]if Lvl == 2 then
local GreenChar = display.newImage (“Green_ball_Char.png”)
GreenChar.x = display.contentWidth /2
GreenChar.y = display.contentHeight /2
physics.addBody (GreenChar, {bounce=0.6, density=1.0})
GreenChar:addEventListener( “tap”, GetMoneySeven )
localGroup:insert(GreenChar)
end[/lua] [import]uid: 122384 topic_id: 24074 reply_id: 98772[/import]

The first one is half working. I got the same effect as before, but i dont get any errors in the terminal, but the ball is still displaying.

The second one is giving me errors and the balls wont be shown at all.

Do you got any other ideas how i can solve this problem :P? [import]uid: 119384 topic_id: 24074 reply_id: 98779[/import]

What errors show up in the second case? [import]uid: 122384 topic_id: 24074 reply_id: 98781[/import]

I dont get any errors, but the ball isnt showing at all:/ [import]uid: 119384 topic_id: 24074 reply_id: 98785[/import]