1

我是 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
4

1 回答 1

2

你还没有启动physics engine. 在班级顶部写下以下几行:

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

继续编码...... :)

于 2013-08-07T19:37:59.727 回答