i try to add a sound whenever I click on the balloon. What I did wrong? My print (tapSound) results in “nill”
local composer = require( "composer" )
local scene = composer.newScene()
local widget = require("widget")
widget.setTheme("widget_theme_android_holo_light")
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- ---------------------------, --------------------------------------------------------
local background
local platform
local balloon
local tapCount=0
local tapText
local physics = require( "physics")
local tapSound
local menuButton
local function pushBalloon(event)
balloon:applyLinearImpulse(0, -0.50, balloon.x, balloon.y)
tapCount = tapCount + .5
tapText.text = tapCount
tapText:setFillColor(0, 0, 0)
print(tapSound)
audio.play(tapSound)
end
local function touchFloor()
tapCount = tapCount - 1
tapText.text = tapCount
tapText:setFillColor(1, 0, 0)
end
local function menu()
composer.gotoScene("menu", {effect="slideUp", time=700})
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
tapSound = audio.loadSound( "assets/random.wav" )
-- Code here runs when the scene is first created but has not yet appeared on screen
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
background = display.newImageRect("assets/background.png", 460*1.2, 570*2.3)
background.x = display.contentCenterX
background.x = display.contentCenterY
sceneGroup:insert(background)
platform = display.newImageRect("assets/platform.png", 300, 50)
platform.x = display.contentCenterX
platform.y = display.contentHeight-25
sceneGroup:insert(platform)
balloon = display.newImageRect("assets/balloon.png", 112, 112)
balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
balloon.alpha = 0.8
sceneGroup:insert(balloon)
tapText = display.newText(tapCount, display.contentCenterX, 10, native.systemFont, 40)
sceneGroup:insert(tapText)
tapText:setFillColor( 0, 0, 0)
menuButton = widget.newButton({label="Menu", left= 75, top=500 })
sceneGroup:insert(menuButton)
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
physics.start()
physics.addBody( platform, "static")
physics.addBody( balloon, "dynamic", { radius = 45, bounce = 0.3})
balloon:addEventListener( "tap", pushBalloon )
balloon:addEventListener("collision", touchFloor)
menuButton:addEventListener("touch", menu)
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
tapCount = 0
physics.stop()
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene`Preformatted text`