Director Class + Lime + Memory Management

I've been wanting to use the director class to control which map or level is loaded in my game.

http://developer.anscamobile.com/code/director-class-10

With the director class, you add objects to a group which gets deleted from memory when you transition out of a scene. I've been able to get maps to load using the director class:

function new()
local localGroup = display.newGroup()

-- Load Lime
local lime = require("lime")

-- Load your map
local map = lime.loadMap("maps/tutorial0.tmx")

-- Create the visual
local visual = lime.createVisual(map)

localGroup:insert(visual)

return localGroup
end

Question:

I know that the "visual" object will be removed from memory when localGroup is deleted. What happens to "map" and "lime" should I be worried about this? Anyone have any tips on integrating lime with the director class. My understanding of corona memory management is a bit weak.

Thanks,

m

I'm very interested in this too. My next mountain to climb! Thanks for sharing any success you have in this integration.

I haven't looked into Director class fully yet (who has time to make games? :-) ) but I would assume that it would destroy everything in your screen files thus including your map data.

What you could possibly do is create a sort of map manager class that would then get called from your screen i.e. mapManager:getMap("map.tmx") and then in the manager the first time that function is called it would load the map and store it and then in each subsequent call you the function would know that it is already loaded and then pass it back to you.

Does Director have an onShow and onHide function?

I have started too try out using director with Lime for figuring out how too control levels and such.
Everything is working good up until I have too go back too the menu. Nothing happens when I click the button set up too go back too menu:

local function touchedmenu ( event )
if event.phase == "ended" then
director:changeScene("menu","fade")
end
end

menuBtn:addEventListener("touch",touchedmenu)

I know the button works cause it executes other things I put in there.. so its the changeScene that dosent work, at least thats what it looks like too me.

Other thing is that when I put objects into groups they disappear.

local localGroup = display.newGroup()
local menuBtn = display.newImage ("menu.png")
localGroup:insert(menuBtn)

then I have the return localGroup at the end

Im new at coding so its all abit touch and go now in the beginning, any help or pointers would be greatly appreciated :)

Aaah, nevermind the question about the back too menu button, the problem was ofcourse that all the gfx was still there and over the menu buttons...

so guess the question is about the localGroup and how too unload all the graphics :)

I actually just started creating a little demo/viewer app that I intend to release on the app store so people can see Lime in action before buying and it uses Director. It's all very basic at the moment but if you want I will post a link to it so you can have a look at the code?

yes plz!

Graham, I think a demo app on the store is a great idea to get more developers to take a look at your lib. I would love to see your code. Please please link it up! Thanks & good luck!

I figured out the grouping issue.. after reading up on it I found out that it puts the object that is put in group too the back of the stack, so I just had too put everything in the group and they display correctly and go away when tocuhed menu.. now I just need too find out why the screen is black when I return too i :) probably some small thing I have overlooked, hehe

I love looking at examples, thats mostly how I learn things :)

Please note, the app is VERY basic :-)

I have added a link to here - http://justaddli.me/members/downloads.php

I will soon rearrange that page to work better as more things get added to it.

Thanks for uploading the example, will take a closer look at it today. I tried running it once yesterday but I could only get too the second screen where you choose wich map too load.

When it comes too my little director project its moving along. I can get too the menu again now from a game level. But when I go back too the game the character moves alot faster.. I have used most of the code from your tutorial on how too make characters move and jump. and as far as I can see its the code part that sets the speed and makes it move:

local DIRECTION_RIGHT = 1

local onButtonRightEvent = function(event)

if event.phase == "press" then
player.direction = DIRECTION_RIGHT
player.xScale = player.direction
player.state = STATE_WALKING
else
player.state = STATE_IDLE
end

player:prepare("anim" .. player.state)

player:play()
end

local onUpdate = function(event)

if player.state == STATE_WALKING then

player:applyForce(player.direction * 7, 0, player.x, player.y)

elseif player.state == STATE_IDLE then

local vx, vy = player:getLinearVelocity()
if vx ~= 0 then
player:setLinearVelocity(vx * 0.9, vy)
end
end
end

Runtime:addEventListener("enterFrame", onUpdate)

not sure why it would become even faster after loading the map up again but there must be some variable that dosent get deleted but stays on? will dig into your example and maybe find some answeres there. If anyone sees the mistake I have made it would be great too know :)

Cool demo Graham, thanks for sharing.

No problem, hope it helps someone.

Hy Graham,

great demo indeed. I have a question. I'm a noob to Lua and I don't get how the (as it seems) global table _G["map"] is filled with the correct map to be loaded. is this _G a built-in Lua table? if not, who declares it?

another question: I can't seem to find where the physics.start() is done. screen_map.lua is where the map is loaded and the listeners are called. but where is the physics started!??

many thanks!

_G is indeed a global table created by the Lua environment, I don't like using it at all however this was a "good enough" type solution.

When the map is built via lime.buildMap() Lime will check if the physics world has already been created and if not it will do it for you.

Works great on this end. Thanks!

Graham, please take a moment to look at the new Director 1.2. Your demo app seems to be crashing when you return to the main menu screen after exiting a map.

Yep - same here...

Oops I didn't even realise that 1.2 was out. I am currently away from my PC as I was meeting a client today but will look when I get back. Sorry for the inconveniance.

No problem - thanks Graham :-)

No problems indeed. Take your time.

Hey Graham, See the following for changes in the Director

http://developer.anscamobile.com/forum/2011/02/02/any-idea-when-v12-might-come-out#comment-22751

Hope this helps.

Here is what I have found, if it helps ...

The viewer code that Graham sent out had what looked like ver 1.1 of director
But it is different than the 1.1 from Ricardo ?????

Ver 1.1 from Graham (with viewer) - THIS WORKS

------------------------------------------------------------------------
-- LOAD SCENE
------------------------------------------------------------------------

local function loadScene ( nextScene )

if currentScreen then
currentScreen:onHide()
end

nextScreen = require(nextScene).new()
nextView:insert(nextScreen)

end

Ver 1.1 from Ricardo - causes ERROR, as does file with ver 1.2

------------------------------------------------------------------------
-- LOAD SCENE
------------------------------------------------------------------------

local function loadScene ( nextScene )

nextScreen = require(nextScene).new()
nextView:insert(nextScreen)

end

Here is the first error caught...

Runtime error
...xCETaaQfpBhUg6xk+++TI/TemporaryItems/15/lime-map.lua:807: attempt to perform arithmetic on field 'x' (a nil value)
stack traceback:

Hope that helps,

Peter

Changing the following two lines seems to make lime work with director 1.2:

lime-utils.lua (80):

1
        return worldToGridPosition(map, {x=position.x - (map.world.x or 0), y=position.y - (map.world.y or 0)})

I had put in a small change just to allow me to deal with cleaning stuff up before the scene was destroyed by Director but looking at the link from ksan it looks like Ricardo has added in a clean() function that will do exactly what I want.

Yup, all fixed now. Just change the two onHide functions declarations (in screen_Map and screen_MapList) to "function clean()" and all will be sorted.

The new version of the viewer app will be uploaded soon as well as included with Lime 2.8

Awesome - sounds good Graham!

Working great now! Thanks much!!!

Awesome, thanks for checking and getting back to me!

views:2241 update:2011/10/13 16:39:51
corona forums © 2003-2011