Code structure in Storyboard API

Hi, everyone! I’am quite newbie to Corona SDK. 

Now I’am working at my first game. I’am using Storyboard API.

In different tuts and video I noticed that people use different methods to structure the game code:

Method A (put variables right into the scene:enterScene(e) function). Simple code for example:

function scene:enterScene(e) local view = self.view; logo = display.newImageRect( "logo.png", 500, 500) view:insert(logo) end

Method B (first declare some function to create logo and the put it into scene:enterScene(e) function). Simple code for example:

local function setupLogo() logo = display.newImageRect( "logo.png", 500, 500) logo.x = \_W logo.y = \_H end function scene:enterScene(e) local view = self.view; setupLogo() end

And the question is - " What is main difference between this two types of code structure?".

Does it have any effect on perfomance or on memory usage and etc.?

Thank you in advance!

First, you probably should be creating things like the logo in the createScene() function and not enterScene().  When you do that in enterScene() they won’t happen until after the scene transition happens so if you’re using any transitions between scene’s they won’t show any thing if you delay creation until enterScene.

Now to your question.  It’s about code organization. Functionally B is about four machine instructions more expensive, but at the CPU speeds of today’s devices, then it’s pretty irrelevant.  

If you only have a hand-full of items that need created, its okay to do it all in the createScene/enterScene functions.  But when the code gets longer and more complex, it makes sense to start creating functions that do specific tasks.  For instance I’m working on a tower defense game at the moment.  My code to create my creeps is in a function outside of the scene functions because it needs to be called multiple times and is pretty long.

But all the code to draw the background, text objects and other bodies on the screen happen in the create function.

Rob 

First, I want to thank you Rob for so fast and clear reply.

I made mistake in my description. Of course I will create my logo, background and UI objects in createScene().

I don’t know is it OK to ask you another question in this topic or I must start new topic :slight_smile:

If your question is about this topic, ask it here.  If it’s about something else, you should start a new thread.  We do this so people searching for things can find them.  Lets say there is a thread about in-app purchases, but it ends up talking about ads.  If someone search for something on ads and they see a subject “In-App purchases” they won’t open the thread because it’s not what they are looking for.  The other side of that is people actually looking for “In-App purchases” will think they found their answer, only to find out that the thread turned into talking about ads.

Hope that helps you decide if you need a new thread or not.

Rob

First, you probably should be creating things like the logo in the createScene() function and not enterScene().  When you do that in enterScene() they won’t happen until after the scene transition happens so if you’re using any transitions between scene’s they won’t show any thing if you delay creation until enterScene.

Now to your question.  It’s about code organization. Functionally B is about four machine instructions more expensive, but at the CPU speeds of today’s devices, then it’s pretty irrelevant.  

If you only have a hand-full of items that need created, its okay to do it all in the createScene/enterScene functions.  But when the code gets longer and more complex, it makes sense to start creating functions that do specific tasks.  For instance I’m working on a tower defense game at the moment.  My code to create my creeps is in a function outside of the scene functions because it needs to be called multiple times and is pretty long.

But all the code to draw the background, text objects and other bodies on the screen happen in the create function.

Rob 

First, I want to thank you Rob for so fast and clear reply.

I made mistake in my description. Of course I will create my logo, background and UI objects in createScene().

I don’t know is it OK to ask you another question in this topic or I must start new topic :slight_smile:

If your question is about this topic, ask it here.  If it’s about something else, you should start a new thread.  We do this so people searching for things can find them.  Lets say there is a thread about in-app purchases, but it ends up talking about ads.  If someone search for something on ads and they see a subject “In-App purchases” they won’t open the thread because it’s not what they are looking for.  The other side of that is people actually looking for “In-App purchases” will think they found their answer, only to find out that the thread turned into talking about ads.

Hope that helps you decide if you need a new thread or not.

Rob