pl help
Gotta share your code to get help, but in all likelihood, you’ve got a syntax error in your code.
menu1.lua file :-
module(…, package.seeall);
local sceneName = menu1
local composer = require( “composer” )
local scene = composer.newScene( sceneName )
function new()
local GUI = display.newGroup();
local easingx = require(“easingx”)
local widget = require(“widget”);
G10.Show_BOTTOM_Ads();
local planeNO = 0
local plane2Status = myGameSettings.plane2
local jetPrice = 125000
local background = display.newImageRect(“home.png”,_W,_H)
background.anchorX = 0.5
background.anchorY = 0.5
background.x = display.contentCenterX
background.y = display.contentCenterY
local goldImage = display.newImage(“gold.png”,10,15)
local goldText = display.newText("", 0, 0, “Antonio-Bold”, 30)
goldText.text = myGameSettings.coin;
goldText.x = 60; goldText.y = 38;
local plane1 = display.newImage(“plane1.png”)
plane1.x = 600; plane1.y = 220; plane1:scale(0.8,0.8)
local plane2 = display.newImage(plane2Status)
plane2.x = 1000; plane2.y = 220; plane2:scale(0.8,0.8)
GUI:insert(background);
GUI:insert(goldImage);
GUI:insert(plane1);
GUI:insert(plane2);
end
function scene:create( event )
local sceneGroup = self.view
– Called when the scene’s view does not exist
– INSERT code here to initialize the scene
new();
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen
– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen
–
– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s “view” (sceneGroup)
–
– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc
end
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
–scene:addEventListener( “hide”, scene )
–scene:addEventListener( “destroy”, scene )
return menu1
main.lua file :-
display.setStatusBar(display.HiddenStatusBar);
local composer = require “composer”
local mainGroup = display.newGroup();
_W = display.contentWidth;
_H = display.contentHeight;
centerX = display.contentCenterX
centerY = display.contentCenterY
local path = system.pathForFile( “myGameSettings.json”, system.DocumentsDirectory )
– io.open opens a file at path. returns nil if no file found
local createNewMGS, errStr = io.open( path, “r” )
if createNewMGS then
--do nothing
else
– create file because it doesn’t exist yet
createNewMGS = io.open( path, “w” )
if createNewMGS then
print( “Created new myGameSettings” )
local loadsave = require(“loadsave”)
myGameSettings = {}
myGameSettings.highScoreWorld1 = 0
myGameSettings.highScoreWorld2 = 0
myGameSettings.world = “world1”
myGameSettings.plane2 = “plane2L.png”
myGameSettings.terrain2 = “terrain2L.png”
loadsave.saveTable(myGameSettings, “myGameSettings.json”)
else
print( “Create file failed!” )
end
end
io.close( createNewMGS )
local loadsave = require(“loadsave”)
myGameSettings = {}
myGameSettings.highScoreWorld1 = 0
myGameSettings.highScoreWorld2 = 0
myGameSettings.plane2 = “plane2L.png”
myGameSettings.terrain2 = “terrain2L.png”
myGameSettings.terrain3 = “terrain3L.png”
myGameSettings = loadsave.loadTable(“myGameSettings.json”)
local function main()
–mainGroup:insert(director.directorView);
composer.gotoScene( “menu1” )
return true;
end
main();
** RE-EDITED FOR CLEANER LAYOUT **
First, I don’t see a call to string.gsub(), so that must be happening elsewhere, nonetheless we can move forward.
Second, You’re not using composer correctly. You didn’t insert any of your object into the scene group so the scene won’t manage them.
I’ve re-written and formatted your code a bit (to the best of my ability based on what I see):
-
Create a new file call ext.lua:
local json = require( “json” ) – == – table.save( theTable, fileName [, base] ) - Saves table to file (Uses JSON library as intermediary) – == function table.save( theTable, fileName, base ) local base = base or system.DocumentsDirectory local path = system.pathForFile( fileName, base ) local fh = io.open( path, “w” ) local tmpTable = table.deepStripCopy(theTable) if(fh) then fh:write(json.encode( tmpTable )) io.close( fh ) return true end return false end – == – table.load( fileName [, base] ) - Loads table from file (Uses JSON library as intermediary) – == function table.load( fileName, base ) local base = base or system.DocumentsDirectory local path = system.pathForFile( fileName, base ) local fh, reason = io.open( path, “r” ) if fh then local contents = fh:read( “*a” ) io.close( fh ) local newTable = json.decode( contents ) return newTable else return nil end end
-
Replace main.lua with this:
display.setStatusBar(display.HiddenStatusBar); local composer = require “composer” local loadsave = require(“loadsave”) require “ext” – Load my table save/load functions _W = display.contentWidth; _H = display.contentHeight; centerX = display.contentCenterX centerY = display.contentCenterY _G.myGameSettings = table.load( “myGameSettings.json” ) if( not myGameSettings ) then myGameSettings = {} myGameSettings.highScoreWorld1 = 0 myGameSettings.highScoreWorld2 = 0 myGameSettings.plane2 = “plane2L.png” myGameSettings.terrain2 = “terrain2L.png” myGameSettings.terrain3 = “terrain3L.png” table.save( myGameSettings, “myGameSettings.json” ) end – Load the composer scene composer.gotoScene( “menu1” )
-
Replace menu1.lua with this:
– ============================================================= – Copyright YOUR COMPANY INFO HERE – ============================================================= – -- ============================================================= local composer = require( “composer” ) local scene = composer.newScene() local easingx = require(“easingx”) local widget = require(“widget”) ---------------------------------------------------------------------- – LOCALS – ---------------------------------------------------------------------- – Variables local planeNO = 0 local plane2Status = myGameSettings.plane2 or “plane2L.png” – just in case local jetPrice = 125000 – Forward Declarations ---------------------------------------------------------------------- – Scene Methods ---------------------------------------------------------------------- ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:create( event ) local screenGroup = self.view – This group is owned by the scene. – If you don’t put objects in it, they won’t be managed – by the scene. local GUI = display.newGroup() screenGroup:insert(GUI) – Do this, otherwise scene won’t own it and clean it – G10.Show_BOTTOM_Ads() – EFM What is this and where from? local background = display.newImageRect( GUI, “home.png”, _W, _H ) background.anchorX = 0.5 background.anchorY = 0.5 background.x = centerX background.y = centerY local goldImage = display.newImage( GUI, “gold.png”, 10, 15 ) local goldText = display.newText( GUI, “”, 0, 0, “Antonio-Bold”, 30 ) goldText.text = myGameSettings.coin goldText.x = 60 goldText.y = 38 local plane1 = display.newImage( GUI, “plane1.png” ) plane1.x = 600 plane1.y = 220 plane1:scale(0.8,0.8) local plane2 = display.newImage( GUI, plane2Status ) plane2.x = 1000 plane2.y = 220 plane2:scale(0.8,0.8) end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willEnter( event ) local screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didEnter( event ) local screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willExit( event ) local screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didExit( event ) local screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:destroy( event ) local screenGroup = self.view end --------------------------------------------------------------------------------- – Scene Dispatch Events, Etc. - Generally Do Not Touch Below This Line --------------------------------------------------------------------------------- function scene:show( event ) local screenGroup = self.view local willDid = event.phase if( willDid == “will” ) then self:willEnter( event ) elseif( willDid == “did” ) then self:didEnter( event ) end end function scene:hide( event ) local screenGroup = self.view local willDid = event.phase if( willDid == “will” ) then self:willExit( event ) elseif( willDid == “did” ) then self:didExit( event ) end end scene:addEventListener( “create”, scene ) scene:addEventListener( “show”, scene ) scene:addEventListener( “hide”, scene ) scene:addEventListener( “destroy”, scene ) --------------------------------------------------------------------------------- return scene
Warning! Sample not run, so there could be typos, but it should be pretty clean. Please pay close attention to how I set up the composer file, AND how I created objects and inserted them into GUI, which itself was inserted into the scene’s group.
thank you so much man !!! u have re-written the code beautifully …
I replaced the files as u said …
got 1 error though … :-
ext.lua:10 attemt to call field deepStripCopy ( a nil value )
stack traceback
ext.lua 10 in function save
main lua 22 in main chunk
You can remove the call to deestripcopy or add it to the file:
-- == -- table.deepStripCopy( src [, dst]) - Copies multi-level tables, but strips out metatable(s) and ... -- -- Fields with these value types: -- functions -- userdata -- -- Fields with these these names: -- \_class -- \_\_index -- -- -- This function is primarily meant to be used in preparation for serializing a table to a file. -- Because you can't serialize a table with the above types and fields, they need to be removed first. -- Of course, when you load the file back up, it will be up to you to reattach the removed parts. -- -- == function table.deepStripCopy( src, dst ) local dst = dst or {} for k,v in pairs(src) do local key = tostring(k) local value = tostring(v) local keyType = type(k) local valueType = type(v) if( valueType == "function" or valueType == "userdata" or key == "\_class" or key == "\_\_index" ) then -- STRIP (SKIP IT) elseif( valueType == "table" ) then dst[k] = table.deepStripCopy( v, nil ) else dst[k] = v end end return dst end
Gotta share your code to get help, but in all likelihood, you’ve got a syntax error in your code.
menu1.lua file :-
module(…, package.seeall);
local sceneName = menu1
local composer = require( “composer” )
local scene = composer.newScene( sceneName )
function new()
local GUI = display.newGroup();
local easingx = require(“easingx”)
local widget = require(“widget”);
G10.Show_BOTTOM_Ads();
local planeNO = 0
local plane2Status = myGameSettings.plane2
local jetPrice = 125000
local background = display.newImageRect(“home.png”,_W,_H)
background.anchorX = 0.5
background.anchorY = 0.5
background.x = display.contentCenterX
background.y = display.contentCenterY
local goldImage = display.newImage(“gold.png”,10,15)
local goldText = display.newText("", 0, 0, “Antonio-Bold”, 30)
goldText.text = myGameSettings.coin;
goldText.x = 60; goldText.y = 38;
local plane1 = display.newImage(“plane1.png”)
plane1.x = 600; plane1.y = 220; plane1:scale(0.8,0.8)
local plane2 = display.newImage(plane2Status)
plane2.x = 1000; plane2.y = 220; plane2:scale(0.8,0.8)
GUI:insert(background);
GUI:insert(goldImage);
GUI:insert(plane1);
GUI:insert(plane2);
end
function scene:create( event )
local sceneGroup = self.view
– Called when the scene’s view does not exist
– INSERT code here to initialize the scene
new();
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen
– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen
–
– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s “view” (sceneGroup)
–
– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc
end
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
–scene:addEventListener( “hide”, scene )
–scene:addEventListener( “destroy”, scene )
return menu1
main.lua file :-
display.setStatusBar(display.HiddenStatusBar);
local composer = require “composer”
local mainGroup = display.newGroup();
_W = display.contentWidth;
_H = display.contentHeight;
centerX = display.contentCenterX
centerY = display.contentCenterY
local path = system.pathForFile( “myGameSettings.json”, system.DocumentsDirectory )
– io.open opens a file at path. returns nil if no file found
local createNewMGS, errStr = io.open( path, “r” )
if createNewMGS then
--do nothing
else
– create file because it doesn’t exist yet
createNewMGS = io.open( path, “w” )
if createNewMGS then
print( “Created new myGameSettings” )
local loadsave = require(“loadsave”)
myGameSettings = {}
myGameSettings.highScoreWorld1 = 0
myGameSettings.highScoreWorld2 = 0
myGameSettings.world = “world1”
myGameSettings.plane2 = “plane2L.png”
myGameSettings.terrain2 = “terrain2L.png”
loadsave.saveTable(myGameSettings, “myGameSettings.json”)
else
print( “Create file failed!” )
end
end
io.close( createNewMGS )
local loadsave = require(“loadsave”)
myGameSettings = {}
myGameSettings.highScoreWorld1 = 0
myGameSettings.highScoreWorld2 = 0
myGameSettings.plane2 = “plane2L.png”
myGameSettings.terrain2 = “terrain2L.png”
myGameSettings.terrain3 = “terrain3L.png”
myGameSettings = loadsave.loadTable(“myGameSettings.json”)
local function main()
–mainGroup:insert(director.directorView);
composer.gotoScene( “menu1” )
return true;
end
main();
** RE-EDITED FOR CLEANER LAYOUT **
First, I don’t see a call to string.gsub(), so that must be happening elsewhere, nonetheless we can move forward.
Second, You’re not using composer correctly. You didn’t insert any of your object into the scene group so the scene won’t manage them.
I’ve re-written and formatted your code a bit (to the best of my ability based on what I see):
-
Create a new file call ext.lua:
local json = require( “json” ) – == – table.save( theTable, fileName [, base] ) - Saves table to file (Uses JSON library as intermediary) – == function table.save( theTable, fileName, base ) local base = base or system.DocumentsDirectory local path = system.pathForFile( fileName, base ) local fh = io.open( path, “w” ) local tmpTable = table.deepStripCopy(theTable) if(fh) then fh:write(json.encode( tmpTable )) io.close( fh ) return true end return false end – == – table.load( fileName [, base] ) - Loads table from file (Uses JSON library as intermediary) – == function table.load( fileName, base ) local base = base or system.DocumentsDirectory local path = system.pathForFile( fileName, base ) local fh, reason = io.open( path, “r” ) if fh then local contents = fh:read( “*a” ) io.close( fh ) local newTable = json.decode( contents ) return newTable else return nil end end
-
Replace main.lua with this:
display.setStatusBar(display.HiddenStatusBar); local composer = require “composer” local loadsave = require(“loadsave”) require “ext” – Load my table save/load functions _W = display.contentWidth; _H = display.contentHeight; centerX = display.contentCenterX centerY = display.contentCenterY _G.myGameSettings = table.load( “myGameSettings.json” ) if( not myGameSettings ) then myGameSettings = {} myGameSettings.highScoreWorld1 = 0 myGameSettings.highScoreWorld2 = 0 myGameSettings.plane2 = “plane2L.png” myGameSettings.terrain2 = “terrain2L.png” myGameSettings.terrain3 = “terrain3L.png” table.save( myGameSettings, “myGameSettings.json” ) end – Load the composer scene composer.gotoScene( “menu1” )
-
Replace menu1.lua with this:
– ============================================================= – Copyright YOUR COMPANY INFO HERE – ============================================================= – -- ============================================================= local composer = require( “composer” ) local scene = composer.newScene() local easingx = require(“easingx”) local widget = require(“widget”) ---------------------------------------------------------------------- – LOCALS – ---------------------------------------------------------------------- – Variables local planeNO = 0 local plane2Status = myGameSettings.plane2 or “plane2L.png” – just in case local jetPrice = 125000 – Forward Declarations ---------------------------------------------------------------------- – Scene Methods ---------------------------------------------------------------------- ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:create( event ) local screenGroup = self.view – This group is owned by the scene. – If you don’t put objects in it, they won’t be managed – by the scene. local GUI = display.newGroup() screenGroup:insert(GUI) – Do this, otherwise scene won’t own it and clean it – G10.Show_BOTTOM_Ads() – EFM What is this and where from? local background = display.newImageRect( GUI, “home.png”, _W, _H ) background.anchorX = 0.5 background.anchorY = 0.5 background.x = centerX background.y = centerY local goldImage = display.newImage( GUI, “gold.png”, 10, 15 ) local goldText = display.newText( GUI, “”, 0, 0, “Antonio-Bold”, 30 ) goldText.text = myGameSettings.coin goldText.x = 60 goldText.y = 38 local plane1 = display.newImage( GUI, “plane1.png” ) plane1.x = 600 plane1.y = 220 plane1:scale(0.8,0.8) local plane2 = display.newImage( GUI, plane2Status ) plane2.x = 1000 plane2.y = 220 plane2:scale(0.8,0.8) end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willEnter( event ) local screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didEnter( event ) local screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willExit( event ) local screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didExit( event ) local screenGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:destroy( event ) local screenGroup = self.view end --------------------------------------------------------------------------------- – Scene Dispatch Events, Etc. - Generally Do Not Touch Below This Line --------------------------------------------------------------------------------- function scene:show( event ) local screenGroup = self.view local willDid = event.phase if( willDid == “will” ) then self:willEnter( event ) elseif( willDid == “did” ) then self:didEnter( event ) end end function scene:hide( event ) local screenGroup = self.view local willDid = event.phase if( willDid == “will” ) then self:willExit( event ) elseif( willDid == “did” ) then self:didExit( event ) end end scene:addEventListener( “create”, scene ) scene:addEventListener( “show”, scene ) scene:addEventListener( “hide”, scene ) scene:addEventListener( “destroy”, scene ) --------------------------------------------------------------------------------- return scene
Warning! Sample not run, so there could be typos, but it should be pretty clean. Please pay close attention to how I set up the composer file, AND how I created objects and inserted them into GUI, which itself was inserted into the scene’s group.
thank you so much man !!! u have re-written the code beautifully …
I replaced the files as u said …
got 1 error though … :-
ext.lua:10 attemt to call field deepStripCopy ( a nil value )
stack traceback
ext.lua 10 in function save
main lua 22 in main chunk
You can remove the call to deestripcopy or add it to the file:
-- == -- table.deepStripCopy( src [, dst]) - Copies multi-level tables, but strips out metatable(s) and ... -- -- Fields with these value types: -- functions -- userdata -- -- Fields with these these names: -- \_class -- \_\_index -- -- -- This function is primarily meant to be used in preparation for serializing a table to a file. -- Because you can't serialize a table with the above types and fields, they need to be removed first. -- Of course, when you load the file back up, it will be up to you to reattach the removed parts. -- -- == function table.deepStripCopy( src, dst ) local dst = dst or {} for k,v in pairs(src) do local key = tostring(k) local value = tostring(v) local keyType = type(k) local valueType = type(v) if( valueType == "function" or valueType == "userdata" or key == "\_class" or key == "\_\_index" ) then -- STRIP (SKIP IT) elseif( valueType == "table" ) then dst[k] = table.deepStripCopy( v, nil ) else dst[k] = v end end return dst end