I’ve been polishing the code for one plugin that I am planning on releasing next week, but while doing so, I think I may have stumbled across a bug.
I have three files: main.lua, myScene.lua and myLibrary.lua. I don’t require the physics library in any file. In main.lua, I check to see if physics exists by trying to print it. I also try to check if it exists in the myLibrary.lua by requiring it. As expected, physics is nil in both files.
However, after using composer to move to myScene.lua and checking to see if physics now exists, I get a table in both myScene.lua and myLibrary.lua files. So after using composer.gotoScene, the physics library has mysteriously been required.
Here’s the code.
main.lua:
print( "main", physics ) -- nil local myLibrary = require( "myLibrary" ) print( myLibrary.check() ) local composer = require("composer") composer.gotoScene( "myScene" )
myLibrary.lua:
local lib = {} function lib.check() print( "myLibrary", physics ) end return lib
myScene.lua:
local composer = require( "composer" ) local scene = composer.newScene() local myLibrary = require( "myLibrary" ) print( myLibrary.check() ) -- table: 04025818 print( "myScene", physics ) -- table: 04025818 -- since physics library suddenly exists, I can start it and create physics bodies physics.setDrawMode( "hybrid" ) physics.start() local rect = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100 ) physics.addBody( rect ) return scene
I have also attached the project. If it isn’t a bug, could someone enlighten me as to why this is happening?