how can I combine the 3 functions into one and checked if the object is correctly drag and go to the next level

here’s the code. [lua] _H = display.contentHeight _W = display.contentWidth local composer = require “composer”; local scene = composer.newScene(); local bg, message, house, ball, can, shadowHouse, shadowBall, shadowCan local function hasCollided(obj1, obj2) if (obj1 == nil) then return false end if (obj2 == nil) then return false end local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax return (left or right) and (up or down) end local function dragHouse(event) local target = event.target local phase = event.phase if (event.phase == “began”) then local parent = target.parent display.getCurrentStage(): setFocus(target) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif(target.isFocus) then if (phase == “moved”) then target.x = event.x - target.x0 target.y = event.y - target.y0 if (hasCollided(event.target, shadowHouse)) then transition.to(event.target, {time = 250, x = shadowHouse.x, y = shadowHouse.y}) audio.play(sound2) composer.gotoScene(“nextScene2”,“fade”, 100) phase = “cancelled” else end elseif(phase == “ended” or phase == “cancelled”) then display.getCurrentStage():setFocus(nil) target.isFocus = false end end return true end local function dragBall(event) local target = event.target local phase = event.phase if (event.phase == “began”) then local parent = target.parent display.getCurrentStage(): setFocus(target) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif(target.isFocus) then if (phase == “moved”) then target.x = event.x - target.x0 target.y = event.y - target.y0 if (hasCollided(event.target, shadowBall)) then transition.to(event.target, {time = 250, x = shadowBall.x, y = shadowBall.y}) audio.play(sound2) phase = “cancelled” else end elseif(phase == “ended” or phase == “cancelled”) then display.getCurrentStage():setFocus(nil) target.isFocus = false end end return true end local function dragCan(event) local target = event.target local phase = event.phase if (event.phase == “began”) then local parent = target.parent display.getCurrentStage(): setFocus(target) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif(target.isFocus) then if (phase == “moved”) then target.x = event.x - target.x0 target.y = event.y - target.y0 if (hasCollided(event.target, shadowCan)) then transition.to(event.target, {time = 250, x = shadowCan.x, y = shadowCan.y}) audio.play(sound2) phase = “cancelled” else end elseif(phase == “ended” or phase == “cancelled”) then display.getCurrentStage():setFocus(nil) target.isFocus = false end end return true end function scene:create(event) local sceneGroup = self.view bg = display.newImage(“sceneBG.png”) bg.x = 50 bg.y = 50 sceneGroup:insert(bg) message = display.newText(“Game na ba?”, 250, 250, nil, 16) message:setFillColor (0, 0, 0) message.x = 50 message.y = 75 sceneGroup:insert(message) house = display.newImage(“house_red.png”) house.x = 50 house.y = 150 sceneGroup:insert(house) house.myName = “house” shadowHouse = display.newImage(“house_red.png”) shadowHouse.x = 160 shadowHouse.y = 150 shadowHouse:setFillColor(50, 50, 50) shadowHouse.alpha = 0.75 sceneGroup:insert(shadowHouse) shadowHouse.myName = “house” shadowHouse.collision = onLocalCollision ball = display.newImage(“ball_blue.png”) ball.x = 50 ball.y = 250 sceneGroup:insert(ball) ball.myName = “ball” shadowBall = display.newImage(“ball_blue.png”) shadowBall.x = 160 shadowBall.y = 250 shadowBall:setFillColor(50, 50, 50) shadowBall.alpha = 0.75 sceneGroup:insert(shadowBall) shadowBall.myName = “ball” shadowBall.collision = onLocalCollision can = display.newImage(“soda_can.png”) can.x = 50 can.y = 350 sceneGroup:insert(can) can.myName = “can” shadowCan = display.newImage(“soda_can.png”) shadowCan.x = 160 shadowCan.y = 350 shadowCan:setFillColor(50, 50, 50) shadowCan.alpha = 0.75 sceneGroup:insert(shadowCan) shadowCan.myName = “can” shadowCan.collision = onLocalCollision end function scene:show(event) local sceneGroup = self.view sound2 = audio.loadSound(“boing_wav.wav”) house:addEventListener(“touch”, dragHouse) shadowHouse:addEventListener(“collision”, shadowHouse) ball:addEventListener(“touch”, dragBall) shadowBall:addEventListener(“collision”, shadowBall) can:addEventListener(“touch”, dragCan) shadowCan:addEventListener(“collision”, shadowCan) end function scene:hide(event) local sceneGroup = self.view house:removeEventListener(“touch”, dragHouse) shadowHouse:removeEventListener(“collision”, shadowHouse) ball:removeEventListener(“touch”, dragBall) shadowBall:removeEventListener(“collision”, shadowBall) can:removeEventListener(“touch”, dragCan) shadowCan:removeEventListener(“collision”, shadowCan) end function scene:destroy(event) local sceneGroup = self.view end scene:addEventListener(“create”, scene) scene:addEventListener(“show”, scene) scene:addEventListener(“hide”, scene) scene:addEventListener(“destroy”, scene) return scene [/lua]

The best way to do this is probably to either (i) use an object or (ii) have a table structure holding state and support functions. To make it into one functions you have to generalise it and seperate out the differences into either subclassable methods or hooks (if you want to do it as a module)

How can i used it as module???

can anyone help me??

Not tested obviously as I don’t have the assets, but should get the idea.

[lua]

local composer = require “composer”

local scene = composer.newScene()

local _H =  display.contentHeight

local _W = display.contentWidth

local gfx = {}

local snd = {}

local function hasCollided(obj1, obj2)

      if (obj1 == nil) then

            return false

      end

     

      if (obj2 == nil) then

            return false

      end

     

      local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin

      local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax

      local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin

      local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax

     

      return (left or right) and (up or down)

     

