hitTestObject not working!

What I have working:

Player clicks ball. Ball moves toward pins and fades off…

What I don’t have working:

When ball hits pins output window is suppose to tell me “Ball hit pins”

Any help would be much appreciated

[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local screenW = display.contentWidth
local screenH = display.contentHeight

local loqsprite = require(‘loq_sprite’)

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

local lane = display.newImageRect(“lane.png”, 897, 645)
lane.x = 510; lane.y = 400

local pin = {}

local pinIcons = {}
local pinDisplay = 10

local i

for i = 1, pinDisplay do
pinIcons[i] = display.newImageRect(“pin.png”, 52, 176)
pinIcons[i].x = 50 + (pinIcons[i].contentWidth * (i - 1))
pinIcons[i].y = 100 – start at 10,10

end

local t1 = {

{ x=720, y=264 },
{ x=760, y=290 },
{ x=795, y=300 },
{ x=830, y=320 },
{ x=720, y=300 },
{ x=750, y=320 },
{ x=790, y=335 },
{ x=720, y=320 },
{ x=750, y=340 },
{ x=700, y=350 }
}

for i = 1,#t1 do
pin[i] = display.newImageRect(“pin.png”, 52, 176)
pin[i].x = t1[i].x; pin[i].y = t1[i].y
end

local bfactory = loqsprite.newFactory(“bowlingBall”)
local bowlingBall = bfactory:newSpriteGroup()
bowlingBall.x = 120 ; bowlingBall.y = 580

local hitArea = display.newImageRect(“hitSpot.png”, 52, 176)
hitArea.x = 700; hitArea.y = 350
hitArea.alpha = 0

function removeBall()
transition.to( bowlingBall, {time = 500, x = 950, y = 220, alpha = 0})

end

function hitTestObjects(obj1, obj2)
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

rollBall = function( event )

local touch = event.phase

if ( touch == “ended” ) then

transition.to( bowlingBall, { time=1500, x= 750, y= 390, onComplete = removeBall } )
bowlingBall:play(“aniBowlingBall roll”)

local knockPins = hitTestObjects(t, hitArea)

if (knockPins == true) then

print(“Ball hit pins”)

end
end
end

bowlingBall:addEventListener( “touch”, rollBall ) [/lua] [import]uid: 51459 topic_id: 16294 reply_id: 316294[/import]

One of the common mistakes, when the object becomes transparent, it stops to be hittestable, so you have to exclusively set it to isHitTestable and it will start to work.

add this around line 25 in your code above…

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16294 reply_id: 60677[/import]

I set both objects alpha = 1 and took out the onComplete = romoveBall

ran a test and it still doesn’t work!

I also tried to do what you suggested and nothing happend

Do I set isHitTestable for the ball, hitArea and pin[i]?

[import]uid: 51459 topic_id: 16294 reply_id: 60681[/import]

I figured out the issue!! It was because I have the ball hitting it in 15 seconds… So I had to set up a timer for the hitTestObject function so that it knows that the bowling ball is going to hit it in 15 sec!! BAM [import]uid: 51459 topic_id: 16294 reply_id: 60688[/import]