Hey guys I am going to post my code below I have been trying to have a money variable then when a button is touched it will save it and subtract the price variable and remeber what is left for future purchases in the game.
I don’t know what I am doing wrong? I tried many tutorials but none seem to work or I am not doing it right
_ -----------------------------------------------------------------------------------------
–
– main.lua
–
-----------------------------------------------------------------------------------------
json = require (‘json’)
print(10-5)
local widget = require (“widget”)
print(numonscreen)
local money = 10000
local mypics = {“regularhouse.png”}
– Save specified value to specified encrypted file
function saveValue(loadsave, money)
local theFile = loadsave
local theValue = money
local path = system.pathForFile( theFile, system.DocumentsDirectory )
local file = io.open( path, “w+” )
if file then – If the file exists, then continue. Another way to read this line is ‘if file == true then’.
file:write(theValue) – This line will write the contents of the table to the .json file.
io.close(file) – After we are done with the file, we close the file.
return true – If everything was successful, then return true
end
end
– Load specified encrypted file, or create new file if it does not exist
function loadValue(loadsave)
local theFile = loadsave
local path = system.pathForFile( theFile, system.DocumentsDirectory )
local file = io.open( path, “r” )
if file then – If file exists, continue. Another way to read this line is ‘if file == true then’.
local contents = file:read( “*a” ) – read all contents of file into a string
io.close( file ) – Since we are done with the file, close it.
return contents – Return the table with the JSON contents
else
return ‘’ – Return nothing
end
end
local rectangle = display.newRect(160,200,275,400)
local buy = widget.newButton
{
left = -45,
top = 350,
id = “buybtn”,
label = “Buy”,
}
local newcard = widget.newButton
{
left = 160,
top = 350,
id = “newcardbtn”,
label = “New Card”,
}
local onTouch
local someNewText
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local mph = display.newText(“Money Per Hour”,155,200,nil,35)
mph:setFillColor(1,0,0)
local text = display.newText(math.random(1000,10000),centerX,centerY,nil,40)
text:setFillColor(1,0,0)
function someNewText()
text.text = math.random(1000,10000)
transition.to(text, {time = 100, alpha = 1})
end
function onTouch(event)
if (event.phase == “began”) then
print(“it began”)
transition.to(event.target,{time = 100, alpha = 0, onComplete = someNewText} )
local r = math.random ( 1, #mypics )local item = display.newImage ( mypics[r], 150, 150 )
local newcard1 = widget.newButton
{
left = 160,
top = 350,
id = “newcardbtn1”,
label = “New Card”,
}
newcard1:addEventListener(“touch”,loadValue)
newcard1:addEventListener(“touch”,onTouch)
end
end
text:addEventListener(“touch”,onTouch)
function onTouch1( event )
if (event.phase == “began”) then
print(money - text.text)
local money = display.newText(money - text.text,50,50,nil,36)
money:setFillColor(1,0,0)
money.trans = transition.to( money, { time = 2000, x = 0, alpha = 0 } )
end
end
buy:addEventListener(“touch”,onTouch1)
buy:addEventListener(“touch”,saveValue)
local function touch2( event )
money3 = display.newText(money,50,50,nil,35)
money3:setFillColor(1,0,0)
if (event.phase == “ended”) then
money3:removeSelf()
end-- body
end
print(money)
_