Variables and Actions

Hello all,
I am trying to get my game to stop when a certain amount of objects are caught but It doesn’t seem to be working. Here is the basics of my code…

local n = 0 -- the variable  
n = n + 1 --when you catch an apple  
if lives == 1 then --The lives are working fine and the game ends when I run out of lives   
noMoreApples()  
pauseBtn.isVisible = false   
pig.isVisible = false  
elseif n == 4 then --But with the same logic it doesn't work  
noMoreApples()  
pauseBtn.isVisible = false   
pig.isVisible = false  

I have a command to zero out the “n” variable which is just “n = 0” in the end game logic and it works because I have it printing to the terminal and it restarts every time. The apples won’t stop falling when it reaches 4 apples.

Danny [import]uid: 59140 topic_id: 30512 reply_id: 330512[/import]

@Dannylego ,

What about putting it into a loop as the example below:

[lua]local n = 0 – the variable

for n = 0, 4 do
if lives == 1 then --The lives are working fine and the game ends when I run out of lives
noMoreApples()
pauseBtn.isVisible = false
pig.isVisible = false
elseif n == 4 then --But with the same logic it doesn’t work
noMoreApples()
pauseBtn.isVisible = false
pig.isVisible = false
n = n + 1 --when you catch an apple
end
end[/lua]

PS: Remember that it will loop 5 times as you`re starting the n as zero.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 30512 reply_id: 122250[/import]

Thank you so much! The only problem I noticed was that it seemed to be selecting a random number between 0 and 4 rather than stoping at 4 all the time. Sometimes it would end at 3, other times 2. [import]uid: 59140 topic_id: 30512 reply_id: 122252[/import]

Glad to help (or give some tip at least). :slight_smile:

But by now I have no idea why it isn`t firing only at the 4. Humm…will think of it, sorry.

Rodrigo. [import]uid: 89165 topic_id: 30512 reply_id: 122254[/import]

@Dannylego ,

What about putting it into a loop as the example below:

[lua]local n = 0 – the variable

for n = 0, 4 do
if lives == 1 then --The lives are working fine and the game ends when I run out of lives
noMoreApples()
pauseBtn.isVisible = false
pig.isVisible = false
elseif n == 4 then --But with the same logic it doesn’t work
noMoreApples()
pauseBtn.isVisible = false
pig.isVisible = false
n = n + 1 --when you catch an apple
end
end[/lua]

PS: Remember that it will loop 5 times as you`re starting the n as zero.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 30512 reply_id: 122250[/import]

Thank you so much! The only problem I noticed was that it seemed to be selecting a random number between 0 and 4 rather than stoping at 4 all the time. Sometimes it would end at 3, other times 2. [import]uid: 59140 topic_id: 30512 reply_id: 122252[/import]

Glad to help (or give some tip at least). :slight_smile:

But by now I have no idea why it isn`t firing only at the 4. Humm…will think of it, sorry.

Rodrigo. [import]uid: 89165 topic_id: 30512 reply_id: 122254[/import]