Combine Two or More Functions

Hello I am 100% completely new to programming. I have a bit of history with working on minor things, however I am dertermined to make my first game. That being said I am running into a bit of a snag.

What I am trying to do fumbling around in the dark is this.

  1. Create Multiple objects on the screen to generate A Random amount of Object in a Random Location on the Screen.

2)Touch to remove these objects.

3)Get a X amount of “points” everytime the object is touch to removed.

I have found two open source peices of code that will allow me to accomplish generating random number of objects on random location on the screen. As well as touch to remove an object. But the trouble is coming in trying to make these two seperate functions work together.

Can anyone point me in the right direction as to what I am doing wrong.

Below is the touch to remove code I have been working off of.

function onSmileTouch(self, event)
    if(event.phase == “began”) then
    timer.performWithDelay(1, function() self:removeSelf() end )
    end
    return true
    end

    crate = display.newImageRect( “crate.png”, 90,90)
    crate.touch = onSmileTouch
    crate:addEventListener(“touch”, smile)

Below is the spawn random object code I have been working off of.

local numberSmiles = 15 --local variable; amount can be changed
 
local function clearSmile( thisSmile )
   display.remove( thisSmile ) ; thisSmile = nil
end

local function spawnSmiles()

   for i=1,numberSmiles do
      smile = display.newImageRect(“crate.png”, 45, 45);
      – smile:setReferencePoint(display.CenterReferencePoint);  --not necessary; center is default
      smile.x = math.random(-10, 400);
      smile.y = -40;
      transition.to( smile, { time=math.random(2000,8000), x=math.random(-10,400) , y=600, onComplete=clearSmile } );
      physics.addBody( smile, “dynamic”, { density=0.1, bounce=0.1, friction=0.1, radius=0 } );

The Problem im having once again is getting these two functions to work together instead of working independently.

More importantly though is how do I get Multiple functions to work together instead of seperatly, because after I get this to work, im planning on adding the gain a point on object removed.

All you need to do is remove these lines

    crate = display.newImageRect( “crate.png”, 90,90)

    crate.touch = onSmileTouch

    crate:addEventListener(“touch”, smile)

    

and go down into your spawnSmiles() function and say

    smile.touch = onSmileTouch

    smile:addEventListener(“touch”, smile)

There are a ton of ways to do what you are attempting, but to your original question on getting multiple functions to work together it is just a matter of knowing (a) where to put the function if it is local vs global and (b) where to call it.

For most things like object events (ie: touch etc.) they will go with the main object then you can spawn out from inside the touch even if you need to access other functions, for anything else it really doesn’t matter.

If it isn’t an object or system event it really just comes down to preference and optimized code.

Just did a quick Google search for Lua Tutorials and guides and came across this only looked at it for a couple seconds but seems to be exactly what you need for a quick reference guide :slight_smile:

http://www.tutorialspoint.com/lua/lua_functions.htm

Thank you so much

All you need to do is remove these lines

    crate = display.newImageRect( “crate.png”, 90,90)

    crate.touch = onSmileTouch

    crate:addEventListener(“touch”, smile)

    

and go down into your spawnSmiles() function and say

    smile.touch = onSmileTouch

    smile:addEventListener(“touch”, smile)

There are a ton of ways to do what you are attempting, but to your original question on getting multiple functions to work together it is just a matter of knowing (a) where to put the function if it is local vs global and (b) where to call it.

For most things like object events (ie: touch etc.) they will go with the main object then you can spawn out from inside the touch even if you need to access other functions, for anything else it really doesn’t matter.

If it isn’t an object or system event it really just comes down to preference and optimized code.

Just did a quick Google search for Lua Tutorials and guides and came across this only looked at it for a couple seconds but seems to be exactly what you need for a quick reference guide :slight_smile:

http://www.tutorialspoint.com/lua/lua_functions.htm

Thank you so much