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

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

Step 1 done :) Which was make two tables work the same way!!

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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)

online I found that this function joins the tables together..

1
2
3
function joinTables(t1, t2)
                for k,v in ipairs(t2) do table.insert(t1, v) end return t1
end

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,

?:)

Thanks JayantV,

That makes perfect sense

I will give that a whirl.

Jake

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

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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

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

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

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

views:1789 update:2011/10/18 8:54:01
corona forums © 2003-2011