Hi!
I have some code that changes images for a ball in my game. I reuse this code 18 times (=18 ball objects), so it would be nice to shorten it with functions so I don’t need to reuse the code for every object. But I don’t know how to do this, so I’m asking here instead!
--Load saved data for ball 1
lvl1Ball1Selection = loadValue( "lvl1Ball1.data" )
if lvl1Ball1Selection == "0" then
lvl1Ball1Selection = "ball"
saveValue( "lvl1Ball1.data", lvl1Ball1Selection )
end
--Set pictures for ball 1 animation
lvl1Ball1Selector = movieclip.newAnim({ "images/balls/ball"..ImageSuffix..".png", "images/balls/ballred"..ImageSuffix..".png", "images/balls/ballyellow"..ImageSuffix..".png", "images/balls/ballgreen"..ImageSuffix..".png", "images/balls/ballpink"..ImageSuffix..".png", "images/balls/ballamfoot"..ImageSuffix..".png", "images/balls/ballbase"..ImageSuffix..".png", "images/balls/ballbasket"..ImageSuffix..".png", "images/balls/ballfoot"..ImageSuffix..".png", "images/balls/balltennis"..ImageSuffix..".png", "images/balls/ballbowling"..ImageSuffix..".png", "images/balls/balleight"..ImageSuffix..".png", "images/balls/balleye"..ImageSuffix..".png", "images/balls/ballskull"..ImageSuffix..".png", "images/balls/ballskull"..ImageSuffix..".png","images/balls/ballmean"..ImageSuffix..".png", "images/balls/ballgusta"..ImageSuffix..".png", "images/balls/balltroll"..ImageSuffix..".png" }, 28, 28 )
lvl1Ball1Selector.isVisible = false
--Start animation selection
local onlvl1Ball1SelectorTouch = function( event )
if event.phase == "began" then
-- Play Sound
if soundsOn == true then
audio.play(sounds.tapsound)
end
if lvl1Ball1Selection == "ball" then
lvl1Ball1Selection = "ballred"
lvl1Ball1Selector:stopAtFrame( 2 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballred" then
lvl1Ball1Selection = "ballyellow"
lvl1Ball1Selector:stopAtFrame( 3 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballyellow" then
lvl1Ball1Selection = "ballgreen"
lvl1Ball1Selector:stopAtFrame( 4 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballgreen" then
lvl1Ball1Selection = "ballpink"
lvl1Ball1Selector:stopAtFrame( 5 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballpink" then
lvl1Ball1Selection = "ballamfoot"
lvl1Ball1Selector:stopAtFrame( 6 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballamfoot" then
lvl1Ball1Selection = "ballbase"
lvl1Ball1Selector:stopAtFrame( 7 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballbase" then
lvl1Ball1Selection = "ballbasket"
lvl1Ball1Selector:stopAtFrame( 8 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballbasket" then
lvl1Ball1Selection = "ballfoot"
lvl1Ball1Selector:stopAtFrame( 9 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballfoot" then
lvl1Ball1Selection = "balltennis"
lvl1Ball1Selector:stopAtFrame( 10 )
settingsChanged = true
elseif lvl1Ball1Selection == "balltennis" then
lvl1Ball1Selection = "ballbowling"
lvl1Ball1Selector:stopAtFrame( 11 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballbowling" then
lvl1Ball1Selection = "balleight"
lvl1Ball1Selector:stopAtFrame( 12 )
settingsChanged = true
elseif lvl1Ball1Selection == "balleight" then
lvl1Ball1Selector:stopAtFrame( 13 )
settingsChanged = true
elseif lvl1Ball1Selection == "balleye" then
lvl1Ball1Selection = "ballskull"
lvl1Ball1Selector:stopAtFrame( 14 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballskull" then
lvl1Ball1Selection = "ballmean"
lvl1Ball1Selector:stopAtFrame( 15 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballmean" then
lvl1Ball1Selection = "ballgusta"
lvl1Ball1Selector:stopAtFrame( 16 )
settingsChanged = true
elseif lvl1Ball1Selection == "ballgusta" then
lvl1Ball1Selection = "balltroll"
lvl1Ball1Selector:stopAtFrame( 17 )
settingsChanged = true
elseif lvl1Ball1Selection == "balltroll" then
lvl1Ball1Selection = "ball"
lvl1Ball1Selector:stopAtFrame( 1 )
settingsChanged = true
end
else
return true
end
end
lvl1Ball1Selector:addEventListener( "touch", onlvl1Ball1SelectorTouch )
lvl1Ball1Selector.x = 148; lvl1Ball1Selector.y = 100
if lvl1Ball1Selection == "ball" then
lvl1Ball1Selector:stopAtFrame( 1 )
elseif lvl1Ball1Selection == "ballred" then
lvl1Ball1Selector:stopAtFrame( 2 )
elseif lvl1Ball1Selection == "ballyellow" then
lvl1Ball1Selector:stopAtFrame( 3 )
elseif lvl1Ball1Selection == "ballgreen" then
lvl1Ball1Selector:stopAtFrame( 4 )
elseif lvl1Ball1Selection == "ballpink" then
lvl1Ball1Selector:stopAtFrame( 5 )
elseif lvl1Ball1Selection == "ballamfoot" then
lvl1Ball1Selector:stopAtFrame( 6 )
elseif lvl1Ball1Selection == "ballbase" then
lvl1Ball1Selector:stopAtFrame( 7 )
elseif lvl1Ball1Selection == "ballbasket" then
lvl1Ball1Selector:stopAtFrame( 8 )
elseif lvl1Ball1Selection == "ballfoot" then
lvl1Ball1Selector:stopAtFrame( 9 )
elseif lvl1Ball1Selection == "balltennis" then
lvl1Ball1Selector:stopAtFrame( 10 )
elseif lvl1Ball1Selection == "ballbowling" then
lvl1Ball1Selector:stopAtFrame( 11 )
elseif lvl1Ball1Selection == "balleight" then
lvl1Ball1Selector:stopAtFrame( 12 )
elseif lvl1Ball1Selection == "balleye" then
lvl1Ball1Selector:stopAtFrame( 13 )
elseif lvl1Ball1Selection == "ballskull" then
lvl1Ball1Selector:stopAtFrame( 14 )
elseif lvl1Ball1Selection == "ballmean" then
lvl1Ball1Selector:stopAtFrame( 15 )
elseif lvl1Ball1Selection == "ballgusta" then
lvl1Ball1Selector:stopAtFrame( 16 )
elseif lvl1Ball1Selection == "balltroll" then
lvl1Ball1Selector:stopAtFrame( 17 )
end
menuGroup:insert( lvl1Ball1Selector )
timer.performWithDelay( 200, function() lvl1Ball1Selector.isVisible = true; end, 1 )
Best regards,
joelwe [import]uid: 54640 topic_id: 22452 reply_id: 322452[/import]
[import]uid: 52491 topic_id: 22452 reply_id: 89546[/import]
[import]uid: 54640 topic_id: 22452 reply_id: 89593[/import]