Matthew is right about rewriting your code and breaking it up into separate class files. Much easier to deal with.
You’ll also notice one of the things he’s doing in his suggestion is passing the variable as an argument to the class methods, e.g. cash.addCash( 56000 ). By passing the variable totalCash to travel.run, you should be able to get your code working without a lot of changes.
Also, I’m not sure if you noticed, but try running your code with Corona Terminal and make sure you’re seeing the output. You’ll notice that shipPurchased is running 4 times and trying to call the native alert 4 times. It’s also subtracting from your totalCash value 4 times. Use the “ended” phase event that’s sent from the touch listener to make sure your function only runs once. “ended” is kind of like button “release”.
I’ve modified your code by adding the phase event “ended” and by adding totalCash as an argument to travel.run. Take a look:
main.lua
—8[code]
display.setStatusBar (display.HiddenStatusBar)
totalCash = 100000;
local shipYard = require(“shipyard”);
shipYard.run();
[/code]
shipYard.lua
—8[code]
module(…, package.seeall)
function shipPurchased(event)
local phase = event.phase
if “ended” == phase then
totalCash = totalCash - 52000;
print(totalCash)
timer.performWithDelay(200, function()
local travel = require(“travel”);
travel.run(totalCash);
end)
end
end
function shipClicked(event)
local phase = event.phase
if “ended” == phase then
buyLabel = display.newImage(“5a_buyshipbutton.png”);
buyLabel.xScale=.5; buyLabel.yScale=.5;
buyLabel.x=50; buyLabel.y=350-60;
buyLabel:addEventListener(“touch”, shipPurchased);
end
end
function run()
shipSmall1 = display.newImage(“5a_buyshipbutton.png”);
shipSmall1.xScale=.3; shipSmall1.yScale=.3;
shipSmall1.x=display.stageWidth/5*4+30; shipSmall1.y=-75;
shipSmall1.shipId=1;
transition.to(shipSmall1, {delay=3400, time=500, y=45})
shipSmall1:addEventListener(“touch”, shipClicked)
end
[/code]
travel.lua
—8[code]
module(…, package.seeall)
function run(totalCash)
native.showAlert(totalCash, totalCash);
end
[/code]
Best,
Gilbert [import]uid: 5917 topic_id: 1002 reply_id: 2715[/import]