My Code is not working little help plz

So, here is what’s going on i can launch the game on simulator but it looks like nothing is working as i expected . when objects collide it should display text ir remove 2 objects and start again . please help

–hide setStatusBar
display.setStatusBar(display.HiddenStatusBar)
–call for physics
local scoreTxt = 0
local physics = require “physics”
physics.start()

**physics.setGravity(0,0)
–setting background image center
local centerX = display.contentCenterX
local centerY = display.contentCenterY

local background = display.newImage(“background.jpg”)
background.y = centerY
background.x = centerX

–playeroneface
local cake
local function createCake()
  cake = display.newImage(“Cake.png”)
  cake.x = 1200
  cake.y = 240
  physics.addBody(cake, “dynamic”)
end
createCake()

– creating happy
local happy
local function createHappy()

     --playeroneface
     happy = display.newImage(“happygirl.png”)
     happy.x = 100
     happy.y = 100
     physics.addBody(happy, “dynamic”)

end

createHappy()

local angry = display.newImage(“angrymen1.png”)
angry.x = 1000
angry.y = 420
physics.addBody(angry, “static”)

local angry2 = display.newImage(“angrymen2.png”)
angry2.x = 830
angry2.y = 520
physics.addBody(angry2, “static”)

function touchScreen(event)
   if event.phase == “began” then
    --print(event.x)
    transition.to(happy, {time=1000, x=event.x, y=event.y})
    end
end
Runtime:addEventListener(“touch”, touchScreen)
   

function moveAngry()
  transition.to(angry,{time=1000, x=math.random(150,1200),y=math.random(60,580), onComplete=moveAngry})
  transition.to(angry2,{time=1000, x=math.random(180,1200),y=math.random(90,580)})
end
moveAngry()

function onCollision (event)

–  testing code
 local function onCollision(self, event)
  if event.phase == “began” then
       if (event.happy ==  “cake”) then
       local text = display.newText(“you just hit the cake”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)
    else

  happy:removeSelf()
  cake:removeSelf()
 
  local text = display.newText(“Game Over. Try Again”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)

  timer.performWithDelay(3000, function() text:removeSelf(); text = nil; cake = nill end)

  timer.performWithDelay(4000, function()

         --reset things after the collision

        createHappy();
        createCake();

        angry.x = 880
        angry.y = 600

        angry2.x = 830
        angry2.y = 520
        
       end)
     end
    end
   end
end

Runtime:addEventListener(“collision”, onCollision)
–Runtime:addEventListener(“collision”, cakeCollision)**
 

Try this.

When you declare your display objects, give them another property called myName…like this.

cake.myName = “cake”

happy.myName = “happy”

Then for your collision, use this.

local function onGlobalCollision( event )

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

        if (event.object1.myName == “happy” and event.object2.myName== “cake”) then

             local text = display.newText(“you just hit the cake”, display.contentCenterX, display.contentCenterY, native.systemFontBold,                  50)

       

    else

  happy:removeSelf()

  cake:removeSelf()

  local text = display.newText(“Game Over. Try Again”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)

  timer.performWithDelay(3000, function() text:removeSelf(); text = nil; cake = nil end)

  timer.performWithDelay(4000, function()

         --reset things after the collision

        createHappy();

        createCake();

        angry.x = 880

        angry.y = 600

        angry2.x = 830

        angry2.y = 520

        

       end)

     end

    end

  

end

Runtime:addEventListener(“collision”, onGlobalCollision)

It’s a start. There’s more you’ll need to work out, specifically with timers, but this will get you started.

Also, you called a global onCollision function before the localized one…get rid of it.

YOUR CODE, revised.

–hide setStatusBar

display.setStatusBar(display.HiddenStatusBar)

–call for physics

local scoreTxt = 0

local physics = require “physics”

physics.start()

physics.setGravity(0,0)

–setting background image center

local centerX = display.contentCenterX

local centerY = display.contentCenterY

local background = display.newImage(“background.png”)

background.y = centerY

background.x = centerX

–playeroneface

local cake

local function createCake()

  cake = display.newImage(“cake.png”)

  cake.x = 240

  cake.y = 240

  cake.myName = “cake”

  physics.addBody(cake, “dynamic”)

end

createCake()

– creating happy

local happy

local function createHappy()

     --playeroneface

     happy = display.newImage(“happygirl.png”)

     happy.x = 100

     happy.y = 100

     happy.myName = “happy”

     physics.addBody(happy, “dynamic”)

end

createHappy()

local angry = display.newImage(“angrymen1.png”)

angry.x = 1000

angry.y = 420

physics.addBody(angry, “static”)

local angry2 = display.newImage(“angrymen2.png”)

angry2.x = 830

angry2.y = 520

