making items unique in a table in a cleaner way.

I set up a table of squares. They are all coming from the same image. I set them up so that they are presented in a circle. These squares are going to be my hit areas. So when I spin the dial it knows what number it is on. The only way that I know how to do this is manual telling each individual box where to go in the code. I know that there is a cleaner and better way of doing this with making each image unique.

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

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

local dial1Table = { “1”, “2”, “3”, “4”, “5”, “6”,“7”,“8”,“9”,“10” }

local numbers = display.newImageRect(“numbers.png”,254,247)
numbers.x = 250; numbers.y = 300

local dial = display.newImageRect(“dial.png”,264,267)
dial.x = 250; dial.y = 300

local numbers = display.newImageRect(“numbers.png”,254,247)
numbers.x = 800; numbers.y = 300

local dial2 = display.newImageRect(“dial.png”,264,267)
dial2.x = 800; dial2.y = 300

for i=1,#dial1Table do
local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 350; hitSpot.y = 275

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 350; hitSpot.y = 330

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 320; hitSpot.y = 380

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 320; hitSpot.y = 380

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 260; hitSpot.y = 400

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 200; hitSpot.y = 380

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 150; hitSpot.y = 330

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 150; hitSpot.y = 275

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 180; hitSpot.y = 225

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 250; hitSpot.y = 205

local hitSpot = display.newImage(“hitSpot.png”)
hitSpot.x = 320; hitSpot.y = 225

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

print(“print where am at”)
end

end

dial:addEventListener(“touch”,turn)
dial2:addEventListener(“touch”,turn)[/lua]

Thanks

Jake [import]uid: 51459 topic_id: 15929 reply_id: 315929[/import]

Also I am not sure how to set collisions for 10 items. I only know how to do two items… [import]uid: 51459 topic_id: 15929 reply_id: 58942[/import]

you do it for two items but in a loop, looping through the items comparing two at a time.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15929 reply_id: 58985[/import]

Thanks Oz apps I will give that a try! [import]uid: 51459 topic_id: 15929 reply_id: 59098[/import]

I set up unique names for my table of hit objects that are over the numbers… [lua]for i=1,#dial1Table do

local oneSpot = display.newImage(“hitSpot.png”)
oneSpot.x = 320; oneSpot.y = 275

local twoSpot = display.newImage(“hitSpot.png”)
twoSpot.x = 350; twoSpot.y = 323

local threeSpot = display.newImage(“hitSpot.png”)
threeSpot.x = 348; threeSpot.y = 383

local fourSpot = display.newImage(“hitSpot.png”)
fourSpot.x = 318; fourSpot.y = 429

local sixSpot = display.newImage(“hitSpot.png”)
sixSpot.x = 192; sixSpot.y = 429

local tenSpot = display.newImage(“hitSpot.png”)
tenSpot.x = 258; tenSpot.y = 450

local sevenSpot = display.newImage(“hitSpot.png”)
sevenSpot.x = 158; sevenSpot.y = 383

local eightSpot = display.newImage(“hitSpot.png”)
eightSpot.x = 150; eightSpot.y = 323

local nineSpot = display.newImage(“hitSpot.png”)
nineSpot.x = 180; nineSpot.y = 275

local tenSpot = display.newImage(“hitSpot.png”)
tenSpot.x = 250; tenSpot.y = 260

end[/lua]
I used this [lua] function hitNumObjects(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[/lua]

to detect if to objects are touching… Inside the turn event to turn each dial I added something like this… [lua]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 “ended” == phase or “cancelled” == phase then

local hitOne = hitNumObjects(t, oneSpot)
local hitTwo = hitNumObjects(t, twoSpot)

if (hitOne == true) or (hitTwo == true) then

print(“Hit one or two”)
end
end

end

Why is my print statement not printing out!!
[import]uid: 51459 topic_id: 15929 reply_id: 59115[/import]

Here I tried something a little different and got a response but not a good one… :frowning: [lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

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

local dial1Table = { “1”, “2”, “3”, “4”, “5”, “6”,“7”,“8”,“9”,“10” }

local dial2Table = { “1”, “2”, “3”, “4”, “5”, “6”,“7”,“8”,“9”,“10” }

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

local numbers = display.newImageRect(“numbers.png”,254,247)
numbers.x = 800; numbers.y = 350

local dial2 = display.newImageRect(“dial.png”,264,267)
dial2.x = 800; dial2.y = 350
for i=1,#dial1Table do

local oneSpot = display.newImage(“hitSpot.png”)
oneSpot.x = 320; oneSpot.y = 275

local twoSpot = display.newImage(“hitSpot.png”)
twoSpot.x = 350; twoSpot.y = 323

local threeSpot = display.newImage(“hitSpot.png”)
threeSpot.x = 348; threeSpot.y = 383

local fourSpot = display.newImage(“hitSpot.png”)
fourSpot.x = 318; fourSpot.y = 429

local sixSpot = display.newImage(“hitSpot.png”)
sixSpot.x = 192; sixSpot.y = 429

local tenSpot = display.newImage(“hitSpot.png”)
tenSpot.x = 258; tenSpot.y = 450

local sevenSpot = display.newImage(“hitSpot.png”)
sevenSpot.x = 158; sevenSpot.y = 383

local eightSpot = display.newImage(“hitSpot.png”)
eightSpot.x = 150; eightSpot.y = 323

local nineSpot = display.newImage(“hitSpot.png”)
nineSpot.x = 180; nineSpot.y = 275

local tenSpot = display.newImage(“hitSpot.png”)
tenSpot.x = 250; tenSpot.y = 260

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

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 one = hitTestObject(t, oneSpot)

if (one == true) then

print(“one selected”)

end
end

end

dial:addEventListener(“touch”,turn)
dial2:addEventListener(“touch”,turn)[/lua]
I got this error

Runtime error
/Users/brandxpress/Desktop/DialGame/main.lua:114: attempt to call global ‘hitTestObject’ (a nil value)
stack traceback:
[C]: in function ‘hitTestObject’
/Users/brandxpress/Desktop/DialGame/main.lua:114: in function
?: in function <?:215>

Can someone please help me get back on my FEET

Thanks [import]uid: 51459 topic_id: 15929 reply_id: 59117[/import]