Collision Function Help

When the bucket drops, I need it to disappear. Help me implement that into my code please.

--\> Physics  
local physics = require("physics")  
physics.start()  
physics.setGravity( 0, 1) -- .01 m/s\*s in the positive x direction   
physics.setScale(200) -- 200 pixels per meter  
  
--local sky = display.newImage("bg2.png")  
  
local ground = display.newImage("ground.png", true)  
ground.y = display.contentHeight - ground.contentHeight/2  
physics.addBody( ground, "static", { friction = 1.0, bounce = 0, density = 1.0 })  
  
--Constants for the level  
\_H = display.contentHeight;  
\_W = display.contentWidth;  
mRand = math.random;  
o = 0;  
time\_remain = 10;  
time\_up = false;  
total\_buckets = 3;  
ready = false;  
  
local display\_txt = display.newText("Wait", 0, 0, native.systemFont, 16\*2);  
display\_txt.xScale = 1; display\_txt.yScale = 1;  
display\_txt:setReferencePoint(display.BottomLeftReferencePoint);  
display\_txt.x = 20; display\_txt.y = \_H-20;  
  
local countdowntxt = display.newText(time\_remain, 0, 0, native.systemFont, 16\*2);  
countdowntxt.xScale = 1; countdowntxt.yScale = 1;  
countdowntxt:setReferencePoint(display.BottomRightReferencePoint);  
countdowntxt.x = \_W-20; countdowntxt.y = \_H-20;  
  
local function winLose(condition)  
 if(condition == "win") then  
 display\_txt.text = "WIN!";  
 elseif(condition == "fail") then  
 display\_txt.text = "FAIL!";  
 end  
end  
  
local function trackBuckets(obj)  
 obj:removeSelf();  
 o = o-1;  
  
 if(time\_up ~= true) then  
 --If all the orbs are removed from the display  
 if(o == 0) then  
 timer.cancel(gametmr);  
 winLose("win");  
 end  
 end  
end  
  
local function countDown(e)  
 if(time\_remain == 10) then  
 ready = true;  
 display\_txt.text = "Go!";  
 end  
 time\_remain = time\_remain - 1;  
 countdowntxt.text = time\_remain;  
  
 if(time\_remain == 0) then  
 time\_up = true;  
  
 if(o ~= 0) then  
 display\_txt.text = "FAIL!";  
 ready = false;  
 end  
  
 end  
end  
  
local function spawnBucket()  
 local bucket = display.newImageRect("redpaintbucket.png", 72, 88);  
 bucket:setReferencePoint(display.CenterReferencePoint);  
 bucket.x = mRand(50, \_W-50); bucket.y = mRand(-20, \_H-870);  
 physics.addBody(bucket, {density=0.1, friction=0.1, bounce=0.1, radius = 9})  
  
 function bucket:touch(e)  
 if(ready == true) then  
 if(time\_up ~= true) then  
 if(e.phase == "ended") then  
 --Play the Popping sound if it was there  
 trackBuckets(self);  
 end  
 end  
 end  
  
 return true;  
 end  
  
 --Increment o for every bucket created  
 o = o+1;  
  
 bucket:addEventListener("touch", bucket);  
  
 --If all buckets created, start the timer  
 if(o == total\_buckets) then  
 gametmr = timer.performWithDelay(1000, countDown, 10);  
 else  
 ready = false;  
 end  
  
end  
tmr = timer.performWithDelay(20, spawnBucket, total\_buckets);  

Thanks a ton.

Also, if you wanna help out more (greatly appreciate it), when the objects hit the bottom ground, make it a fail (winLose) condition.

Please tell me how to do all of this. And don’t say like, “oh make a collision test” or something like that. Because that’s jibberish to me. Write it out, and tell me where to put it please. Thanks a ton. I’m a newb. [import]uid: 19768 topic_id: 6530 reply_id: 306530[/import]

bump. [import]uid: 19768 topic_id: 6530 reply_id: 22848[/import]

Fixed the Physics question. Now all I have left is for somebody to help me out with the if the object hits the ground it produces a Fail function. Thanks! [import]uid: 19768 topic_id: 6530 reply_id: 22853[/import]

Ok heck with the if it hits the ground, it’s game over.

I need help. I’ve made it so that when you win, image pops up. And if you fail, image pops up. I have director in the file. However, I need it so that when you click the image, you restart the level. Help please. [import]uid: 19768 topic_id: 6530 reply_id: 23082[/import]

