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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 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 ) |
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,
?:)
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]?
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