Hi all!
Can anyone tell me what can I do to literally kill a level? I want to restart a level, tried everything and the only thing that makes me go into here is when I tried everything and nothing absolutely nothing works.
I tried to collect garbage, no works
I tried to change where some sprites must start, (self, stage) just got trouble.
Is there a thing that I didn't try? Is there a thing that I still don't know?
I am since morning thinking hard and I don't know what to do anymore.
I have a level, and in this level I add a table with sprites in it. Inside a sprite, there is another sprite (bullets) that I load and inside this sprite it is generated inside 'stage'. It is working until I'm getting to restart the level... when the level is 'restarted' the sprites just gets invisible and 'shooting', it doesn't disappear and I'm getting hardly frustrated of how I can't understand how it works...
What I want is simple. Restart a level and kill everthing that has in it.
Comments
Here is an example that creates a table of sprites, each with random numbers of child and grandchild sprites.
The table is then iterated and all sprites recursively removed.
In your own code you would have to remove any event listeners that each sprite might have (although in a perfect world they should not have any besides touch listeners if required).
Once all of the sprites have been removed, if there are no references to them anywhere else in your code, they should be garbage collected.
Likes: Apollo14
I guess function 'killChild(sprite)' would better be global?
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
To load my sprites, I'm doing this way:
Is that a wrong way to do?
GameScene = Core.class(Sprite)
function GameScene:init()
# do some staff
end
In main.lua define your scenes
scenes = {"menu", "game"}
sceneManager = SceneManager.new({
["menu"] = MenuScene,
["game"] = GameScene,
})
to change to GameScene
sceneManager:changeScene(scenes[2]);
it will create new GameScene from scratch.
Likes: Apollo14
@jdbc That would work I suppose but if the scene to be discarded contains persistent game data, it would also be recycled. The best approach is most likely to manage the resources internally in the scene.
@Paulo777 So at the start of the scene you create 5 gears and then as counter increments you add gears to the stage at preset times right? If it works, then its fine. You can reiterate later and optimize it. So you could use something like this for your project..
Take a look to the source code of my complete game. It has ads, social, leaderboard, scenemanager...
https://github.com/jdbcdev/Dots
All my scenes, ads and social manager, leaderboard are just classes. Easier and clean code.
Do not use global variables or functions to keep game state, it is really difficult to keep it.
Likes: Paulo777, Apollo14
Likes: Paulo777
Likes: Paulo777, antix
I find it pretty easy to manage global vars myself. I only save to the device when the user decides to (with options screens and stuff).
I use classes for pretty much everything too just because it makes things easier to manage (20,000+ lines of code in one file is daunting!) and it makes things easy to reuse in future projects.
I noticed that in the key events you are using stopPropagation() which the Gideros documentation specifies is only required for mouse and touches.. unless the documentation is wrong?
Likes: jdbc
Likes: pie, Apollo14, Paulo777, keszegh, vitalitymobile, antix
Dislikes: oleg
@hgy29 that'd be great! I'd love that, hahah
http://docs.giderosmobile.com/reference/gideros/Event/stopPropagation#Event:stopPropagation
Where did you see that is only for Touch or Mouse events? May be key events does nothing calling this method but it is working this code.
SceneManager creates a new instance of your GameScene class when changes the current scene, I guess the previous GameScene instance is destroyed and all listeners disabled. May be we could check SceneManager source code to make sure about it.
One way to make this work is to have a bool for each gear that lets the game logic know if it is alive or dead (should be processed or not).
Ie.
Scenes:
Game
Dummy
When you are in Game and need to restart it changeScene to Dummy. In Dummy set the code to changeScene to Game. It should work.
If I remember, the issue in changing scene to itself is that you never stop referencing scene vars and objects, and gc does not collect everything properly.
Hope this helps
Likes: Apollo14
Still, it's pretty silly to just start a new blank scene, then start a new copy of the scene you want. In my opinion you really need to learn how to reset stuff without resorting to such methods
Likes: jdbc
Likes: antix
self:removeEventListener(Event.ENTER_FRAME, self.onDetectCollision, self)
You are removing a wrong EventListener, it should be just
self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame)
and this?
stage:removeEventListener(Event.ENTER_FRAME, self.shot.onFire, self.shot)
why are you using stage instead of self (current scene)
You have to stop music and removelisteners before change scene (reset same scene),
for instance:
You have to stop music and removelisteners before change scene (reset same scene),
for instance:
This when? Level? Ship? Gear? all? Is this functions to add?