Have you checkout the Ghosts Vs Monsters example?

This is a code from that game:
you can replace the button with your image instead.
[lua]-- RESTART BUTTON
local onRestartTouch = function( event )
if event.phase == “release” then
audio.play( tapSound )
local theModule = “load” … restartModule
director:changeScene( theModule )
end
end

local restartBtn = ui.newButton{
defaultSrc = “restartbtn.png”,
defaultX = 60,
defaultY = 60,
overSrc = “restartbtn-over.png”,
overX = 60,
overY = 60,
onEvent = onRestartTouch,
id = “RestartButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}

restartBtn.x = menuBtn.x + 72; restartBtn.y = 186
restartBtn.alpha = 0 [import]uid: 12455 topic_id: 6530 reply_id: 23192[/import]

I already tried that, didn’t work. I’ll try it again though. [import]uid: 19768 topic_id: 6530 reply_id: 23199[/import]

Yup, still doesn’t work. [import]uid: 19768 topic_id: 6530 reply_id: 23330[/import]

Be more specific than “doesn’t work.” Explain what you did, and explain what happens. [import]uid: 12108 topic_id: 6530 reply_id: 23343[/import]

Ok, first i’ll post the code, and then tell you what I need to happen Jhocking.

now I need this function,

local reset = display.newImage("restartbtn.png") reset.x = display.contentWidth/2 reset.y = display.contentHeight/2
to when clicked, goes to reset the level. I’ve tried all the possible combinations and it still won’t work. It’s in lines 85 - 87. Thanks

module(..., package.seeall)  
--Status bar, like power and such at top  
display.setStatusBar( display.HiddenStatusBar ) -- hide the status bar from the top  
  
local director = require("director")  
local ui = require("ui")  
  
--\> Physics  
local physics = require("physics")  
physics.start(false)  
physics.setGravity( 0, 1) -- .01 m/s\*s in the positive x direction   
physics.setScale(200) -- 200 pixels per meter  
  
--local sky = display.newImage("bg2.png")  
  
local ground = display.newImage("ground.png", true)  
ground.y = display.contentHeight - ground.contentHeight/2  
physics.addBody( ground, "static", { friction = 1.0, bounce = 0, density = 1.0 })  
  
--Constants for the level  
\_H = display.contentHeight;  
\_W = display.contentWidth;  
mRand = math.random;  
o = 0;  
time\_remain = 5;  
time\_up = false;  
total\_buckets = 3;  
ready = false;  
  
local display\_txt = display.newText("Wait", 0, 0, native.systemFont, 16\*2);  
display\_txt.xScale = 2; display\_txt.yScale = 2;  
display\_txt:setReferencePoint(display.BottomLeftReferencePoint);  
display\_txt.x = 20; display\_txt.y = \_H-20;  
  
local countdowntxt = display.newText(time\_remain, 0, 0, native.systemFont, 16\*2);  
countdowntxt.xScale = 2; countdowntxt.yScale = 2;  
countdowntxt:setReferencePoint(display.BottomRightReferencePoint);  
countdowntxt.x = \_W-20; countdowntxt.y = \_H-20;  
  
local function winLose(condition)  
 if(condition == "win") then  
 display\_txt.text = "WIN!";  
 local win = display.newImage("youwin2.png")  
 win.x = display.contentWidth/2  
 win.y = display.contentHeight/2  
 elseif(condition == "fail") then  
 display\_txt.text = "FAIL!";  
 end  
end  
  
local function trackBuckets(obj)  
 obj:removeSelf();  
 o = o-1;  
  
 if(time\_up ~= true) then  
 --If all the orbs are removed from the display  
 if(o == 0) then  
 timer.cancel(gametmr);  
 winLose("win");  
 end  
 end  
end  
  
local function countDown(e)  
 if(time\_remain == 5) then  
 ready = true;  
 display\_txt.text = "Go!";  
 end  
  
 local physics = require("physics")  
 physics.start(true)  
 physics.setGravity( 0, 1) -- .01 m/s\*s in the positive x direction   
 physics.setScale(200) -- 200 pixels per meter  
  
 time\_remain = time\_remain - 1;  
 countdowntxt.text = time\_remain;  
  
 if(time\_remain == 0) then  
 time\_up = true;  
  
 if(o ~= 0) then  
 display\_txt.text = "FAIL!";  
 ready = false;  
  
 local reset = display.newImage("restartbtn.png")  
 reset.x = display.contentWidth/2  
 reset.y = display.contentHeight/2  
 end  
  
 end  
