Another tables question - calling class with event arguments

Hi, another table question. Im getting a basic understanding but am struggling with the below so all help appreciated.

Ok so I have a touch event to fire my weapons, I have various classes for each weapon. Instead of using IF statements to check the variable on fire I would like to use the variable (number) to call the particular class.

Here is the function working, it only calls one class (Missile):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function fireMissile(event)
                if event.phase == "began" and control == false then
                
                
                        local i = math.random(2) 
                        audio.play(pistolSounds[2])
                
                -- create missile, aim at event target, spawn from character
                        local missile = Missile.new(event.x, event.y, balloon.x, balloon.y)
                        missiles:insert(missile)
                        
 
                        setMass(-1)
                        setScore(1)     
                end
        end

answered my own question

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
local weapons = {}
        weapons[1] = require("Bullet") --reference to instaniated object
        weapons[2] = require("Missile") --reference to instaniated object
        
    local grade = 2
 
        
        function fireMissile(event)
                if event.phase == "began" and control == false then
                
                
                        local i = math.random(2) 
                        audio.play(pistolSounds[2])
                        
                
                                
                
 
                         --make object 1 do something
                
                -- create missile, aim at event target, spawn from character
                        local missile = weapons[1].new(event.x, event.y, balloon.x, balloon.y)
                        missiles:insert(missile)
                        
 
                        setMass(-1)
                        setScore(1)     
                end
        end

answered my own question

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
local weapons = {}
        weapons[1] = require("Bullet") --reference to instaniated object
        weapons[2] = require("Missile") --reference to instaniated object
        
    local grade = 2
 
        
        function fireMissile(event)
                if event.phase == "began" and control == false then
                
                
                        local i = math.random(2) 
                        audio.play(pistolSounds[2])
                        
                
                                
                
 
                         --make object 1 do something
                
                -- create missile, aim at event target, spawn from character
                        local missile = weapons[1].new(event.x, event.y, balloon.x, balloon.y)
                        missiles:insert(missile)
                        
 
                        setMass(-1)
                        setScore(1)     
                end
        end
views:1584 update:2012/2/9 11:37:26
corona forums © 2003-2011