The solution is still the same as what Nick has offered, you need to store these objects in a table. If you use the same variable to store multiple things then you will lose access to all but the last. This will lead to memory leaks.
i got ur point ser so i asign the all same object with the different variable? with the use of table .?.
wat if theres 2 same falling object with the same time sir?
so all the same object are need to assign in a table?
same like this?
- local objects = {}
- objects[1] = display.newImage(“apple.png”)
- objects[2] = display.newImage(“apple.png”)
its not posible to use 1 object only sir ?
same like this :
objects[1] = display.newImage(“apple.png”)
No, that will cause the same problem with being able to access each apple individually and give you memory leaks.
Each time you create a new apple, just add it to the table like this:
[lua]
objects[#objects+1] = display.newImage(“apple.png”)
[/lua]
I’m guessing these apples are falling and you have to tap them or something? If so, within your touch listener you can access the apple that was touched with event.target - so you don’t need to know which number apple it is.
To remove the apple in a touch listener:
[lua]
if event.phase == “ended” then
local obj = event.target
obj:removeSelf()
obj = nil
end
[/lua]
If you need to remove apples as they fall off the screen, you can do something like this:
[lua]
local gameLoop = function () – this function will run every frame (30 or 60 times a second)
for a = #objects ,1, -1 do – loop through all the objects
if objects[a].y > 600 then – check where the object is on screen
--this value will depend how tall your screen is
objects[a]:removeSelf() – remove if off screen
objects[a] = nil
end
end
end
Runtime:addEventListener(“enterFrame”,gameLoop)
[/lua]
If you are not using storyboard then remember to clean up the objects at the end of each game by looping through the table and removing each one. Otherwise each time you play the old apples will remain in memory and slow your game down.
sir actualy in my game im using storyboard…
acording to the object… when theres 2 object with the same varible den i go to mainmenu or restart the game…
ders a 1 object remain…
->actualy i have a memory leak problem…
when i always restart my game the memory usage it add +4 mb to the memory… and i dont know weres the problem…
-> ihave many problem in my game
sir actualy in my game im using storyboard…
Regardless of if you use Storyboard or not you can’t use the same variable for more than one object, you have to use something like tables to manage them.
when i always restart my game the memory usage it add +4 mb to the memory… and i dont know weres the problem…
The problem is that you are using the same variable to store more than one object, this means that when you destroy the object when quitting the level you are in fact only deleting the most recent object and then leaving all the other ones alive.
so all object falling is need to use table?
same like this ser ?
- objects[1] = display.newImage(“apple.png”)
- objects[2] = display.newImage(“manggo.png”)
- objects[3] = display.newImage(“orange.png”)
- objects[4] = display.newImage(“banana.png”)
so all object falling is need to use table?
same like this ser ?
- objects[1] = display.newImage(“apple.png”)
- objects[2] = display.newImage(“manggo.png”)
- objects[3] = display.newImage(“orange.png”)
- objects[4] = display.newImage(“banana.png”)
Yea that’s correct, although if you want to have more than one orange for example you will still need to create the image twice and have two separate objects in the table.
ok sir tnx for a big idea,
my another problem now is SQLITE…
i cant connect … :wacko:
@jaurio
When corona executes
myApple = display.newImage(“apple.png”) – First apple
myApple.x = 100
print(" myApple.x == ", myApple.x)
myApple has an x and y coord (and alpha, xScale, etc)… It’s x and y are unique from all other objects. Changing the x,y of myApple will only change that one object.
Now if you load a second apple later in the same code like this
myApple = display.newImage(“apple.png”) – Second apple
print(" myApple.x == ", myApple.x)
The print will say myApple.x = 0 !
It is because myApple now points to the second apple object that was loaded, not the first apple object. (Your variable is a POINTER to a loaded object, not the object itself)
myApple:removeSelf will now only remove the second one. The memory address of the first apple was lost, because it was overwritten with the second apple memory address. (which is why everyone is saying to make two variables, or use a table of variables, so the memory address where the first apple was stored is not overwritten with the address of the second apple object)
Not sure if this helps you understand what is going on, maybe explaining it this different way helps. Try changing a couple values on the first apple, load a second and see for yourself. Best of luck.
ok tnx papps i understands. .^^
sir how can i set location using this ?
objects[#objects+1] = display.newImage(“apple.png”)
when i use objects[#objects+1].x =50 ; objects[#objects+1].y =50
its error
never mind ser… i got it