end  
  
local function spawnBucket()  
 local bucket = display.newImageRect("redpaintbucket.png", 72, 88);  
 bucket:setReferencePoint(display.CenterReferencePoint);  
 bucket.x = mRand(50, \_W-50); bucket.y = mRand(-20, \_H-870);  
 physics.addBody(bucket, {density=0.1, friction=0.1, bounce=0.1, radius = 9})  
  
 function bucket:touch(e)  
 if(ready == true) then  
 if(time\_up ~= true) then  
 if(e.phase == "ended") then  
 --Play the Popping sound if it was there  
 trackBuckets(self);  
 end  
 end  
 end  
  
 return true;  
 end  
  
 --Increment o for every bucket created  
 o = o+1;  
  
 bucket:addEventListener("touch", bucket);  
  
 --If all buckets created, start the timer  
 if(o == total\_buckets) then  
 gametmr = timer.performWithDelay(1000, countDown, 5);  
 else  
 ready = false;  
 end  
  
end  
tmr = timer.performWithDelay(20, spawnBucket, total\_buckets);  

[import]uid: 19768 topic_id: 6530 reply_id: 23356[/import]

hey EvilSniper,

I think what I would do personally is define the reset outside of the countDown function.

example:

[lua] local reset = display.newImage(“restartbtn.png”)
reset.x = display.contentWidth/2
reset.y = display.contentHeight/2
reset.isVisible = false
reset.isBodyActive = false

– create touch function for the resetBtn
local onResetTouch = function (event)
if event.phase == “ended” then

– below is a code from Ghosts Vs Monsters to reset level
– created by Jonathan Beebe
local theModule = “load” … restartModule
director:changeScene( theModule )
end
end

reset:AddEventListener(“touch”, onResetTouch)

– inside your countDown function instead of creating the resetBtn do this

local function countDown(e)
if(time_remain == 5) then
ready = true;
display_txt.text = “Go!”;
end

local physics = require(“physics”)
physics.start(true)
physics.setGravity( 0, 1) – .01 m/s*s in the positive x direction
physics.setScale(200) – 200 pixels per meter

time_remain = time_remain - 1;
countdowntxt.text = time_remain;

if(time_remain == 0) then
time_up = true;

if(o ~= 0) then
display_txt.text = “FAIL!”;
ready = false;
reset.isVisible = true – your new code
reset.isBodyActive = true – your new code

end

end
end

– I haven’t tried it, but I think it should work.

[import]uid: 12455 topic_id: 6530 reply_id: 23408[/import]

Ok thanks for helping. I’ll try it tonight and see what happens and then let you know. Thanks for being like the only person to help write the code. Hope it works. Thanks mate! [import]uid: 19768 topic_id: 6530 reply_id: 23504[/import]

Nope. Didn’t. Comes up with a black line. And then it says Director, line 137 has a proxy, and it should be nil.

Here’s the current code with your code put in it. Let me know what you think we should do from here.

[code]
module(…, package.seeall)
–Status bar, like power and such at top
display.setStatusBar( display.HiddenStatusBar ) – hide the status bar from the top

function new()
local mainGroup()
local director = require(“director”)
local ui = require(“ui”)

–> Physics
local physics = require(“physics”)
physics.start(false)
physics.setGravity( 0, 1) – .01 m/s*s in the positive x direction
physics.setScale(200) – 200 pixels per meter

–local sky = display.newImage(“bg2.png”)

local ground = display.newImage(“ground.png”, true)
ground.y = display.contentHeight - ground.contentHeight/2
physics.addBody( ground, “static”, { friction = 1.0, bounce = 0, density = 1.0 })

–Constants for the level
_H = display.contentHeight;
_W = display.contentWidth;
mRand = math.random;
o = 0;
time_remain = 5;
time_up = false;
total_buckets = 3;
ready = false;

local display_txt = display.newText(“Wait”, 0, 0, native.systemFont, 16*2);
display_txt.xScale = 2; display_txt.yScale = 2;
display_txt:setReferencePoint(display.BottomLeftReferencePoint);
display_txt.x = 20; display_txt.y = _H-20;

