Need help with writing a function

Hey

I have created a set of 3 boxes with collision filters. when a specific ball hits the box it removes itself.

Once all three balls have been removed i would like a button to appear, how would i go about writing this function with the correct syntax.

below is the logic

if
ball1:removeSelf, ball2:removeSelf, ball3:removeSelf
then
button.isVisible = true [import]uid: 24981 topic_id: 5751 reply_id: 305751[/import]

Sorry about the formatting. Assuming you have a listener being fired when the collision happens…

[lua]function newButton( btnImg, x, y )
– create and position the button
local btn = display.newImage( btnImg )
btn.x, btn.y = x, y

– create and attach an event handler to do something when the user taps the button
function btn:tap( event )
– do something here because the button was tapped
end
btn:addEventListener( “tap”, btn )

– don’t really need this, but if you want to keep a reference to the button after creating it…
return btn
end[/lua]

matt. [import]uid: 8271 topic_id: 5751 reply_id: 19709[/import]

Thank you for you quick reply.

I think i may need to make my problem clearer though.

I already have a button created which is called nextLevel currently set to isVisible = false. This button works fine and allows the user to move on to the next level.

I only want this button to become visible if the user has completed the task in the level.

The task in the level involves the user hitting 3 specific collision points with specific balls, once a collision point is hit the ball removes itself from the screen. once all 3 collisions have happened the task of the level is complete and only then would i like the nextLevel button to appear.

everything is working fine, i just need to make the nextLevel button appear once all three collisions have finished and all three balls have removed themselves.

so essentially all i need is to make the logic below work

if
ball1:removeSelf, ball2:removeSelf, ball3:removeSelf
then
button.isVisible = true

[import]uid: 24981 topic_id: 5751 reply_id: 19712[/import]

Store your three “balls” in an array.

You said your code to remove the ball from screen is working, so when that code is executed, also remove that ball from the array.

Then either with a timer, or during every collision event, check the array and see if it still contains balls. If the array is empty, show your button. [import]uid: 22457 topic_id: 5751 reply_id: 19726[/import]

I have posted part of my code below. Having an array sounds interesting, how would i go about implementing it or use a different method to achieve my goal

[code]

–>Background image code
local background = display.newImage(“bkg_bricks.png”)
localGroup:insert(background)

balloon = display.newImage(“red_balloon1.png”)
balloon.x = display.contentWidth/2
balloon.y = display.contentHeight/2
physics.addBody(balloon, { bounce =0.4, radius =47, friction =1.0, filter=balloonCollisionFilter} )
localGroup:insert(balloon)

finish1 = display.newImage(“brickGreen1.png”)
finish1.x = 300
finish1.y = 160
physics.addBody( finish1, “static”, { friction=0.5, bounce=0.3, filter=finish1CollisionFilter } )
localGroup:insert(finish1)

function onLocalCollision( self, event )
if ( event.phase == “began” ) then

elseif ( event.phase == “ended” ) then
balloon:removeSelf()
balloon = nil
end
end

balloon.collision = onCollision
balloon:addEventListener( “collision”, balloon )

finish1.collision = onLocalCollision
finish1:addEventListener( “collision”, finish1 )

balloon2 = display.newImage(“red_balloon2.png”)
balloon2.x = balloon.x + 105
balloon2.y = display.contentHeight/2
physics.addBody(balloon2, { bounce =0.5, radius =47, friction =1.0, filter=balloon2CollisionFilter } )
localGroup:insert(balloon2)

finish2 = display.newImage(“brickGreen2.png”)
finish2.x = 160
finish2.y = 160
localGroup:insert(finish2)
physics.addBody( finish2, “static”, { friction=0.5, bounce=0.3, filter=finish2CollisionFilter } )

function onLocalCollision( self, event )
if ( event.phase == “began” ) then

elseif ( event.phase == “ended” ) then
balloon2:removeSelf()
balloon2 = nil

end
end

balloon2.collision = onCollision
balloon2:addEventListener( “collision”, balloon2 )

finish2.collision = onLocalCollision
finish2:addEventListener( “collision”, finish2 )

balloon3 = display.newImage(“red_balloon3.png”)
balloon3.x = balloon.x - 105
balloon3.y = display.contentHeight/2
physics.addBody(balloon3, { bounce =0.3, radius =48, friction =1.0, filter=balloon3CollisionFilter} )
localGroup:insert(balloon3)

finish3 = display.newImage(“brickGreen3.png”)
finish3.x = 30
finish3.y = 160
localGroup:insert(finish3)
physics.addBody( finish3, “static”, { friction=0.5, bounce=0.3, filter=finish3CollisionFilter } )

function onLocalCollision( self, event )
if ( event.phase == “began” ) then

elseif ( event.phase == “ended” ) then
balloon3:removeSelf()
balloon3 = nil

end
end

balloon3.collision = onCollision
balloon3:addEventListener( “collision”, balloon3 )

finish3.collision = onLocalCollision
finish3:addEventListener( “collision”, finish3 )

