hi,
I’m trying to keep score game using storyboard scenes. i would like to start the game with 100 health. would it be better to store it in a external module and keep calling it up in each scene ?
i used the code from peach’s techority.com tutorial and modified it from a scoring system to a health system
-- health.lua
module(..., package.seeall)
--Start the health at 100
local health = 100
--Create health text and make it dark gray
local healthText = display.newText(health, 20, 290, native.systemFont, 24)
healthText:setTextColor( 255, 255, 255 )
--Create the first aid kit
local firstaidkit = display.newRect( 0, 0, 50, 50 )
firstaidkit:setFillColor( 150, 180, 200 )
--Function to add to health and update healthText
local function addToHealth()
health = health + 30
healthText.text = health
firstaidkit:removeSelf()
firstaidkit:removeEventListener("tap", addToHealth)
end
firstaidkit:addEventListener("tap", addToHealth)
--Hide the status bar
display.setStatusBar(display.HiddenStatusBar)
i know id have to remove the following from the health.lua
--Create the first aid kit
local firstaidkit = display.newRect( 0, 0, 50, 50 )
firstaidkit:setFillColor( 150, 180, 200 )
--Function to add to health and update healthText
local function addToHealth()
health = health + 30
healthText.text = health
firstaidkit:removeSelf()
firstaidkit:removeEventListener("tap", addToHealth)
end
firstaidkit:addEventListener("tap", addToHealth)
----------------------------------------------------------------------------------------
-- main.lua
----------------------------------------------------------------------------------------
display.setStatusBar( display.HiddenStatusBar )
-- require controller module
local storyboard = require "storyboard"
local health = require( "health" )
-- comment out below four lines to remove FPS status
--local fps = require("fps")
--local performance = fps.PerformanceOutput.new();
--performance.group.x, performance.group.y = 50, 0;
--performance.alpha = 0.2; -- So it doesn't get in the way of the rest of the scene
-- load first screen
storyboard.gotoScene( "scene1", "fade", 400 )
how could i create the firstaidkit in scene1.lua and have it access the data from health.lua and then add 30 to the health.
[code]
– scene1.lua Room 1
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local lastScene = storyboard.getPrevious()
ocal health = require( “health” )
– BEGINNING OF YOUR IMPLEMENTATION
local background, room2, blackKey, blackdoorlocked, text1
– Touch event listener for background image
local function gotoRoom2( self, event )
if event.phase == “began” then
storyboard.gotoScene( “scene2”, “fade”, 400 )
return true
end
end
local function doorlocked( self, event )
if event.phase == “began” then
– storyboard.gotoScene( “scene20”, “fade”, 400 )
text1 = display.newText( “Door is Locked, the key should be near by”, 0, 245, native.systemFontBold, 20 )
text1:setTextColor( 255 )
transition.to(text1, {time=1000, onComplete = function() display.remove(text1) end})
return true
elseif(event.phase == “moved”) then
– Do Something During the “moved” phase
transition.to(self, {x = event.x, y = event.y, time=0});
end
end
local function onSceneTouchx( self, event )
if event.phase == “began” then
storyboard.gotoScene( “scene3”, “fade”, 400 )
return true
end
end
– Called when the scene’s view does not exist:
function scene:createScene( event )
local screenGroup = self.view
background = display.newImage( “assets/graphics/Room1.png”, 0, 0 )
screenGroup:insert( background )
blackdoorlocked = display.newImage( “assets/graphics/BlackLock2.png”, 175, 0 )
screenGroup:insert( blackdoorlocked )
blackdoorlocked.touch = doorlocked
room2 = display.newImage( “assets/graphics/gotoRoom2.png”, 175, 0 )
screenGroup:insert( room2 )
room2.touch = gotoRoom2
– blackKey = display.newImage( “assets/graphics/BlackKey.png”, 100, 200 )
– screenGroup:insert( blackKey )
– blackKey.touch = onSceneTouchx
print( “\n1: createScene event”)
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
print( “1: enterScene event” )
– remove previous scene’s view
storyboard.purgeScene( lastScene )
print( “last scene was:”, lastScene ) – output: last scene name
– Update Lua memory text display
– blackKey:addEventListener( “touch”, blackKey )
room2:addEventListener( “touch”, room2 )
blackdoorlocked:addEventListener( “touch”, blackdoorlocked )
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
print( “1: exitScene event” )
– remove touch listener for image
– blackKey:removeEventListener( “touch”, blackKey )
room2:removeEventListener( “touch”, room2 )
blackdoorlocked:removeEventListener( “touch”, blackdoorlocked )
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
print( “((destroying scene 1’s view))” )
end
– END OF YOUR IMPLEMENTATION
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene
[/code] [import]uid: 17701 topic_id: 25782 reply_id: 325782[/import]
[import]uid: 52491 topic_id: 25782 reply_id: 104403[/import]