I’m attempting to create a bubble shooter game. It is currently comprised of main.lua, bubble.lua and shoot.lua. When trying to call a function with a setLinearVelocity method inside it I am getting the following error back in the terminal.
shoot.lua:52: attempt to call method ‘setLinearVelocity’ (a nil value)
stack traceback:
I think its because i’m not calling the function from the shoot.lua class correctly.
shoot.lua
[lua]local shoot = {}
local shoot_mt = { __index = shoot } – metatable
display.setStatusBar( display.HiddenStatusBar )
d = 62
local physics = require (“physics”)
physics.start()
background = display.newImage( “space.jpg”, 0, 0)
shootTable = { {rb } , { bb } , { gb } }
shootBubble = display.newImage( shootTable[math.random( 1,3 )][1])
shootBubble:setReferencePoint(display.CenterReferencePoint)
shootBubble.x = w*0.5; shootBubble.y = h*0.91;
shootBubble.myName = “Blue Bubble”
physics.addBody( shootBubble, “dynamic”, { density=2.9, friction=0, bounce=0.2, radius=d/2 } )
txt = display.newText(“Click State!”, 0, 0, nil, 80)
txt.x = w*0.5; txt.y = h*0.885;
txt.xScale = 0.5; txt.yScale = 0.5;
txt:setReferencePoint(display.CenterReferencePoint)
function shoot(event)
if(event.phase == “began”) then
–Do something during the began phase
txt.text = "Began Phase “…event.x…” = x “…event.y…” = y "
display.getCurrentStage():setFocus(event.target, event.id)
startPosX = event.x; startPosY = event.y;
elseif(event.phase == “moved”) then
–Do something during the moved phase
txt.text = event.x…" = x “…event.y…” = y Moved"
elseif(event.phase == “ended”) then
–Do something when the phase has ended
txt.text = event.x…" = x “…event.y…” = y Ended"
display.getCurrentStage():setFocus(event.target, nil)
endPosX = event.x; endPosY = event.y
–shootBubble:applyLinearImpulse( (shootBubble.x - event.x), (shootBubble.y - event.y), shootBubble.x, shootBubble.y )
print(startPosX)
shootBubble:setLinearVelocity( (startPosX - endPosX)*500, (startPosY - endPosY)*500 )
end
return setmetatable( shootBubble, shoot_mt )
end
background:addEventListener(“touch”, shoot) ;
return shoot[/lua]
main.lua
[lua]local bubbleClass = --communicates with the bubble.lua class
require ( “bubble” )
local shootClass = --communicates with the shoot.lua class
require ( “shoot” )
w,h = display.contentWidth, display.contentHeight
local physics = require (“physics”)
physics.start()
Ground = display.newImage(“timber.png”, w*0.00001, h*0.95)
physics.addBody( Ground, “static”,{ density=5, friction=0.1, bounce=0.1 } )
d = 62;
level = display.newGroup()
matrix = { { 5, 2, 3, 4, 5, 6, 2, 3, 4, 0},
{ 6, 2, 5, 3, 2, 1, 2, 3, 4, 0},
{ 1, 6, 2, 4, 5, 1, 2, 3, 4, 0},
{ 2, 3, 3, 4, 5, 1, 2, 3, 4, 0},
{ 2, 4, 3, 4, 5, 1, 2, 3, 4, 4},
{ 0, 0, 3, 4, 5, 1, 2, 3, 4, 0},
{ 0, 0, 3, 4, 5, 1, 2, 3, 4, 0},
{ 5, 2, 3, 4, 5, 1, 2, 3, 4, 0},
{ 3, 5, 3, 4, 5, 1, 2, 3, 4, 0},
{ 0, 3, 3, 4, 5, 1, 2, 3, 4, 0} }
for i=1, 10 do
for j=1, 10 do
if matrix[i][j] == 1 then
bubble = display.newImage(bb, ( i*62 ) - 62 , ( j*62 ) - 62)
physics.addBody( bubble, “static”,{ density=5, friction=0.1, bounce=0.1, radius = d/2 } )
elseif matrix[i][j] == 2 then
bubble = display.newImage(rb, ( i*62 ) - 62 , ( j*62 ) - 62)
physics.addBody( bubble, “static”,{ density=5, friction=0.1, bounce=0.1, radius = d/2 } )
elseif matrix[i][j] == 3 then
bubble = display.newImage(gb, ( i*62 ) - 62 , ( j*62 ) - 62)
physics.addBody( bubble, “static”,{ density=5, friction=0.1, bounce=0.1, radius = d/2 } )
elseif matrix[i][j] == 4 then
bubble = display.newImage(yb, ( i*62 ) - 62 , ( j*62 ) - 62)
physics.addBody( bubble, “static”,{ density=5, friction=0.1, bounce=0.1, radius = d/2 } )
elseif matrix[i][j] == 5 then
bubble = display.newImage(pb, ( i*62 ) - 62 , ( j*62 ) - 62)
physics.addBody( bubble, “static”,{ density=5, friction=0.1, bounce=0.1, radius = d/2 } )
elseif matrix[i][j] == 6 then
bubble = display.newImage(ob, ( i*62 ) - 62 , ( j*62 ) - 62)
physics.addBody( bubble, “static”,{ density=5, friction=0.1, bounce=0.1, radius = d/2 } )
end
end
end
shoot(event)
background:addEventListener(“touch”, shoot) ;[/lua] [import]uid: 163580 topic_id: 30332 reply_id: 330332[/import]