sexta-feira, 1 de julho de 2011

Director Class 1.3

Hello folks,

Today I'm releasing the new Director Class 1.3. As you could see on the preview video, there are some cool stuff on this version that I will explain on this post, but first the link to download it:

https://bitbucket.org/ricardorauber/corona/downloads/director_by_ricardo_rauber-1_3.zip

Almost completely rewrite

I did so many changes that the code it's way different of the other versions. You will find that it's clean and easier to understand.

More consistent

The older versions were made based on timers, I changed that to execute transitions/fxEnded on the onComplete parameter of the transition.to function as the example below:

Before:

showFx = transition.to ( currView, { x=display.contentWidth*-1, time=fxTime } )
timer.performWithDelay( fxTime, fxEnded )

After:

showFx = transition.to ( currView, { x=-_W, time=fxTime, onComplete=fxEnded } )

Function cleanGroups() removed

There is no need to keep this function because Ansca changed the removeSelf() method to be recursive, so now the scene groups are cleaned only with the removeSelf().

Call to clean() function removed

On Director 1.2 I put a call to a clean() function if it exists on the scene that was going to be removed. That was a good thing but why did I removed it from Director? Just because a lot of people didn't understand how to use it properly. I received A LOT of e-mails asking about this and almost 90% were about wrong name and commands (i.e: function Clean() ... ). You can still create a clean() function but its up to you to call it before change scenes.

Scenes = Objects

Director works with display groups but I did a mistake on version 1.2 writting a bunch of code outside the new() function. On this version, I'm trying to go back on this step and put everything back inside the new() function. Why? It will be needed to reload the same scene. Display objects are Lua tables with some more data, when you create a new table you are creating a new instance that we could understand as an object. You can add attributes and methods to a display group and treat it like a real object oriented programming, like this:

local obj = display.newGroup()
--
obj.rect = display.newRect( 0, 0, 200, 200 )
obj:insert( obj.rect )
--
obj.changeColor = function ( r, g, b )
  obj.rect:setFillColor( r, g, b )
end

Reload Scene

After some research and assumption that every scene is an object, it was fine to reload the same scene. Now you can use changeScene() to load the same scene you are using but be sure that it's an object or some weird errors will occur.

Protection

I created an invisible protection (square) to prevent unwanted touches on scenes during the transition. It becomes active when you call changeScene() and turns off after fxEnded(). If you really need to disable this, just cut off the event listener on line 218.

protection:addEventListener( "touch", fncProtection )

Security and error handling

This is a great add-on, now every new scene will be called using PCALL function to execute it on a secure mode and Director will be able to tell you if some error happens. This is another think that programmers sent me a lot of e-mails. Good developers take a deep look on their code before anything when an error occur, other guys simply goes crazy telling everyone that the error is on Director, never on their code. The thing is that 99% of these errors are mistakes of the developers and take sometime to they see it. Now if a scene has errors, Director will show an error message on the simulator and device.

Pop Up

You want to call a pause scene during a game without clean the scene? You can use now the new pop up feature. The openPopUp() function loads a scene on the top layer, turns on the protection, doesn't clean the current scene and can be closed with the closePopUp() function. The only restriction is that you can only load 1 pop up, if you want other pop ups, you need to close it first.

Parameters

Now you can send parameters between scenes! It's very simple, you only need to put it in a table, check this out:

local params = { label="Sending Parameters", reload=true }
director:changeScene( params, "screen2", "fade", "white" )

Parameters are optional, you can keep changing scenes without parameters:

director:changeScene( "screen2", "fade", "white" )

Folders

This is not on Director itself but about the sample of the version 1.2. I did it using folders for images but this is not good. Ansca recommends that we put all our files on the same folder. I don't like this but I had some problems with it on Android builds so I changed the sample and put all together.

Help us with a donation

I never put a price on Director and I don't want to do this but everybody needs money, if you want to help me keeping it free, you can donate any value on the donate button here. Thanks a lot for all that already donated!






