Using values on a flag instead of "true" or "false" ?

Hi everybody!

Is it possible to add values to a global flag?
So instead of just having two function, true or false, you can have a lot of functions. Here is a example what i mean:

if _G.FLAG = 1 then
local 1 = display.newImage ("1.png)
1.x = 250
1.y = 250
if _G.FLAG = 2 then
local 2 = display.newImage ("2.png)
2.x = 250
2.y = 250
if _G.FLAG = 3 then
local 3 = display.newImage ("3.png)
3.x = 250
3.y = 250

Is there a function similar to this code? And if it is so can you please let me know and wrote the code down, or send me a link to a good tutorial on this.(Because i doubt that the code above is working XD). And how do i set my FLAG to 1,2 or 3 using a collision or a button?

Thanks in advance:)!

//Philip [import]uid: 119384 topic_id: 22637 reply_id: 322637[/import]

Try this

  
image = display.newImage(\_G.FLAG .. ".png")  
image.x = 250  
image.y = 250  
  

If you need all three images you might want:

  
images = {} -- make a table/array  
for i = 1, 3 do -- or how many ever you need  
 images[i] = display.newImage(i .. ".png")  
 images[i].x = 250  
 images[i].y = 250  
 images[i].isVisible = false  
end  
images[\_G.FLAG].isVisible = true  

btw, you probably can’t have object/variable names that are just numbers. [import]uid: 19626 topic_id: 22637 reply_id: 90258[/import]

Okey, i didnt understand that :confused: But lets say im about to add a lot of functions to one of the number, instead of just a picture. How do i do that? And how do i change to a different “number/value” using a button (event listener).

Example:

Level1 = 1(value)

*Then i click a button*

Level1 = 2(value)

Please leave some notes that will describe the code you are whriting ^^

//Philip [import]uid: 119384 topic_id: 22637 reply_id: 90292[/import]

@Baronen,
you need to make note that in Lua there is a difference between = and ==

so name = “Philip” is assignment

and

if name==“Philip” is for checking conditions.

if you fix that, your code would start to work. Then you can look at other options to optimise it further.

Jayant

@ozapps | howto.oz-apps.com | reviewme.oz-apps.com [import]uid: 3826 topic_id: 22637 reply_id: 90354[/import]

Thanks! :slight_smile: But there is a little problem. When i’m running my test-app using the code below it let me change from Locked to Unlocked(when im clicking the BTN). But when i click on BG it wont change to Clear. I have try to fix this code for a long time now and i cant manage the last part. When i touch BG, Locked will be removed and Clear should be displayed instead, but that doesnt happen. I’m getting an error in line 42 but I really cant se the problem:/

Here’s my code:

[lua]local BG = display.newImage (“background.png”)
BG.x = display.contentWidth /2
BG.y = display.contentHeight /2

local BTN = display.newImage (“Unlock.png”)
BTN.x = display.contentWidth /2
BTN.y = 300

_G.button = 1

if _G.button == 1 then
Locked = display.newImage(“Level_Locked.png”)
Locked.x = display.contentWidth /2
Locked.y = display.contentHeight /3
end

local function changeImage ()
if _G.button == 2 then
local UnLocked = display.newImage(“Level_Playable.png”)
UnLocked.x = display.contentWidth /2
UnLocked.y = display.contentHeight /3
end
end

local function UnLock (event)
if event.phase == “ended” then
_G.button = 2
Locked:removeSelf()
changeImage()
BTN:removeEventListener(“touch”,UnLock)
end
end

local function changeIt ()
if _G.button == 3 then
local Clear = display.newImage(“Level_Clear.png”)
Clear.x = display.contentWidth /2
Clear.y = display.contentHeight /3
end
end

local function Comp (event)
if event.phase == “ended” then
_G.button = 3
UnLocked:removeSelf()
changeIt()
BG:removeEventListener(“touch”,Comp)
end
end

BTN:addEventListener(“touch”,UnLock)
BG:addEventListener(“touch”,Comp) [import]uid: 119384 topic_id: 22637 reply_id: 90944[/import]

My first observation is that you never do a “return true” on any of your listeners. This could result in your touch event going to your button THEN going to your BG. When a touch makes it to your background, you remove the touch handler for the BG.

So if you accidently touch the BG, it won’t respond to touches and your button handlers are also likely passing the event on.

Add a:

return true

to each of your touch handler functions.

consider removing the removeEventListener in the BG handler.
[import]uid: 19626 topic_id: 22637 reply_id: 90951[/import]

Thank you, but it still doesnt work:/ And this app is just a test app and i will add this code into my game that im working on. And in that game i will bind the “level clear” function to a collision not to the background. So it really doesnt matter if someone accedently touches the BG in this app. But have you any other ideas how to get this thing to work:)?

Thanks in advance :slight_smile:

//Philip [import]uid: 119384 topic_id: 22637 reply_id: 91081[/import]

Try replacing this:

\_G.button = 1  
   
if \_G.button == 1 then  
Locked = display.newImage("Level\_Locked.png")  
Locked.x = display.contentWidth /2  
Locked.y = display.contentHeight /3  
end  
   
local function changeImage ()  
if \_G.button == 2 then  
local UnLocked = display.newImage("Level\_Playable.png")  
UnLocked.x = display.contentWidth /2  
UnLocked.y = display.contentHeight /3  
end  
end  

With this:

\_G.button = 1  
local Locked  
local UnLocked  
   
if \_G.button == 1 then  
Locked = display.newImage("Level\_Locked.png")  
Locked.x = display.contentWidth /2  
Locked.y = display.contentHeight /3  
end  
   
local function changeImage ()  
if \_G.button == 2 then  
UnLocked = display.newImage("Level\_Playable.png")  
UnLocked.x = display.contentWidth /2  
UnLocked.y = display.contentHeight /3  
end  
end  

Jeff

[import]uid: 14119 topic_id: 22637 reply_id: 91094[/import]