Please Help: About overlapping objects [RESOLVED]

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 :stuck_out_tongue:

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. :slight_smile:

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 :slight_smile:

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 :slight_smile:

Apologies for the long story. and thanks in advance. Cheers to all. :slight_smile:

[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]

mhelgarc,

Well for your question on removing from the table what has been popped, add a variable to every balloon you create, for example:

myBalloon.positionInTable = #balloonsOnScreen  

Then when the balloon is popped use the following to remove it in balloonWasTouched():

if (event.target.positionInTable ~= nil) then  
 table.remove(balloonsOnScreen,positionInTable)  
end  

Just make sure to make sure anything you grab from balloonsOnScreen isn’t nil.

Hope that helps!

  • idrach55
    [import]uid: 12741 topic_id: 8974 reply_id: 32803[/import]

thanks idrach…

I used your code and its working good… I was able to delete the popped balloon. :slight_smile:
Though I found some problems if i used that on my current code.

When I deleted that specific item within the array/table, the index of remaining items will change as well so the value from positionInTable will not be right anymore coz the position of the items has changed. Thus, I got nil error. :slight_smile:

Though I cannot use it here on my current code, it surely helps me to know that I could be able to assign a variable within the object. I’ll surely use this on my future project…

Thanks a lot mate. :slight_smile: [import]uid: 51389 topic_id: 8974 reply_id: 32845[/import]

On my second problem,
Though I was able to isolate the object (if two objects overlapped) so that its the only one that would be processed on touch event,

the problem that arises is that the one being processed was the object behind instead of the top object. :frowning:

I used this following API to isolate an object
[lua]local stage = display.getCurrentStage()
if “began” == event.phase then
stage:setFocus(currentBalloon)
end
if “end” == event.phase then
stage:setFocus(nil)
end[/lua]

I hope someone could help me to resolve this.
[import]uid: 51389 topic_id: 8974 reply_id: 32847[/import]

Help please… anybody… [import]uid: 51389 topic_id: 8974 reply_id: 32909[/import]

** bump ***

Pls… help [import]uid: 51389 topic_id: 8974 reply_id: 33011[/import]

Resolved

[lua]local function OnTouch ( event )
– some codes

– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end[/lua]

I don’t even need to put the setFocus API

I think this should be added on the documentation … :slight_smile:
[import]uid: 51389 topic_id: 8974 reply_id: 33208[/import]

Thanks @mhelgarc! I was having the same problem with overlapping buttons. Simple solution, just took a while to figure it out. [import]uid: 45104 topic_id: 8974 reply_id: 42225[/import]