18 comentários:

  1. Is there a way to immediately get a reference to the new scene that is being created / required? Not necessarily the group that was returned from the new() call, but the module? If not is there way to have a function provided as a callback when I can get it? Maybe even have it come as a param in the callback? Thanks! I am just starting into lua, but I will be sure to donate if I find myself using the director class.

    By the way, nice to see the +1 button here already :)

    ResponderExcluir
  2. For now you only can do it by yourself inside the new() function but I like your idea and will put it on the roadmap for Director 1.4.

    Thanks

    ResponderExcluir
  3. You can also use:

    local myVar = package.loaded[moduleName]

    ResponderExcluir
  4. Ah ya, thats a tricky way to get it :) but I suppose that would work. Is it available immediately after the call to changeScene()? By the way, thanks for replying so quickly!

    I am coming from using Flash since version 3, so its going to be odd not expecting things to happen the way they do in actionscript :)

    ResponderExcluir
  5. Yes, right after the scene change you can use it.

    I used to work with Flash 4, 5 and MX, good times!

    ResponderExcluir
  6. Director is pure awesomeness. It makes my code and games so much more organized. I just downloaded 1.3 and going to try it right now.

    ResponderExcluir
  7. I had been using Director 1.2.
    But I am having trouble with continuos background music.
    upon switching scene, Director will clean out the music.

    How to keep music playing within the Director framework.
    Cheers
    V

    ResponderExcluir
  8. I wanted to keep the clean call, but after the the scene transitions out. I added:

    local cleanFunc = package.loaded[moduleName].clean or function() end
    pcall(cleanFunc)

    in the unloadScene function, just before:

    package.loaded[moduleName] = nil

    Is that an ok way to do it? I need clean to be able to manually remove widgets. But it needs to wait till after the transition if I want to see the animation. Thanks!

    ResponderExcluir
  9. @hktrends

    Just put the audio on the main.lua file or on other module that isn't a scene.

    @Shawn

    Yes, that's it!

    ResponderExcluir
  10. I really like Director and I just tried Director 1.3 with my current project, and had great difficulties with adapting my project to it. The problem is that I am building something like a story book, where I add some menu items etc to each scene after it's loaded (so I don't have to do this in each scene), and I also have a script engine that interacts with objects from the current scene, so I don't have to program animations etc in each scene. With the previous version, I could refer to objects like this: director.currentView[1].myObject - and life was good!

    I read the earlier comments, and tried to assign the current scene to a global variable in the new() function and then use this variable instead of the director.directorView, but it does not work well. Simpler operations works OK, but not things like adding new objects to the group, using the "Beebe games" module with reference to the group etc.

    Seems like the new version adds a lot of good stuff, but it also seems like it is limiting the possibilities we had with the older version. Maybe I am just doing things wrong... I can send you a code example if you like.

    ResponderExcluir
  11. I took a second look, and found ways to adapt my code to work with the new version. I had to change the reference to the currently loaded scene in quite a few places, had to re-think the menu objects so that they are now added to each scene after it loads, and had to stop using "Bebee games" functions when referencing a scene after it's been created. But now everything is working fine, and I really like the new popup function - thanks!

    +1 on re-adding the "clean" call, it's a must I think (although it was easy to add as described in a previous comment).

    ResponderExcluir
  12. is there a popup example working?

    ResponderExcluir
  13. Thanks for this awesome package!

    ResponderExcluir
  14. Thank you very much for the director class. It's amazing.

    Very new to Lua, having trouble changing scene when video ends. I wonder if my mistake is obvious to you? Thank you for any help you can give. I'm sure you're very busy. See:

    http://developer.anscamobile.com/forum/2011/08/18/changescene-not-working-video

    ResponderExcluir
  15. Amazing class. Thank you for sharing.

    I am trying to build off of your class and add a callback to the new scene after transition. So far, the only way I've figured out how to do it is by placing a function outside the new functions and calling

    if package.loaded[currScene].afterFXCallback then
    local endFXFunctionName = package.loaded[currScene].afterFXCallback
    pcall( endFXFunctionName )

    end

    in fxEnded. Thoughts?

    ResponderExcluir
  16. I am living in Albama (US) But mostly I use to call Bangladesh for my personal as well as for business purpose. So can anyone suggest me how to make cheap calls to Bangladesh like this advertisement http://www.reddit.com/r/technology/comments/1a9w0q/call_bangladesh/

    ResponderExcluir
  17. I am living in London(uk)But mostly I use to call Bangladesh for my personal as well as for business purpose. So can anyone suggest me how to call Bangladesh on a low call rate , It would be help full for me.

    ResponderExcluir