Change an Image

Hey,

Here is my code:
local room = StartScreen
local scene = display.newImage( “StartScreen.png”, 0, 0, true )

local funtion sceneSelector()
if room == StartScreen then
local scene = display.newImage( “StartScreen.png”, 0, 0, true )
elseif room == SatnavNewCase then
local scene = display.newImage( “SatnavNewCase.png”, 0, 0, true )
end
end

sceneSelector()

What I want to achieve though is not to create a new image every time the if statement is correct but to change the image of ‘scene’.

I’m sure it’s pretty simple, just need a quick hand.

Cheers,
Ace [import]uid: 9899 topic_id: 4294 reply_id: 304294[/import]

you’ve got [lua]local scene[/lua] in your function. which is a separate variable to the one outside of your function. remove the world [lua]local[/lua] inside your function [import]uid: 6645 topic_id: 4294 reply_id: 13337[/import]

haha perfect thanks

Ace [import]uid: 9899 topic_id: 4294 reply_id: 13338[/import]

Actually, no, it’s not working, still adding an image on top of the Start Screen image instead of replacing it

Ace [import]uid: 9899 topic_id: 4294 reply_id: 13339[/import]

firstly you’re calling startscreen twice

[lua]local room = StartScreen
local scene = display.newImage( “StartScreen.png”, 0, 0, true ) – once here

local funtion sceneSelector()
if room == StartScreen then
scene = display.newImage( “StartScreen.png”, 0, 0, true ) – once here

end

sceneSelector()[/lua]

secondly, i’m guessing display.newImage does exactly that, creates a new image. you’re just changing your pointer from your first image to your second. I suggest you remove the image first

maybe:

[lua]local room = StartScreen
local scene

local funtion sceneSelector()

local newScene

if(scene~=nil) then scene:removeSelf() end – remove original if present

if room == StartScreen then
newScene = display.newImage( “StartScreen.png”, 0, 0, true )

else…
end

return newScene
end

scene = sceneSelector()[/lua]

personally i’d add a parameter… something like (although I haven’t thought it through exactly)

[lua]local room = StartScreen – current room
local scene

local funtion sceneSelector(newRoom)

local newScene
if(newRoom == room and scene~=nil) then return {scene, room) – (basically do nothing)

if(scene~=nil) then scene:removeSelf() end – remove original if present

if newRoom == StartScreen then
newScene = display.newImage( “StartScreen.png”, 0, 0, true )

else…
end

return {newScene, newRoom}
end

scene,room = sceneSelector(room) – create initial state

local newRoom = Whatever
scene,room = sceneSelector(newRoom) – test new scene[/lua] [import]uid: 6645 topic_id: 4294 reply_id: 13373[/import]