Hi guys,
I decided to use JSON to save my game settings between play. I loved PropertyBag but I had to switch because of the complexity of my game settings table. Anyway it works fine for fixed sprites(x,y,health) and things like score and hi-score . My issue is that I discovered that I need to also to save moving sprites (it is space shooter game) The enemies also need to be saved. The problems is that those sprites a spawn randomly so i never know how many of them will be present (alive) just before I exit the game (those sprites get shot at and go away) So I need to save whatever sprites are left on the screen just before the player exit to the main menu. Of course the game needs to read the file back to place those sprites back on the screen…
In any event, I have fixed amount of fixed sprites (3) so it was easy to keep track and save/load those. Something like:
In Main:
[lua]_G.gameSettings.spriteA =
{
{x=0, y=0,vx=0,vy =0,health = 100},
{x=0, y=0,vx=0,vy =0,health = 100},
{x=0, y=0,vx=0,vy =0,health = 100}
}[/lua]
Then in the mainGame module. I reload those like this:
[lua]spriteA = {}
for i = 1,3 do
spriteA[i] = display.newImageRect(“spriteA”…i … “.png”,75,75)
spriteA[i].x = gameSettings.spriteA [i].x
spriteA[i].y = gameSettings.spriteA [i].y
spriteA[i].health = gameSettings.spriteA [i].health
end [/lua]
BUT what to do with with spriteB when I do not know in advance how many I will need to save before end. SpriteB will also be a table with each element having an x,y,vx and vy (no health)
I tried for i = 1, #spriteB do… but it does not seems to work or I may not have save the spriteB table correctly.
I tried this to save (JSON) the spriteB’s location and speed:
[lua]-- in main game screen in the exit to main menu button method
– (spriteB [i]x,y,vx,vy are the current –
– location/speed of each spriteB on the screen just before the user --exit the game.
for i = 1,#spriteB do gameSettings.spriteB[i].x = spriteB [i].x
gameSettings.spriteB[i].y = spriteB [i].y
gameSettings.spriteB[i].vx = spriteB [i].vx
gameSettings.spriteB[i].vy = spriteB [i].vy
end
data.saveSettings()
director:changeScene(“menuScreen”,“downFlip”)
–…[/lua]
In the main.lua I have (update from the _G setting above)
[lua]_G.gameSettings =
{
[“spriteA”] =
{
{x=0, y=0,vx=0,vy =0,health = 100},
{x=0, y=0,vx=0,vy =0,health = 100},
{x=0, y=0,vx=0,vy =0,health = 100}
},
[“spriteB”] = {} – I also tried {x=0,y=0,vx=0,vy=0}
}[/lua]
I am stuck at this time.Most of the time I get a “encoding of userdate: 01E5D218 unsupported” runtime error message. To complete the description, I should say I am using the data.lua module which is used to save and load settings. I am pretty sure that I have problem with saving the spriteB table into gamesSettings.
I have no idea if I made any sense! But if I do, I would really appreciate some pointers on how to add my sprites into spriteB table.
THANK YOU!
Mo
[import]uid: 49236 topic_id: 15517 reply_id: 315517[/import]