end

local function dragObject(event)

      local target = event.target

      local phase = event.phase

     

      if (event.phase == “began”) then

            local parent = target.parent

            display.getCurrentStage(): setFocus(target)

            target.isFocus = true

            target.x0 = event.x - target.x

            target.y0 = event.y - target.y

            target.xStart = target.x

            target.yStart = target.y

            target:toFront()

           

      elseif(target.isFocus) then

            if (phase == “moved”) then

                  target.x = event.x - target.x0

                  target.y = event.y - target.y0

                  if (hasCollided(target, target.shadow )) then

                        transition.to(target, {time = 250 , x = target.shadow.x, y = target.shadow.y})

                        audio.play(snd.sound2)

                        composer.gotoScene(“nextScene2”,“fade”, 100 )

                        phase = “cancelled”

                  else

                 

                  end

            elseif(phase == “ended” or phase == “cancelled”) then

                  display.getCurrentStage():setFocus(nil)

                  target.isFocus = false

           

            end

      end

return true

end

function scene:create(event)

      local sceneGroup = self.view

     

      local i = display.newImage(“sceneBG.png”)

      i.x = 50

      i.y = 50

      sceneGroup:insert(i)

      gfx.bg = i

     

      local i = display.newText(“Game na ba?”, 250 , 250 , nil, 16 )

      i:setFillColor ( 0 , 0 , 0 )

      i.x = 50

      i.y = 75

      sceneGroup:insert(i)

      gfx.message = i

     

      local i = display.newImage(“house_red.png”)

      i.x = 50

      i.y = 150

      sceneGroup:insert(i)

      i.myName = “house”

      i:addEventListener(“touch”, dragObject)

      gfx.house = i

     

      local i = display.newImage(“house_red.png”)

      i.x = 160

      i.y = 150

      i:setFillColor( 50 , 50 , 50 )

      i.alpha = 0.75

      sceneGroup:insert(i)

      i.myName = “house”

      i.collision = onLocalCollision

      gfx.house.shadow = i

     

      local i = display.newImage(“ball_blue.png”)

      i.x = 50

      i.y = 250

      sceneGroup:insert(i)

      i.myName = “ball”

      i:addEventListener(“touch”, dragObject)

      gfx.ball = i

      local i = display.newImage(“ball_blue.png”)

      i.x = 160

      i.y = 250

      i:setFillColor( 50 , 50 , 50 )

      i.alpha = 0.75

      sceneGroup:insert(i)

      i.myName = “ball”

      i.collision = onLocalCollision

      gfx.ball.shadow = i

     

      local i = display.newImage(“soda_can.png”)

      i.x = 50

      i.y = 350

      sceneGroup:insert(i)

      i.myName = “can”

      i:addEventListener(“touch”, dragObject)

      gfx.can = i

     

      local i = display.newImage(“soda_can.png”)

      i.x = 160

      i.y = 350

      i:setFillColor( 50 , 50 , 50 )

      i.alpha = 0.75

      sceneGroup:insert(i)

      i.myName = “can”

      i.collision = onLocalCollision

      gfx.can.shadow = i

     

end

function scene:show(event)

      local sceneGroup = self.view

     

      snd.sound2 = audio.loadSound(“boing_wav.wav”)

     

      gfx.house.shadow:addEventListener(“collision”, gfx.house.shadow)

      gfx.ball.shadow:addEventListener(“collision”, gfx.ball.shadow)

      gfx.can.shadow:addEventListener(“collision”, gfx.can.shadow)

                  – ARE THESE NEEDED??

end  

function scene:hide(event)

     

      local sceneGroup = self.view

     

      gfx.house.shadow:removeEventListener(“collision”, gfx.shadowHouse)

      gfx.ball.shadow:removeEventListener(“collision”, gfx.ball.shadow)

      gfx.can.shadow:removeEventListener(“collision”, gfx.can.shadow)

                  – ARE THESE NEEDED??

end

function scene:destroy(event)

      local sceneGroup = self.view

end

scene:addEventListener(“create”, scene)

scene:addEventListener(“show”, scene)

scene:addEventListener(“hide”, scene)

scene:addEventListener(“destroy”, scene)

return scene

[/lua]

Thanks nick_sherman i’ll try it. .  I will update for possible errors. =)

only the house can trigger the next scene?

[lua]

if target.myName == “house” then

  composer.gotoScene(“nextScene2”,“fade”, 100)

end

[/lua]

yes but it is possible that only one function (which is drag function) can handle 3 objects?

A function can handle a million objects if you wanted, just give them an id to differentiate between them. In this case you have used ‘myName’ so you can apply different behaviour to the different objects if required.

hmmmm? I see I am trying to make a simulation game if this forte suits me well

please teach me on how I can accomplished the simulation game, I am just new at this forte

anyone?

What are you asking? If someone can teach you (for free) how to make an entire game that would take even an experienced developer anywhere between 6 and 12 months?

I would advise starting with something much simpler and building your Corona skills step by step.

not all I just need to know how will encapsulate functions and how to verify them if all the objects are in placed

Nick is right - you need to learn to walk before you can run.

I mean I do not need to have a full tutorial all i need is 2 factors:

  1. verifying the objects before going to the next process

  2. handling all functions into one (if possible)

sorry for the debate but i am not arguing I just want to know how and some alternative ways to achieve the factors

I’ve shown you above how to combine three functions into one, you just use the same principles.

thanks nick sorry for the troubles. . , so in order to check if the objects are in a right place can i use nested if?

The best way to do this is probably to either (i) use an object or (ii) have a table structure holding state and support functions. To make it into one functions you have to generalise it and seperate out the differences into either subclassable methods or hooks (if you want to do it as a module)