whats the best way to combine two tables

What I have is one set of numbers 1-10 and another set of numbers 1-10… when I click on the left side of numbers the screen tells me what number it is… I want to incorporate the other set of numbers to do the same thing but I am having a little trouble… I also want to be able to add the two numbers that where selected and get an end result

[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 = 243

local numbers2 = display.newImageRect(“numbers.png”,464,471)
numbers2.x = 750; numbers2.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 = 500; numberText.y = 400

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 }
}

–[[
local hitPoints = {
{ x=810, y=188 },
{ x=840, y=233 },
{ x=840, y=280 },
{ x=805, y=322 },
{ x=755, y=340 },
{ x=700, y=325 },
{ x=660, y=285 },
{ x=660, y=235 },
{ x=685, y=188 },
{ x=750, y=170 }
}
]]

for i=1,#hitPoints do
hitSpot = display.newImageRect(“hitSpot.png”, 44, 40)
hitSpot.x = hitPoints[i].x; hitSpot.y = hitPoints[i].y
hitSpot.alpha = .3
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: 16043 reply_id: 316043[/import]

Step 1 done :slight_smile: Which was make two tables work the same way!!
[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 number1 = display.newImageRect(“numbers.png”,464,471)
number1.x = 250; number1.y = 243

local number2 = display.newImageRect(“numbers.png”,464,471)
number2.x = 750; number2.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 = 500; numberText.y = 400

local number2Text = display.newText(string.format(“Number: %02d”, 0), 0, 0, native.systemFontBold, 40)
number2Text:setTextColor( 255,0,0 )
number2Text.x = 500; number2Text.y = 470

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 t1 = {
{ 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 }
}

local t2 = {
{ x=810, y=188 },
{ x=840, y=233 },
{ x=840, y=280 },
{ x=805, y=322 },
{ x=755, y=340 },
{ x=700, y=325 },
{ x=660, y=285 },
{ x=660, y=235 },
{ x=685, y=188 },
{ x=750, y=170 }
}

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

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

local function select1Number(e)

if e.phase == “ended” then

local hitNumber = 0
for i=1,#t1 do

local outCome = hitTestObject(e, t1[i].x - 20, t1[i].y - 22, t1[i].x + 20, t1[i].y + 22)

if (outCome == true) then
hitNumber = i
end
end

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

print(“hit” … hitNumber)
end
end

local function select2Number(e)

if e.phase == “ended” then

local hit2Number = 0
for i=1,#t2 do

local outCome = hitTestObject(e, t2[i].x - 20, t2[i].y - 22, t2[i].x + 20, t2[i].y + 22)

if (outCome == true) then
hit2Number = i
end
end

number2Text.text = string.format(“Number: %02d”, hit2Number)

print(“hit” … hit2Number)
end
end

number1:addEventListener(“touch”,select1Number)
number2:addEventListener(“touch”,select2Number)[/lua]

Now does anybody have any idea how I can combine the two tables so that when both are clicked it will output what both numbers are = too

for example when I click on a 10 and I click on a 5 the output should be 15
is this possible?

Thanks
[import]uid: 51459 topic_id: 16043 reply_id: 59594[/import]

online I found that this function joins the tables together…
[lua]function joinTables(t1, t2)
for k,v in ipairs(t2) do table.insert(t1, v) end return t1
end[/lua]

But how do I add them together when both are clicked… :confused:

thanks
Jake
[import]uid: 51459 topic_id: 16043 reply_id: 59596[/import]

have variables, var1 and var2
when the user clicks on one, set var1 with the value and when the user clicks on the other, set var2 with the value, and at the end of each of the clicks, calculate the total, so if var2 is nil or 0, the total will be the value of var1 alone.

hope that makes sense to you,

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16043 reply_id: 59661[/import]

Thanks JayantV,

That makes perfect sense

I will give that a whirl.
Jake [import]uid: 51459 topic_id: 16043 reply_id: 59716[/import]

ok well nothing is happening… Do I have this set up right? How do I set up a print statement telling me my result?

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

local number2 = display.newImageRect(“numbers.png”,464,471)
number2.x = 750; number2.y = 253

local hitSpot = {}

local var1 = 0
local var2 = 0

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

local number2Text = display.newText(string.format(“Number: %02d”, 0), 0, 0, native.systemFontBold, 40)
number2Text:setTextColor( 255,0,0 )
number2Text.x = 500; number2Text.y = 470

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 t1 = {
{ 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 }
}

local t2 = {
{ x=810, y=188 },
{ x=840, y=233 },
{ x=840, y=280 },
{ x=805, y=322 },
{ x=755, y=340 },
{ x=700, y=325 },
{ x=660, y=285 },
{ x=660, y=235 },
{ x=685, y=188 },
{ x=750, y=170 }
}

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

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

local function numberSet1(e)

if e.phase == “ended” then

local hitNumber = 0
for i=1,#t1 do

local outCome = hitTestObject(e, t1[i].x - 20, t1[i].y - 22, t1[i].x + 20, t1[i].y + 22)

if (outCome == true) then
hitNumber = i
end
end

numberText.text = string.format(“Number: %02d”, hitNumber)
var1 = nil
print(“hit” … hitNumber)
end
end

local function numberSet2(e)

if e.phase == “ended” then

local hit2Number = 0
for i=1,#t2 do

local outCome = hitTestObject(e, t2[i].x - 20, t2[i].y - 22, t2[i].x + 20, t2[i].y + 22)

if (outCome == true) then
hit2Number = i
end
end

number2Text.text = string.format(“Number: %02d”, hit2Number)
var2 = nil
print(“hit” … hit2Number)

if (var2 == nil) or (var2 == 0) then

var1 = var1

end
end

end[/lua] [import]uid: 51459 topic_id: 16043 reply_id: 59727[/import]

  1. If you are going to use touch only for the ended phase, then use a tap instead.

  2. I do not know what you are trying to do with making var1 and var2 as nil, so the logic is

local res, var1, var2 = 0,0,0  
  
local function numberSet1(e)  
 for i=1,#t1 do  
 hitNumber = 0  
 outcome = hitTestObject(...)  
 if outcome == true then  
 hitNumber = i  
 break --used to stop looping through the other items  
 end  
 end  
 numberText.text = string.format(...)  
 var1 = hitNumber  
 res = tonumber(var1) + tonumber(var2)  
 print("TOTAL " .. res)  
end  
  
local function numberSet2(e)  
 for i=1,#t2 do  
 hit2Number = 0  
 outcome = hitTestObject(...)  
 if outcome == true then  
 hit2Number = i  
 break --used to stop looping through the other items  
 end  
 end  
 number2Text.text = string.format(...)  
 var2 = hit2Number  
 res = tonumber(var1) + tonumber(var2)  
 print("TOTAL " .. res)  
end  
  

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16043 reply_id: 59728[/import]

Thank you so much JayantV…
That was exactly what I need!!

It worked out great…

Just a simple question… Why does the output print out several times? Is it because its running through the whole function until the answer pops up?

Thanks again

Jake [import]uid: 51459 topic_id: 16043 reply_id: 59738[/import]