Yeah I’m pretty sure, I took out all other code of the program to see if that was the problem, but it still doesn’t work. This is all I have now, and the global variable still resets to 100:
main.lua - creates the global monster_health and loads mainscreen.lua
monster\_health = 100
display.setStatusBar( display.HiddenStatusBar )
-- Import director class
local director = require("director")
-- Create a main group
local mainGroup = display.newGroup()
-- Main function
local function main()
-- Add the group from director class
mainGroup:insert(director.directorView)
-- Change scene without effects
director:changeScene("mainscreen")
return true
end
-- Begin
main()
mainscreen.lua - prints the variable and to buttons, one that adds 10 to monster_health and one who takes you to fight.lua
module(..., package.seeall)
-- Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()
button\_xp = display.newImage( "button\_xp.png" )
localGroup:insert( button\_xp )
button\_xp.x = display.contentWidth - 74
button\_xp.y = 42
button\_fight = display.newImage( "button\_fight.png" )
localGroup:insert( button\_fight )
button\_fight.x = display.contentWidth - 74
button\_fight.y = 180
-- skriver ut hp-mätaren
hpTextfield = display.newText( monster\_health, 10, 120, "Helvetica Bold", 12 )
hpTextfield:setTextColor( 0, 0, 255, 255 )
localGroup:insert( hpTextfield )
function button\_xp:tap( event )
monster\_health = monster\_health + 10
hpTextfield.text = monster\_health
end
function button\_fight:tap( event )
director:changeScene("fight","fade",255,255,255)
end
button\_xp:addEventListener( "tap", button\_xp )
button\_fight:addEventListener( "tap", button\_fight )
-- MUST return a display.newGroup()
return localGroup
end
fight.lua - prints monster_health. but always turn out “100”
module(..., package.seeall)
-- Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()
monsterhpFight = monster\_health
monsterhpTextfield = display.newText( monsterhpFight, 40, 40, "Times", 36 )
monsterhpTextfield:setTextColor( 255, 0, 0, 255 )
localGroup:insert( monsterhpTextfield )
-- MUST return a display.newGroup()
return localGroup
end
Really can’t figure out what I’m doing wrong… [import]uid: 11440 topic_id: 3934 reply_id: 11995[/import]