physics.addBody(angry2, “static”)

function touchScreen(event)

   if event.phase == “began” then

    --print(event.x)

    transition.to(happy, {time=1000, x=event.x, y=event.y})

    end

end

Runtime:addEventListener(“touch”, touchScreen)

   

function moveAngry()

  transition.to(angry,{time=1000, x=math.random(150,1200),y=math.random(60,580), onComplete=moveAngry})

  transition.to(angry2,{time=1000, x=math.random(180,1200),y=math.random(90,580)})

end

moveAngry()

–  testing code

local function onGlobalCollision( event )

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

        if (event.object1.myName == “happy” and event.object2.myName== “cake”) then

             local text = display.newText(“you just hit the cake”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)

       

    else

  happy:removeSelf()

  cake:removeSelf()

  local text = display.newText(“Game Over. Try Again”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)

  timer.performWithDelay(3000, function() text:removeSelf(); text = nil; cake = nil end)

  timer.performWithDelay(4000, function()

         --reset things after the collision

        createHappy();

        createCake();

        angry.x = 880

        angry.y = 600

        angry2.x = 830

        angry2.y = 520

        

       end)

     end

    end

  

end

Runtime:addEventListener(“collision”, onGlobalCollision)

Thanks James Sherburne

Try this.

When you declare your display objects, give them another property called myName…like this.

cake.myName = “cake”

happy.myName = “happy”

Then for your collision, use this.

local function onGlobalCollision( event )

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

        if (event.object1.myName == “happy” and event.object2.myName== “cake”) then

             local text = display.newText(“you just hit the cake”, display.contentCenterX, display.contentCenterY, native.systemFontBold,                  50)

       

    else

  happy:removeSelf()

  cake:removeSelf()

  local text = display.newText(“Game Over. Try Again”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)

  timer.performWithDelay(3000, function() text:removeSelf(); text = nil; cake = nil end)

  timer.performWithDelay(4000, function()

         --reset things after the collision

        createHappy();

        createCake();

        angry.x = 880

        angry.y = 600

        angry2.x = 830

        angry2.y = 520

        

       end)

     end

    end

  

end

Runtime:addEventListener(“collision”, onGlobalCollision)

It’s a start. There’s more you’ll need to work out, specifically with timers, but this will get you started.

Also, you called a global onCollision function before the localized one…get rid of it.

YOUR CODE, revised.

–hide setStatusBar

display.setStatusBar(display.HiddenStatusBar)

–call for physics

local scoreTxt = 0

local physics = require “physics”

physics.start()

physics.setGravity(0,0)

–setting background image center

local centerX = display.contentCenterX

local centerY = display.contentCenterY

local background = display.newImage(“background.png”)

background.y = centerY

background.x = centerX

–playeroneface

local cake

local function createCake()

  cake = display.newImage(“cake.png”)

  cake.x = 240

  cake.y = 240

  cake.myName = “cake”

  physics.addBody(cake, “dynamic”)

end

createCake()

– creating happy

local happy

local function createHappy()

     --playeroneface

     happy = display.newImage(“happygirl.png”)

     happy.x = 100

     happy.y = 100

     happy.myName = “happy”

     physics.addBody(happy, “dynamic”)

end

createHappy()

local angry = display.newImage(“angrymen1.png”)

angry.x = 1000

angry.y = 420

physics.addBody(angry, “static”)

local angry2 = display.newImage(“angrymen2.png”)

angry2.x = 830

angry2.y = 520

physics.addBody(angry2, “static”)

function touchScreen(event)

   if event.phase == “began” then

    --print(event.x)

    transition.to(happy, {time=1000, x=event.x, y=event.y})

    end

end

Runtime:addEventListener(“touch”, touchScreen)

   

function moveAngry()

  transition.to(angry,{time=1000, x=math.random(150,1200),y=math.random(60,580), onComplete=moveAngry})

  transition.to(angry2,{time=1000, x=math.random(180,1200),y=math.random(90,580)})

end

moveAngry()

–  testing code

local function onGlobalCollision( event )

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

        if (event.object1.myName == “happy” and event.object2.myName== “cake”) then

             local text = display.newText(“you just hit the cake”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)

       

    else

  happy:removeSelf()

  cake:removeSelf()

  local text = display.newText(“Game Over. Try Again”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)

  timer.performWithDelay(3000, function() text:removeSelf(); text = nil; cake = nil end)

  timer.performWithDelay(4000, function()

         --reset things after the collision

        createHappy();

        createCake();

        angry.x = 880

        angry.y = 600

        angry2.x = 830

        angry2.y = 520

        

       end)

     end

    end

  

end

Runtime:addEventListener(“collision”, onGlobalCollision)

Thanks James Sherburne