尝试索引全球“物理”(零值)

时间:2013-08-07 18:53:39

标签: global corona physics

我是corona / lua的新手,我无法找到解决这个问题的方法。我正在尝试生成一个从上到下落下的对象,应该停在屏幕的底部。然后我会创建触摸事件等等。

但是现在的问题是我收到了这个错误:

尝试索引全局'物理'(零值)

并且对象ofc不会掉下来。

这是我的代码:

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

local buttonY = display.contentWidth * 0.02
local buttonWidth = display.contentWidth * 0.1
local buttonHeight = display.contentWidth * 0.1

background = display.newImage("graphics/background.jpg")

local localGroup = display.newGroup()
local spawnTable = {}

function spawnLattina(params)
    local object = display.newImage(params.image, params.buttonX,50);
    object.objTable = params.objTable;
    object.index = #object.objTable+1;
    object.name = "object:".. object.index;


    --fisica
    if params.hasBody then

        object.density = params.density or 0;
        object.friction = params.friction or 0;
        object.bounce = params.bounce or 0;
        object.isSensor = params.isSensor or false;
        object.bodyType = params.bodyType or "dynamic";
        print(object.density .. " Friction: ".. object.friction .."bodyType: "..object.bodyType)

        physics.addBody(object, object.bodyType, 
                {density = object.density, 
                friction = object.friction, 
                bounce = object.bounce}
        ) 
    end

    object.group = params.group or nil
    object.group:insert(object)
    object.objTable[object.index] = object


    return object
end

for i = 1, 2 do
    local spawns = spawnLattina(
        {
            image = "graphics/lattina.png",
            objTable = spawnTable,
            buttonX = math.random(50,480),
            hasBody = true,
            density = 0,
            friction = 12,
            bodyType = "static",
            group = localGroup,
        }
    )
end

1 个答案:

答案 0 :(得分:2)

您尚未启动physics engine。在课程顶部写下以下几行:

local physics = require "physics"
physics.start()

保持编码..................:)