Hi guys
I found this game programming tutorial for beginner using Sparrow Framework.
Im trying to learn lua/corona by trying to port the same logic/flow of the game in Corona (already playable). But with some parts, unluckily I’m stuck
I put my generated code below (updated, Apr 17) for you to see and comment from that. I really got stuck with some things. And hope someone could help me in this very lively forum.
Firstly, How to remove a specific object inside the table without knowing the index?
- currently Im using [blockcode]table.remove(balloonsOnScreen)[/blockcode] which only removes the last item in the table… But it would be great if I could remove the one that was “touched/popped” from the balloon table. how could I do it?
–resolved. Thanks for the advice
Secondly, From 2 overlapping objects between N number of objects, how can I only choose the front object to process?
- currently when two balloons are overlapping and I touched one on the front one, the one behind it is also being touched as well. How could I make it that the only one in front should be the only one to be popped out and the user should separately touch the other balloon (the one being overlapped)
Lastly, as you can see… I had made a function within a function and currently my code seems not optimal. Im not sure if this style of coding is alright or crap haha! so hopefully someone could comment on my current code and mentor me to make it better
Apologies for the long story. and thanks in advance. Cheers to all.
[lua]points = 0
levelnumber = 1
– display background
local background = display.newImage(“images/tutorialbackground.png”, 0, 0)
popSound = audio.loadSound( “music/balloonpop.caf”)
– play music
local bgMusic = media.playSound(“music/music.caf”, true);
– display score
local score = display.newText("Score: ", display.contentCenterX - 125, 25, “Marker Felt”, 20)
score:setTextColor(0,0,0)
– display level
local level = display.newText("Level: " … tostring(levelnumber), display.contentCenterX + display.contentCenterX/2, 25, “Marker Felt”, 20)
level:setTextColor(0,0,0)
– balloon images list
local balloonLocation = “images/”
local balloonsCollectionName = {“bluetutorial.png”, “greentutorial.png”, “indigotutorial.png”, “orangetutorial.png”, “redtutorial.png”, “violettutorial.png”, “yellowtutorial.png”}
local balloonsOnScreen = {} – empty table for storing objects
– for good randomization
math.randomseed( os.time() )
local stage = display.getCurrentStage()
– generate a popped event on the balloon that has been touched
– from 2 overlapping objects between N number of objects, how can I only choose the front object?
local function balloonWasTouched(event)
local phase = event.phase
local currentBalloon = event.target
if “began” == phase then
stage:setFocus(currentBalloon)
end
if “ended” == phase then
score.text = "Score: " … tostring(points + 10)
points = points + 10
audio.play(popSound)
currentBalloon.yScale = 1
currentBalloon.xScale = 0.60
transition.to( currentBalloon, {time=1000, x=(event.x), y=(event.y + display.viewableContentHeight)})
– function to monitor that a popped balloon has been gone off the screen
local function balloonPoppedAndGone( event)
if currentBalloon.y >= (display.viewableContentHeight + 10 ) then
Runtime:removeEventListener(“enterFrame”, balloonPoppedAndGone)
display.remove( currentBalloon )
currentBalloon.isVisible=false
table.remove(balloonsOnScreen)
end
if #balloonsOnScreen == 0 then
level.text = "Level: " … tostring(levelnumber + 1)
levelnumber = levelnumber + 1
for i = 1, levelnumber do
AddBalloon()
end
end
end
Runtime:addEventListener(“enterFrame”, balloonPoppedAndGone)
stage:setFocus(nil)
end
end
– A balloon add function
function AddBalloon()
local w,h = math.random(display.viewableContentWidth), display.viewableContentHeight
balloonsOnScreen[#balloonsOnScreen + 1] = display.newImage(balloonLocation … balloonsCollectionName[math.random(table.maxn(balloonsCollectionName))],w,h);
myBalloon = balloonsOnScreen[#balloonsOnScreen]
myBalloon.positionInTable = #balloonsOnScreen
transition.from( myBalloon, { time=math.random(2,5)*1000, x=(w), y=(display.viewableContentHeight)} )
transition.to( myBalloon, { time=math.random(2,5)*1000, x=(w-math.random(1,50)), y=-50, onComplete=listener } )
myBalloon.myPreviousID = myBalloon
myBalloon:addEventListener(“touch”, balloonWasTouched)
–print ("AddBalloon: " … tostring(myBalloon))
end
AddBalloon()[/lua] [import]uid: 51389 topic_id: 8974 reply_id: 308974[/import]