nextLevel = display.newImage(“bt_moveFromLeft.png”)
local function bt02t ( event )
if event.phase == “ended” then
director:changeScene(“level2”)
end
end
nextLevel :addEventListener(“touch”,bt02t)
nextLevel .x = 240
nextLevel .y = 120
nextLevel.isVisible = false
localGroup:insert(nextLevel )

function moveBalloon(event)
balloon = event.target
balloon:applyLinearImpulse( 0, -0.3, event.x, event.y )
end

balloon:addEventListener(“touch”, moveBalloon)
balloon2:addEventListener(“touch”, moveBalloon)
balloon3:addEventListener(“touch”, moveBalloon)

[/code] [import]uid: 24981 topic_id: 5751 reply_id: 19734[/import]

Try something like this:

local gameobjects = {}  
  
--create balloon1  
--create balloon2  
--create balloon3  
  
gameobjects["balloon1"] = balloon1  
gameobjects["balloon2"] = balloon2  
gameobjects["balloon3"] = balloon3  
  
--then in your collision listener to  
  
--assuming this is the event handler for balloon1 collision  
gameobjects["balloon1"] = nil  
  
--then at some point, keep checking if gameobjects still has balloon objects in it.  
  
local function isTaskComplete()  
 for i,k in pairs(gameobjects) do  
 if(k) then return false end --you have an element that is not nil, which means there is still at least one active balloon.  
 end  
 return true --you can show your level complete button  
end  

Now the code above is just to get you thinking. It’s not the most ideal code–what happens when you want to add 1 balloon? 10 balloons?

From looking at your posted code, here are a couple things that will help you as you become a coding ninja:

  1. Master “Don’t Repeat Yourself” aka DRY. It’s a simple principle: if you have code that is nearly identical to code you’ve already written, you’re doing it wrong, so stop and try to make your code such that you can reuse existing code.

  2. Add comments. I’m sure you know what your code is doing, but will you remember 4 weeks from now when you go back to look at your existing code? [import]uid: 22457 topic_id: 5751 reply_id: 19737[/import]

Thanks, i will give it a go [import]uid: 24981 topic_id: 5751 reply_id: 19743[/import]

The interesting thing is that the first principle reduces the importance of the second. For example, if you take the repeated code for creating a balloon and stick it in a function called createBalloon() that you call whenever you want to create a balloon, it’s pretty clear what that chunk of code does. [import]uid: 12108 topic_id: 5751 reply_id: 19747[/import]

Thats extremely true, if you make your code clear enough it’s just as good as commenting. I think comments make your files bulky and ugly.

You could try this method

[lua]balloon = display.newImage( “balloon.png” )
physics.addBody ( balloon, “dynamic”, {bounce = 0.3, friction = 1.0,density = 1.0 } )

– Level Complete Check
function checkIsLevelComplete()
–Check to see if your object array is empty for example
end

function balloonCollision(self, event)
if event.phase == “ended” then
–Remove self
event.target:removeSelf()
–Proceed to check if level is complete
checkIsLevelComplete()
end
end

balloon.collision = balloonCollsion[/lua]

Something like that may work, mind you I’m new to lua and don’t have Corona SDK on this computer so it may not completely work. [import]uid: 29147 topic_id: 5751 reply_id: 19750[/import]

It’s a lot tougher to write self-documenting code than people think, especially if you’re new to coding.

Getting in the habit of adding comments gets you in the routine of thinking about what your code is doing. As you progress in your skill, that process transitions to writing your code so that it’s self descriptive.

Writing self-documenting code, should never be the reason you don’t write code comments.

[import]uid: 22457 topic_id: 5751 reply_id: 19783[/import]

True.

Also, writing vars/functions with long descriptive names is not the way to do it; Make short and punctual. One method to writing self-doc code is to write the comments first, then go back and fill in the code.

M [import]uid: 8271 topic_id: 5751 reply_id: 19786[/import]

Understandable.

I use the "If I have to think for more than 5 seconds about a function and what it’s about to do, comment it.

Although, this thread may be getting off topic and into something more opinionated and less game development related. :smiley:

@projectcolour I hope we helped you out a bit [import]uid: 29147 topic_id: 5751 reply_id: 19791[/import]

Thanks for helping

regarding comments, i actually had paragraphs of comments but removed them from the code snippet as it made it too long. I have used most of the original names for functions and coding style found in the sample folder of the SDK, starting with the basic collision detection sample [import]uid: 24981 topic_id: 5751 reply_id: 19829[/import]

@projectcolor: I hope you didn’t get the feeling that we were picking on you–that wasn’t the case. Just passing on some tips that us grizzly vets have picked up having been writing software for a few years. [import]uid: 22457 topic_id: 5751 reply_id: 19860[/import]

Grizzly?

M;) [import]uid: 8271 topic_id: 5751 reply_id: 19890[/import]

Its all good, all advice is welcomed. :slight_smile: [import]uid: 24981 topic_id: 5751 reply_id: 20025[/import]