简体   繁体   English

电晕/卢阿和碰撞:“试图连接字段'名称'(nil值)”

[英]Corona/Lua and collisions: “attempt to concatenate field 'name' (a nil value)”

I'm trying to get some collision detection working but I can't seem to figure out what the problem is. 我正在尝试进行一些碰撞检测,但似乎无法弄清楚问题出在哪里。

The error I'm getting is 我得到的错误是

...\\main.lua:178:attempt to concatenate field 'name' (a nil value)

What I have is this: I have a box (the "ship") that stays at a fixed X coordinate, but moves up and down. 我所拥有的是:我有一个盒子(“船”),它保持在固定的X坐标上,但可以上下移动。 It's meant to go through a "tunnel" made up of two rectangles with a gap between them, and I'm trying to detect a collision between the ship and the walls of the tunnel (ie the rectangles). 它的意思是经过由两个矩形组成的“隧道”,两个矩形之间有一个缝隙,我试图检测船与隧道壁(即矩形)之间的碰撞。

I get this error when the collision happens. 发生碰撞时出现此错误。 A lot of my code is just modified versions of the official Corona documentation and I'm just not able to figure out what the problem is. 我的很多代码只是Corona官方文档的修改版本,而我只是无法弄清楚问题出在哪里。

Here are the relevant pieces of code: 以下是相关的代码段:

function playGame()
    -- Display the ship
    ship = display.newRect(ship);
    shipLayer:insert(ship);
    -- Add physics to ship
    physics.addBody(ship, {density = 3, bounce = 0.3});

    ...

    beginRun();
end

function beginRun()
    ...
    spawnTunnel(1100); -- this just calls the createTunnel function at a specific location
    gameListeners("add");
    ...
end

function gameListeners(event)
    if event == "add" then
        ship.collision = onCollision;
        ship:addEventListener("collision", ship);
        -- repeat above two lines for top
        -- and again for bottom
    end
end

-- Collision handler
function onCollision(self,event)  
    if ( event.phase == "began" ) then
        -- line 178 is right below this line  ----------------------------------
        print( self.name .. ": collision began with " .. event.other.name )
end

-- Create a "tunnel" using 2 rectangles
function createTunnel(center, xLoc)
    -- Create top and bottom rectangles, both named "box"
    top = display.newRect(stuff);
    top.name = "wall";
    bottom = display.newRect(stuff);
    bottom.name = "wall";

    -- Add them to the middleLayer group
    middleLayer:insert(top);
    middleLayer:insert(bottom);

    -- Add physics to the rectangles
    physics.addBody(top, "static", {bounce = 0.3});
    physics.addBody(bottom, "static", {bounce = 0.3});
end  

I only get the error message once the two objects should collide, so it seems like the collision is happening, and it is being detected. 一旦两个对象发生碰撞,我只会收到错误消息,因此看起来好像正在发生碰撞,并且正在检测到它。 But for some reason self.name and event.other.name are nil. 但是由于某种原因,self.name和event.other.name为零。

Try to use : 尝试使用:

top.name = "wall";

and

bottom.name = "wall";

as

top.myName = "wall"

and

bottom.myName = "wall";

Use onCollision function after your "createTunnel: function : 在您的“ createTunnel:函数之后使用onCollision函数:

-- Collision handler
function onCollision( self, event)

    if ( event.phase == "began" ) then
        print( self.myName .. ": collision began with " .. event.other.myName )
    end
end

Oh wow. 哇哦 After many hours I finally figured out my stupid, simple mistake. 几个小时后,我终于弄清了自己的愚蠢,简单的错误。

The problem was that I forgot to give the ship its own name. 问题是我忘了给这艘船起自己的名字了。 The code now looks like this and works just fine: 代码现在看起来像这样,可以正常工作:

function playGame()
    -- Display the ship
    ship = display.newRect(ship);
    ship.name = "ship";  -- added this line to give the ship a name
    shipLayer:insert(ship);
    -- Add physics to ship
    physics.addBody(ship, {density = 3, bounce = 0.3});

    ...

    beginRun();
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM