Hello everyone,
I want to detect when two images hit each other on the screen. I’ve used Rob Miracles code but I am facing few problems while detection.
http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/
I have a Ball and few obstacles which are being generated randomly on the screen. I just want to destroy the ball when it collides with an obstacle.
There is uncertainty in detection code. Sometimes it work and sometimes it doesn’t.
Explaining my problem further more:
When the ball hits first few obstacles, it shows detection properly but after that it stops working for few seconds and works again and then so on.
Here is my code:
--BALL local myCircle = display.newCircle( \_W\*0.5, \_H - 100, 30 ) myCircle:setFillColor( 0.5 ) myCircle.strokeWidth = 5 myCircle:setStrokeColor( 1, 0, 0 ) --COLLISION DETECTION 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 --RANDOMLY ADDING OBSTACLES local function addBlock() block = display.newImage("images/objects/"..objectName..".png") block.x, block.y = 1 + math.random(450), -0 block.shape = objectShape blocksGroup:insert(block) block.type = "block" blocks[#blocks+1] = block end --OBSTACLE COLLISION local function blockCollisions() for i = 1, #blocks do if hasCollided(blocks[i], myCircle) then remove\_block(i) end end end myCircle:addEventListener( "touch", xDrag ) blocksTimer = timer.performWithDelay( obstaclesGenerationTime, addBlock, -1 ) Runtime:addEventListener("enterFrame", blockCollisions)
Any help would be highly appreciated.
Thanks,