Gravity with the library "physics" crown SKD (ERROR)

2

I am solving a college exercise in corona SDK in making a small game, however I have a small problem at the time the ball will fall from "top" for the second time. The point is that I need to drop the polka dots from the top of the phone so that I can deviate with my "character" object, which is however a blue square. The first time when the program is executed, the ball can fall normally, and as soon as it touches the ground it shuts off and a new one is created at the top of the cell, but it does not apply gravity so that it can fall, the crown generates the following error:

Followthedrawingoftheballalongwiththeappliedgravity,andcalltheballdrawingmethodforittobecreatedagain.

--Personagemquad[1]=display.newRect(200,400,50,50)quad[1]:setFillColor(0,0,255)quad[1].myName="personagem"

-- bola
local function desenhaBolinha()
    local bola = {}
    local raio = math.random(20,40)
    local bolaX = math.random(0,320)
    bola[1] = display.newCircle(bolaX,raio,raio)
    bola[1]:setFillColor(255,0,0)
    bola[1].anchorX = 0
    bola[1].anchorY = 0
    bola[1].y = 10
    bola[1].myName = "bola"
    phy.addBody(bola[1], "dynamic")
    return (bola[1])
end
circ[1] = desenhaBolinha()
-- chão 
quad[7] = display.newRect(0,500,700,10)
quad[7]:setFillColor(255,255,255)
quad[7].myName = "chao"

-- FISICA
local function grav()
    phy.setGravity(0,20)
    phy.addBody(circ[1], "dynamic")
    phy.addBody(quad[7], "static")
    phy.addBody(quad[1], "dynamic")
end
grav()

Method created below the code posted above, so that the collision and recreation checks of the "ball" object are done:

local function colisao(e)
    --detecta colisao 
    if(e.phase == "began") then
        print("began: " .. e.object1.myName .. " com " .. e.object2.myName)
    elseif e.phase == "ended" then
        print("ended: " .. e.object1.myName .. " com " .. e.object2.myName)
    end

    if(e.phase == "began" and e.object1.myName == "bola" and e.object2.myName == "personagem") then
        text[2].text =  text[10] - 1
    end

    if(e.phase == "began" and e.object1.myName == "bola" and e.object2.myName == "chao") then
        circ[2] = e.object1
        circ[2]:removeSelf()
        circ[2] = desenhaBolinha()
    end

end

If someone can give a force! Thanks.

    
asked by anonymous 04.04.2018 / 04:28

1 answer

1

Several physics functions can not be called during a collision event, exactly as the warning message says. It is necessary to perform this type of operation after a very short timer (10 to 50 milliseconds) for the operation to occur in the next stage of the runtime frame, after the physics engine has done its calculations for the collision, and so on.

Another thing to do, is to put the physics code in the Show function, if you are using composer.

    
09.07.2018 / 13:44