Collisions - Confused?

In my app there is a simple ball controlled by the accelerometer and a static goal on the other end of the screen. I want something to happen when the ball collides with the goal, such as test appearing, or eventually moving to the next level. But for now I will stick to something simple like text appearing. This is what I thought would work:

local function levelComplete(event) if (event.phase == "began" and event.other.class == "goalZone") then local winner = display.newText("You have completed the level!", 100, 150 , nil, 24) end end goal.collision = levelComplete goal:addEventListener("collision", levelComplete)

But when the ball hits the goal, it just bounces off like any other physics object. No text appears. I have been stuck on this for the past couple of days and any help would be greatly appreciated! - Alex [import]uid: 7116 topic_id: 5903 reply_id: 305903[/import]

Have you thought about removing the ball?
Try putting this in your collision code
[lua] ball:removeSelf() [import]uid: 12455 topic_id: 5903 reply_id: 20266[/import]

[lua] local removeballCollision = function(self, event)
if event.phase. == “began” then
if event.other == “goal” then
self:removeSelf()
– display text code goes here
end
end
end [import]uid: 12455 topic_id: 5903 reply_id: 20267[/import]

But, how would that help me get the text to appear? – oh wait, my bad, I sdidnt see the comment in between, 1 sec, let me try it out… [import]uid: 7116 topic_id: 5903 reply_id: 20268[/import]

local removeBallCollision = function(self, event) if event.phase. == "began" then if event.other == "goal" then self:removeSelf() local winner = display.newText("You have completed the level!", 100, 150 , nil, 24) end end end goal.collision = removeBallCollision goal:addEventListener("collision", removeBallCollision)

this gives me a syntax error [import]uid: 7116 topic_id: 5903 reply_id: 20269[/import]

try this:

set the ball with the collision listerner
[lua]-- example
local ball
local spawnBall = function ()
ball = display.newCircle(0,0, 12.5)
ball.x = 0
ball.y = 0
physics.addBody(ball, “dynamic”, {density=0.5, bounce = 0})
ball:setFillColor( 0, 0, 0, 255) – 0,0,0 is black, 255 is 100% visible
end

local removeBallCollision = function(self, event)
if event.phase == “began” then
if event.other == “goal” then
self:removeSelf()
local winner = display.newText(“You have completed the level!”, 100, 150 , nil, 24)
end
end
end
ball.collision = removeBallCollision
ball:addEventListener(“collision”, ball) [import]uid: 12455 topic_id: 5903 reply_id: 20288[/import]

Doesn’t work… anybody else have any suggestions? [import]uid: 7116 topic_id: 5903 reply_id: 20677[/import]

Here is my code:

display.setStatusBar(display.HiddenStatusBar)  
  
--add physics to the game  
local physics = require("physics")  
physics.start()  
physics.setGravity(0, 0)  
local xMot = 8  
local xMot2 = 7  
local xMot3 = 7  
  
--function to move enemy1  
local enemy1 = display.newRoundedRect(20, 120, 40, 40, 5 )  
physics.addBody(enemy1)  
moveEnemy1 = function()  
enemy1.x = enemy1.x + xMot  
if (enemy1.x \> 300) then  
 xMot = -xMot  
 end  
if (enemy1.x \< 20) then  
 xMot = -xMot  
 end  
end  
Runtime:addEventListener( "enterFrame", moveEnemy1 )  
  
--create the player  
local hero = display.newRoundedRect(140, 30, 50, 50, 15)  
hero:setFillColor(0,255,0)  
physics.addBody(hero)  
--creat means of control for character, in this case, via touch and drag  
function moveHero( event )  
 hero.x = event.x  
 hero.y = event.y  
end  
Runtime:addEventListener("touch", moveHero)  

What would I have to have text appear that says “winner” when hero collides with enemy 1? [import]uid: 7116 topic_id: 5903 reply_id: 20685[/import]