local countdowntxt = display.newText(time_remain, 0, 0, native.systemFont, 16*2);
countdowntxt.xScale = 2; countdowntxt.yScale = 2;
countdowntxt:setReferencePoint(display.BottomRightReferencePoint);
countdowntxt.x = _W-20; countdowntxt.y = _H-20;

local function winLose(condition)
if(condition == “win”) then
display_txt.text = “WIN!”;
local win = display.newImage(“youwin2.png”)
win.x = display.contentWidth/2
win.y = display.contentHeight/2
elseif(condition == “fail”) then
display_txt.text = “FAIL!”;
end
end
local function trackBuckets(obj)
obj:removeSelf();
o = o-1;

if(time_up ~= true) then
–If all the orbs are removed from the display
if(o == 0) then
timer.cancel(gametmr);
winLose(“win”);
end
end
end

local reset = display.newImage(“restartbtn.png”)
reset.x = display.contentWidth/2
reset.y = display.contentHeight/2
reset.isVisible = false
reset.isBodyActive = false

local onResetTouch == function (event)
if event.phase == “ended” then
local theModule = “load” … restartModule
director:changeScene( theModule )
end
end

reset:addEventListener(“touch”, onResetTouch)

local function countDown(e)
if(time_remain == 5) then
ready = true;
display_txt.text = “Go!”;
end

local physics = require(“physics”)
physics.start(true)
physics.setGravity( 0, 1) – .01 m/s*s in the positive x direction
physics.setScale(200) – 200 pixels per meter

time_remain = time_remain - 1;
countdowntxt.text = time_remain;

if(time_remain == 0) then
time_up = true;

if(o ~= 0) then
display_txt.text = “FAIL!”;
ready = false;
reset.isVisible = true – test
reset.isBodyActive = true – test

end

end
end

local function spawnBucket()
local bucket = display.newImageRect(“redpaintbucket.png”, 72, 88);
bucket:setReferencePoint(display.CenterReferencePoint);
bucket.x = mRand(50, _W-50); bucket.y = mRand(-20, _H-870);
physics.addBody(bucket, {density=0.1, friction=0.1, bounce=0.1, radius = 9})

function bucket:touch(e)
if(ready == true) then
if(time_up ~= true) then
if(e.phase == “ended”) then
–Play the Popping sound if it was there
trackBuckets(self);
end
end
end

return true;
end

–Increment o for every bucket created
o = o+1;

bucket:addEventListener(“touch”, bucket);

–If all buckets created, start the timer
if(o == total_buckets) then
gametmr = timer.performWithDelay(1000, countDown, 5);
else
ready = false;
end

end
tmr = timer.performWithDelay(20, spawnBucket, total_buckets);
return true
end
[/code] [import]uid: 19768 topic_id: 6530 reply_id: 23507[/import]

Evil,

I know that others would say that this method may be wrong or the long way, but it works.

Use my first piece of code from our other thread to create a “Restart.lua” file and change your current restart button code to the following. And in the restart.lua file make it call back to the current level.

The current code you have from Ghosts must be tied into other parts of Jon Beebe’s code, which I haven’t been able to break apart yet. If anyone knows how Jon’s code is being executed; it would be great to see an explanation. Thx.

[code]local onResetTouch == function (event)
if event.phase == “ended” then
director:changeScene( “restart”)
end
end
[import]uid: 16527 topic_id: 6530 reply_id: 23555[/import]

Oh!!! Great idea! I’ll try that and let you know! Thanks for coming up with that idea! It actually makes way more sense then having a thousand codes in one file!

Update: Still didn’t work. Please try to just copy my code, create your own images, and run it. I wanna see if it works for you. Please. I’m begging you guys. Please.

I need this to work. I’ve tried everything. I mean everything. [import]uid: 19768 topic_id: 6530 reply_id: 23563[/import]

Oh wow rob that’s so amazing. Thank you so so so so so so much!

xxevilsniper@yahoo.com

[import]uid: 19768 topic_id: 6530 reply_id: 23575[/import]

Evil,

I got it working. You were either missing a few lines of code or you hadn’t posted them. Give me an email address, I can sent you a link for the data, if you wish.

Thanks
Rob
@Evil - Did you get the files I sent you? [import]uid: 16527 topic_id: 6530 reply_id: 23567[/import]

Replied to your email! [import]uid: 19768 topic_id: 6530 reply_id: 23807[/import]