简体   繁体   中英

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. 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.

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.

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 :

-- 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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