In this game it has a set of numbers 1-10 on a dial. I set up an array of a hit spots that are placed on top of the numbers 1-10. I made some modifications on the hitTestObject to determine if the mouse is inside a box. When the user spins the dial and lifts his finger the program is to know which number the dial is on. All the numbers seem to come out right except for 5 and 6 are switched. Can anybody point me in the right direction?
[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR
local screenW = display.contentWidth
local screenH = display.contentHeight
local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2
local lock = display.newImageRect(“lock.png”,305,413)
lock.x = 800; lock.y = 300
local lock = display.newImageRect(“lock.png”,305,413)
lock.x = 250; lock.y = 300
local numbers = display.newImageRect(“numbers.png”,254,247)
numbers.x = 250; numbers.y = 350
local dial = display.newImageRect(“dial.png”,264,267)
dial.x = 250; dial.y = 350
function hitTestObject(point, xMin, yMin, xMax, yMax)
local xInside = point.x >= xMin and point.x <= xMax
local yInside = point.y >= yMin and point.y <= yMax
return (xInside and yInside)
end
local hitPoints = {
{ x=320, y=275 },
{ x=350, y=323 },
{ x=348, y=383 },
{ x=318, y=429 },
{ x=192, y=429 },
{ x=258, y=450 },
{ x=158, y=383 },
{ x=150, y=323 },
{ x=180, y=275 },
{ x=250, y=260 }
}
for i=1,#hitPoints do
local hitSpot = display.newImageRect(“hitSpot.png”, 44, 40)
hitSpot.x = hitPoints[i].x; hitSpot.y = hitPoints[i].y
hitSpot.alpha = 0
end
local x0 , y0, x1, x2
local function angleBetween(a, b) --returns angle between 2 vectors a and b
local angleA = math.deg(math.atan(a.y/a.x))
local angleB = math.deg(math.atan(b.y/b.x))
print(angleB - angleA)
return (angleB - angleA)
end
local function turn(e)
if e.phase == “began” then
x0 = e.x
y0 = e.y
elseif e.phase == “moved” then
x1 = e.x
y1 = e.y
local vector1 = {x=x0-e.target.x, y=y0-e.target.y}
local vector2 = {x=x1-e.target.x, y=y1-e.target.y}
local angle = angleBetween(vector1,vector2)
if (angle > 90) then
angle = angle - 180
elseif (angle < -90) then
angle = angle + 180
end
e.target.rotation = e.target.rotation + angle
x0 = x1
y0 = y1
elseif e.phase == “ended” then
local hitNumber = 0
for i=1,#hitPoints do
local outcome = hitTestObject(e, hitPoints[i].x - 20, hitPoints[i].y - 22, hitPoints[i].x + 20, hitPoints[i].y + 22)
if (outcome == true) then
hitNumber = i
end
end
print(“hit” … hitNumber)
end
end
–end
dial:addEventListener(“touch”,turn) [/lua]
Thanks
Jake [import]uid: 51459 topic_id: 15998 reply_id: 315998[/import]