How can i transition from storyboard to physics??
do i put in each storyboard new scene the
local physics = require(“physics”)??
and where do i put the physics codes?
do i put it in the function scene:createScene?? or enterScene??
Thanks
How can i transition from storyboard to physics??
do i put in each storyboard new scene the
local physics = require(“physics”)??
and where do i put the physics codes?
do i put it in the function scene:createScene?? or enterScene??
Thanks
Hi
I have 16 modules in my game so far and I’ve found it best to require the physics in main.lua only.
I don’t use local physics = require(“physics”) I drop the local and make it global, that way you don’t have to put it at the top of every module or remember to do it.
So my main.lua starts like this…
storyboard = require ("storyboard") local scene = storyboard.newScene() physics = require("physics") physics.setScale(30)
I have found that I only ever start the physics in enterScene
physics.start()
If I need physics in that module that is, not all my modules need a physics engine.
My approach might be bad practice I don’t know, I gave it some thought and it seems perfectly ok to make physics global, after all it is part of Corona just like storyboard is.
Notice how I made storyboard global at the top of my main.lua aswell?
I store player and game variables in it throughout my game like this…
storyboard.state.level = 1
local level = storyboard.state.level
I do however start all the other modules with…
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
Infact I use local for everything apart from the two examples at the top of my main.lua
If any Corona Staff like Rob for example could comment on this I would really like to know if what I’m doing is OK.
I’ve tried doing what you’ve done.
Where do i put the codes for the Physics module also? do i put it at the enterScene too?
I’ve tried putting the Functions outside the scene events and put the codes in enterScene and i get an error for “dispatchEvent”
a nil value. so I tried putting it in createScene, still the same error.
For now I’m using the physics codes that is in Corona sample codes folder. Creating a start button then going to actual game
@QuizMaster wrote:
If any Corona Staff like Rob for example could comment on this I would really like to know if what I’m doing is OK.
Actually, making things like physics and storyboard global are not the end of the world, but they are not the most optimum way either. Things in the global table (_G) (even if you don’t use the _G. in front of the global variable) are slower to access that localized variables. Its worth the performance gain to add:
local physics = require(“physics”)
at the top of each module. It doesn’t really reload it. It only loads it the first time in the first module that it’s used. Any modules afterwards, you’re just creating a localized reference to it. Yes, it takes you longer to write out those extra calls in every module. But there is “Copy and Paste”. It’s really worth the gain.
Once you have an object like Storyboard loaded, its a table in memory and you can easily set additional table members, be it other objects, functions or data attributes like:
storyboard.state.level = 1
The danger here is that who says the next time we put out a version of Storyboard (we won’t since it’s deprecated, but the same thing goes for Composer), that we would not use .state as a member and then you would end up writing over part of storyboard that new behavior was tied to. We added a setVariable and getVariable method to Composer to allow you to safely store key-value pairs in the Composer object, so the idea of storing data you need between scenes there is perfectly valid.
However that said, the safest way to pass data between scenes is to use a data scene. That is make your own globals that are not globals but act like it. See: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
Rob
Thanks for your input Rob, as my save data is quite small I think I’ll stick with what I’ve done already but it’s useful advice which I’ll bear in mind for my next project. I’m learning as I go and I’ve already re-written my code so many times I’ve lost count.
As for accidentally using a variable or member of a Corona table that is reserved then luckily I use Sublime Text which highlights all the API’s in blue as I type, which has saved my bacon on a few occassions.
B.T.W. my game has 27 modules not 16 and 14,400 lines of code, 423 different graphics (all created by myself), 39 sound effects and music tracks and I’m still not finished! The APK is 42.5MB and I’m trying very hard to keep it below 50MB when it’s finished.
NaughtyKid…
I’m a little confused by what you’re trying to explain or do with your code, without seeing an example it’s hard to help.
But what I’m interpretting from your last post it seems like your code is all over the place and out of scope.
When you create something within a scene it’s only accessable in that scene unless it’s global or returned.
You really don’t want to make anything global if you can help it.
When you create a module don’t forget to RETURN the table (variable) at the bottom of the code so that when you require that module you have a variable to work with.
I use a countdown timer in a lot of my levels so I created a module (clock.lua) to load the graphics and psoition it on screen, here is the code…
local clock = {} local loadClock = require("loadClock") -- This loads the 60 png's from another module local \_H=display.contentHeight local \_W=display.contentWidth local clockGroup = display.newGroup() local clocks = {} for i = 1, #loadClock.animClock do clocks[i] = display.newImageRect( clockGroup, loadClock.animClock[i], 160, 160) clocks[i].x = \_W\*0.92 clocks[i].y = \_H\*0.82 clocks[i].isVisible = false end clockGroup.currentClock = 1 clocks[clockGroup.currentClock].isVisible = true clock.clocks = clocks return clock
Then in each module’s enterScene (The main chunk) I load the module with…
local clock = require("clock")
And use it like so…
local checktime = function() if t \<= 60 then local idx = t clock.clocks[idx].isVisible = false idx = idx + 1 clock.clocks[idx].isVisible = true t = idx end if t == 61 then timer.cancel( selfTimer ) timesup() end end
I hope my real life example illustrates how to use modules and notice how I use local for everything, t inside the above function is declared local at the top of the enterScene() block, so is selfTimer.
timesup() calls another local function above that function.
I’m not sure you even need physics for a start button B.T.W. and as for a physics module?
I use a lot of physics API’s in my game but I’ve never found the need to write a seperate module for them as Corona has very simple one line commands for most things, like some more of my code…
local physics = require("physics") -- I added this to encourage proper programming techinique local star = {} local myTransition for i = 1, 10 do local r = math.random(9) star[i].anchorX=0.5; star[i].anchorY=0.5 physics.addBody( star[i], "dynamic", {density=r/10, friction=r/10, radius=35} ) local r = math.random(-300, 300) local s = math.random(-300, 300) local av = math.random(60, 360) local tm = math.random(1000, 2000) star[i].alpha = 1 myTransition = transition.to( star[i], { time=tm, alpha=0, x=x+r, y=y+s } ); star[i].angularVelocity = av end
Creates a very nice star burst by the way.
I also suspect you’re not putting your code in the right order, a mistake I make quite often even after 7 months of solid coding.
For example, let’s say you create…
local a = function()
some code
…
end
local b = function()
some code
…
end
If you try to jump to function b from within function a you will get a nil error because lua reads the code from top to bottom and function b doesn’t exist yet nor does anything inside it.
One way around this problem is to drop the local and just write b = function() and declare local b at the top of your main block, the same goes for any variables inside function b.
The best way if you can is to simply put function b above function a, but remember that until that function is called any code inside that function doesn’t yet exist.
I hope this clears things up for you…
Thanks for explaining it to me
This is my code for the Storyboard to Physics
the function for the button
[lua]
local function NextButtonEventHandler(event)
storyboard.gotoScene( “physics”, “fade”, 500 )
return true
end
[/lua]
this is the code for my button
[lua]
local NextButton = widget.newButton( {
left = -130,
top = 670,
width = 170,
height = 80,
defaultFile = “Buttons/Next.png”,
overFile = “Buttons/NextOff.png”,
onRelease = NextButtonEventHandler,
} );
[/lua]
for some reason i seem to get a dispatchEvent(a nil value) error even though i don’t have any local stuff.
(Sorry for confusing questions)
I’m pretty sure you can’t call the scene ‘physics’ so your first step is to name it something else.
I’ve solved it, though i changed from storyboard to composer to solve the problem easier.
Thank you for the explanation it helps me a lot
Also thank you very much for helping me.
Thank you for explaining about global variables to us
Thank you for pointing that out. Turns out you really can’t name the file physics xD
THANKS! to ALL!
Glad to help.
Hi
I have 16 modules in my game so far and I’ve found it best to require the physics in main.lua only.
I don’t use local physics = require(“physics”) I drop the local and make it global, that way you don’t have to put it at the top of every module or remember to do it.
So my main.lua starts like this…
storyboard = require ("storyboard") local scene = storyboard.newScene() physics = require("physics") physics.setScale(30)
I have found that I only ever start the physics in enterScene
physics.start()
If I need physics in that module that is, not all my modules need a physics engine.
My approach might be bad practice I don’t know, I gave it some thought and it seems perfectly ok to make physics global, after all it is part of Corona just like storyboard is.
Notice how I made storyboard global at the top of my main.lua aswell?
I store player and game variables in it throughout my game like this…
storyboard.state.level = 1
local level = storyboard.state.level
I do however start all the other modules with…
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
Infact I use local for everything apart from the two examples at the top of my main.lua
If any Corona Staff like Rob for example could comment on this I would really like to know if what I’m doing is OK.
I’ve tried doing what you’ve done.
Where do i put the codes for the Physics module also? do i put it at the enterScene too?
I’ve tried putting the Functions outside the scene events and put the codes in enterScene and i get an error for “dispatchEvent”
a nil value. so I tried putting it in createScene, still the same error.
For now I’m using the physics codes that is in Corona sample codes folder. Creating a start button then going to actual game
@QuizMaster wrote:
If any Corona Staff like Rob for example could comment on this I would really like to know if what I’m doing is OK.
Actually, making things like physics and storyboard global are not the end of the world, but they are not the most optimum way either. Things in the global table (_G) (even if you don’t use the _G. in front of the global variable) are slower to access that localized variables. Its worth the performance gain to add:
local physics = require(“physics”)
at the top of each module. It doesn’t really reload it. It only loads it the first time in the first module that it’s used. Any modules afterwards, you’re just creating a localized reference to it. Yes, it takes you longer to write out those extra calls in every module. But there is “Copy and Paste”. It’s really worth the gain.
Once you have an object like Storyboard loaded, its a table in memory and you can easily set additional table members, be it other objects, functions or data attributes like:
storyboard.state.level = 1
The danger here is that who says the next time we put out a version of Storyboard (we won’t since it’s deprecated, but the same thing goes for Composer), that we would not use .state as a member and then you would end up writing over part of storyboard that new behavior was tied to. We added a setVariable and getVariable method to Composer to allow you to safely store key-value pairs in the Composer object, so the idea of storing data you need between scenes there is perfectly valid.
However that said, the safest way to pass data between scenes is to use a data scene. That is make your own globals that are not globals but act like it. See: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
Rob
Thanks for your input Rob, as my save data is quite small I think I’ll stick with what I’ve done already but it’s useful advice which I’ll bear in mind for my next project. I’m learning as I go and I’ve already re-written my code so many times I’ve lost count.
As for accidentally using a variable or member of a Corona table that is reserved then luckily I use Sublime Text which highlights all the API’s in blue as I type, which has saved my bacon on a few occassions.
B.T.W. my game has 27 modules not 16 and 14,400 lines of code, 423 different graphics (all created by myself), 39 sound effects and music tracks and I’m still not finished! The APK is 42.5MB and I’m trying very hard to keep it below 50MB when it’s finished.
NaughtyKid…
I’m a little confused by what you’re trying to explain or do with your code, without seeing an example it’s hard to help.
But what I’m interpretting from your last post it seems like your code is all over the place and out of scope.
When you create something within a scene it’s only accessable in that scene unless it’s global or returned.
You really don’t want to make anything global if you can help it.
When you create a module don’t forget to RETURN the table (variable) at the bottom of the code so that when you require that module you have a variable to work with.
I use a countdown timer in a lot of my levels so I created a module (clock.lua) to load the graphics and psoition it on screen, here is the code…
local clock = {} local loadClock = require("loadClock") -- This loads the 60 png's from another module local \_H=display.contentHeight local \_W=display.contentWidth local clockGroup = display.newGroup() local clocks = {} for i = 1, #loadClock.animClock do clocks[i] = display.newImageRect( clockGroup, loadClock.animClock[i], 160, 160) clocks[i].x = \_W\*0.92 clocks[i].y = \_H\*0.82 clocks[i].isVisible = false end clockGroup.currentClock = 1 clocks[clockGroup.currentClock].isVisible = true clock.clocks = clocks return clock
Then in each module’s enterScene (The main chunk) I load the module with…
local clock = require("clock")
And use it like so…
local checktime = function() if t \<= 60 then local idx = t clock.clocks[idx].isVisible = false idx = idx + 1 clock.clocks[idx].isVisible = true t = idx end if t == 61 then timer.cancel( selfTimer ) timesup() end end
I hope my real life example illustrates how to use modules and notice how I use local for everything, t inside the above function is declared local at the top of the enterScene() block, so is selfTimer.
timesup() calls another local function above that function.
I’m not sure you even need physics for a start button B.T.W. and as for a physics module?
I use a lot of physics API’s in my game but I’ve never found the need to write a seperate module for them as Corona has very simple one line commands for most things, like some more of my code…
local physics = require("physics") -- I added this to encourage proper programming techinique local star = {} local myTransition for i = 1, 10 do local r = math.random(9) star[i].anchorX=0.5; star[i].anchorY=0.5 physics.addBody( star[i], "dynamic", {density=r/10, friction=r/10, radius=35} ) local r = math.random(-300, 300) local s = math.random(-300, 300) local av = math.random(60, 360) local tm = math.random(1000, 2000) star[i].alpha = 1 myTransition = transition.to( star[i], { time=tm, alpha=0, x=x+r, y=y+s } ); star[i].angularVelocity = av end
Creates a very nice star burst by the way.
I also suspect you’re not putting your code in the right order, a mistake I make quite often even after 7 months of solid coding.
For example, let’s say you create…
local a = function()
some code
…
end
local b = function()
some code
…
end
If you try to jump to function b from within function a you will get a nil error because lua reads the code from top to bottom and function b doesn’t exist yet nor does anything inside it.
One way around this problem is to drop the local and just write b = function() and declare local b at the top of your main block, the same goes for any variables inside function b.
The best way if you can is to simply put function b above function a, but remember that until that function is called any code inside that function doesn’t yet exist.
I hope this clears things up for you…
Thanks for explaining it to me
This is my code for the Storyboard to Physics
the function for the button
[lua]
local function NextButtonEventHandler(event)
storyboard.gotoScene( “physics”, “fade”, 500 )
return true
end
[/lua]
this is the code for my button
[lua]
local NextButton = widget.newButton( {
left = -130,
top = 670,
width = 170,
height = 80,
defaultFile = “Buttons/Next.png”,
overFile = “Buttons/NextOff.png”,
onRelease = NextButtonEventHandler,
} );
[/lua]
for some reason i seem to get a dispatchEvent(a nil value) error even though i don’t have any local stuff.
(Sorry for confusing questions)
I’m pretty sure you can’t call the scene ‘physics’ so your first step is to name it something else.
I’ve solved it, though i changed from storyboard to composer to solve the problem easier.
Thank you for the explanation it helps me a lot
Also thank you very much for helping me.
Thank you for explaining about global variables to us
Thank you for pointing that out. Turns out you really can’t name the file physics xD
THANKS! to ALL!
Glad to help.