Number Dial Game

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]

Looks like I solved my own issue… Just had the coronations switched… Does anybody now how I can spit out text on the screen telling the user what number it is? [import]uid: 51459 topic_id: 15998 reply_id: 59340[/import]

[lua]display.newText(numberHere, x, y, font, size)[/lua]

Assuming you already have;
[lua]local numberHere = 12345[/lua]

Or something like that. [import]uid: 52491 topic_id: 15998 reply_id: 59467[/import]

Wow thanks Peach! [import]uid: 51459 topic_id: 15998 reply_id: 59479[/import]

Ok so I got the number to show up! :slight_smile:

But I don’t know how to make that number go away when another number is selected. :frowning:

Heres my updated code… [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 numbers = display.newImageRect(“numbers.png”,596,606)
numbers.x = 250; numbers.y = 253

local hitNumber = 0

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=310, y=178 },
{ x=340, y=220 },
{ x=340, y=270 },
{ x=305, y=312 },
{ x=258, y=328 },
{ x=203, y=315 },
{ x=165, y=280 },
{ x=165, y=228 },
{ x=185, y=181 },
{ x=249, y=160 }
}

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 selectNumber(e)
if e.phase == “began” then
x0 = e.x
y0 = e.y

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

local hitNumber = display.newText(hitNumber, 0, 0, native.systemFontBold, 40)
hitNumber:setTextColor( 255,0,0 )
hitNumber.x = screenW/2; hitNumber.y = screenH/2

– print(“hit” … hitNumber)
end
end

numbers:addEventListener(“touch”,selectNumber)[/lua] [import]uid: 51459 topic_id: 15998 reply_id: 59498[/import]

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  

In this snippet, you have two things that may be issues. First of all if Lua is like most languages, declaring it local inside the for loop only makes that variable available inside the for loop.

Try:

local hitSpot  
for i=1,#hitPoints do  
 hitSpot = display.newImageRect("hitSpot.png", 44, 40)  
 hitSpot.x = hitPoints[i].x; hitSpot.y = hitPoints[i].y   
 hitSpot.alpha = 0  
end  

Secondly you are overwriting hitSpot each time you run through the loop. This is a memory leak. And more importantly you don’t have references to any of the hitSpots but the last one.
[import]uid: 19626 topic_id: 15998 reply_id: 59510[/import]

As for the 2nd issue, making the number go away, you have two choices:

  1. Declare the numberText display object once (hitHumber) and then change its text property when you want the number to change or…

  2. Create the display object, then when you want to change the number, remove the previous display object and create a new one.

The first option is above and by far the most efficient way. If you want to make the text disappear while inbetween selections you can always set the text property to an empty string or set the isVisible property to false.

[import]uid: 19626 topic_id: 15998 reply_id: 59511[/import]

Ok so how do I avoid that memory leak and still accomplish the same thing… Also I thought I was doing exactly what you where saying… Declaring the numberText display once and then changing the text property when I want the number to change…
[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 numbers = display.newImageRect(“numbers.png”,464,471)
numbers.x = 250; numbers.y = 253

local hitSpot

local numberText = 0

local hitNumber = display.newText(string.format(“Number: %02d”, 0), 0, 0, native.systemFontBold, 40)
hitNumber:setTextColor( 255,0,0 )
hitNumber.x = screenW/2; hitNumber.y = screenH/2

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=310, y=178 },
{ x=340, y=220 },
{ x=340, y=270 },
{ x=305, y=312 },
{ x=258, y=328 },
{ x=203, y=315 },
{ x=165, y=280 },
{ x=165, y=228 },
{ x=185, y=181 },
{ x=249, y=160 }
}

for i=1,#hitPoints do
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 selectNumber(e)
if e.phase == “began” then
x0 = e.x
y0 = e.y

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

numberText.text = string.format(“Number: %02d”, hitNumber)

print(“hit” … hitNumber)
end
end

numbers:addEventListener(“touch”,selectNumber)[/lua]

But I keep getting that same error…
:frowning: [import]uid: 51459 topic_id: 15998 reply_id: 59518[/import]

hitSpot needs to be an array of hitSpots

local hitSpot = {} for i=1,#hitPoints do hitSpot[i] = display.newImageRect("hitSpot.png", 44, 40) hitSpot[i].x = hitPoints[i].x; hitSpot.y = hitPoints[i].y hitSpot[i].alpha = 0 end [import]uid: 19626 topic_id: 15998 reply_id: 59524[/import]

Ok well I eliminated the errors. But nothing is happening…

Is this correct?

[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 numbers = display.newImageRect(“numbers.png”,464,471)
numbers.x = 250; numbers.y = 253

local hitSpot = {}

local hitNumber = display.newText(string.format(“Number: %02d”, 0), 0, 0, native.systemFontBold, 40)
hitNumber:setTextColor( 255,0,0 )
hitNumber.x = screenW/2; hitNumber.y = screenH/2
hitNumber = 0

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=310, y=178 },
{ x=340, y=220 },
{ x=340, y=270 },
{ x=305, y=312 },
{ x=258, y=328 },
{ x=203, y=315 },
{ x=165, y=280 },
{ x=165, y=228 },
{ x=185, y=181 },
{ x=249, y=160 }
}

for i=1,#hitPoints do
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 selectNumber(e)
if e.phase == “began” then
x0 = e.x
y0 = e.y

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

hitSpot.text = string.format(“Number: %02d”, hitNumber)

print(“hit” … hitNumber)
end
end

numbers:addEventListener(“touch”,selectNumber)[/lua] [import]uid: 51459 topic_id: 15998 reply_id: 59528[/import]

What is hitSpot?
What is hitNumber?

Why are you trying to set the .text attribute on an image? Thats something for display.newText objects.

Also I see you’ve made hitSpot an array, but you’re not putting anything into the array values.

You are also trying to use “hitNumber” as two different things. One place you have it as a display.newText object in another you’re treating it like an integer.

[import]uid: 19626 topic_id: 15998 reply_id: 59537[/import]

I DID IT!! I don’t know what I was thinking before… :confused:

[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 numbers = display.newImageRect(“numbers.png”,464,471)
numbers.x = 250; numbers.y = 253

local hitSpot = {}

local numberText = display.newText(string.format(“Number: %02d”, 0), 0, 0, native.systemFontBold, 40)
numberText:setTextColor( 255,0,0 )
numberText.x = screenW/2; numberText.y = screenH/2

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=310, y=178 },
{ x=340, y=220 },
{ x=340, y=270 },
{ x=305, y=312 },
{ x=258, y=328 },
{ x=203, y=315 },
{ x=165, y=280 },
{ x=165, y=228 },
{ x=185, y=181 },
{ x=249, y=160 }
}

for i=1,#hitPoints do
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 selectNumber(e)
if e.phase == “began” then
x0 = e.x
y0 = e.y

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

numberText.text = string.format(“Number: %02d”, hitNumber)

print(“hit” … hitNumber)
end
end

numbers:addEventListener(“touch”,selectNumber)[/lua] [import]uid: 51459 topic_id: 15998 reply_id: 59538[/import]

Now I am not fully understanding the hitSpot array I thought I was assigning values in this area

[lua]for i=1,#hitPoints do
hitSpot = display.newImageRect(“hitSpot.png”, 44, 40)
hitSpot.x = hitPoints[i].x; hitSpot.y = hitPoints[i].y
hitSpot.alpha = 0
end
[/lua] [import]uid: 51459 topic_id: 15998 reply_id: 59539[/import]