简体   繁体   English

重新启动游戏时尝试调用方法“插入”(nil值)

[英]attempt to call method 'insert' (a nil value) when i restart game

I've added objects to the group so they get deleted and respawn when the game restarts but when the game restarts it gives the following error "attempt to call method 'insert' (a nil value)" 我已将对象添加到组中,以便它们在游戏重新启动时被删除并重新生成,但是在游戏重新启动时,出现以下错误“试图调用方法'insert'(nil值)”

This is my object code: 这是我的目标代码:

local function spawnObject()
    local objIdx = mRandom(#objects)
    objName = objects[objIdx]

    object = display.newImage( "images/fruit_" .. objName .. "_100.png" )

    object.x = mRandom(screenLeft+30, screenRight-30)
    object.y = screenTop
    object.rotation = mRandom(-15, 15)
    object.id = mRandom(-15,15)

    group:insert( object )

    if objIdx < 4 then
        object.type = "food"
    else
        object.type = "other"
    end

    physics.addBody(object, "dynamic",{radius=45 , bounce = bt})
    grassfront320w:toFront()
    object.collision = objectCollision
    object:addEventListener( "collision", object )
end

And for restarting I go to restart scene which I've created and from there I go back to my play scene. 对于重新启动,我要重新启动我创建的场景,然后从那里回到我的游戏场景。

Please help me solve the problem. 请帮我解决问题。

Don't add directly to the group view. 不要直接添加到group视图。 Make a separate display group for the objects 为对象创建一个单独的显示组

local objectGroup = display.newGroup() group:insert(objectGroup)

by doing it this way, you are not obligated to kill out the group when you change scene (if you are using composer or storyboard). 通过这种方式,您无需在更改场景时消灭该组(如果使用的是作曲家或情节提要)。 It will clear it self in the memory when you change scene. 更改场景时,它会自动清除内存中的内容。

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

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