Noobish questions :D

5 replies [Last post]
H4ch1
User offline. Last seen 22 min 52 sec ago. Offline
Joined: 27 Aug 2010

Hi there,
I'm new to Corona, and apart from a little C, I'm new to coding :°
I got the hang of this, but I'm still a little unsure.
How do you save?
I mean, if I want to make a game where you can unlock levels as you play through the game, how can I save the results of the player when he quits the app?
I thought about writing the values in a txt, but even though I looked in the doc, I'm not sure at how I can, for example, find the "levels unlocked" words in my doc, and replace the number near it, or read that value and changing a value with that number.
Or is there another way to save?

Then again, if I create some objects within a loop, all those objects will end up with the same "name".
How can I track if one of those object isn't on the screen?
I thought about having an object with isSensor=true, and using the collision, event.other thing to remove event.other.
Is there a better way?

Replies

tetu
User offline. Last seen 5 days 14 hours ago. Offline
Joined: 3 May 2010

Hi H4ch1,

You might find useful this post by Gilbert for saving data: http://developer.anscamobile.com/code/save-and-restore-state

Suppose that we create 10 fruits with the loop:

1
2
3
for k=1,10 do
         local fruit=display.newImage("fruit.png",0,0)
end

fruit is a variable name and not the object's name. using the same variable (fruit) we created 10 objects that do not have a name. if you have to give each fruit a name, you can assign a "name" property (or any other) to it
1
2
3
4
for k=1,10 do
         local fruit=display.newImage("fruit.png",0,0)
         fruit.name="fruit"..k
end

i find it very convenient to store the fruits in an array

1
2
3
4
5
local fruits={}
for k=1,10 do
         local fruit=display.newImage("fruit.png",0,0)
         fruits[#fruits+1]=fruit
end

from now on, whenever i want to refer to the 7th fruit, i use fruits[7]

an object isn't on the screen (assuming its rotation is zero) if

1
object.x-object.width/2<=0 or object.x+object.width/2>=display.stageWidth or object.y-object.height/2<=0 or object.y+object.height/2>=display.stageHeight

H4ch1
User offline. Last seen 22 min 52 sec ago. Offline
Joined: 27 Aug 2010

Thanks a lot tetu!
I'm looking in that save sample.
About the objects offscreen, I already knew what you said, my concern was more about how can I keep track of each one of them.
I mean, should I use an enterFrame listener? Calling a function with for loop that check if anyone of the object in my array is offscreen?
Wouldn't this be heavy for performance?

tetu
User offline. Last seen 5 days 14 hours ago. Offline
Joined: 3 May 2010

you are welcome!

you can store thousands of values in the text file (not just the level), and retrieve them when the application starts

it depends on how many and what kind of objects you want to track
an enterFrame listener is not a bad idea if you have few objects, but if you have hundreds of them timer.performWithDelay might be more efficient

check the performance on the device not the simulator

H4ch1
User offline. Last seen 22 min 52 sec ago. Offline
Joined: 27 Aug 2010

Thanks tetu! I'll try :D

H4ch1
User offline. Last seen 22 min 52 sec ago. Offline
Joined: 27 Aug 2010

I've encountered another problem.
From the documentation, it says that a param of transition is onComplete, that activates when the transition is over.
I wanted a function to be called after the transition, but it seems that it's called right when the transition start.

This is the little code:
for i=1, scene1.numChildren, 1 do
transition.to (scene1[i], {500, alpha=0})
end
transition.to (bg[1], {500, alpha=0, onComplete=loadNew()})
end

and then the loadNew() function change the alpha of another group from 0 to 1.
If I don't set a delay in the other transition (the one stored in loadNew), the fadein/out happens at the same time, and not when the first transition is complete.
Everybody knows why?

EDIT: Solved. Seems that with onComplete, when calling a function, I must not pass a target, or else it would fire immediately. So writing onComplete=loadNew solved everythings :D

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.