Don’t have Skype sorry.
No problem! Well i guess ill see you around the forums!
See ya around !!!
I didn’t notice but there was a ‘add.physics can not be called in the middle of collision’ error in the console. To fix this I have added
a timer.
Here’s the new code
box=display.newRect(x\_location -28, y\_location, 2,320) physics.addBody(box,"static", {isSensor= true}) local y= math.random(80,180) bad\_fish = display.newImage('srd2.png',x\_location+ 100, y); physics.addBody(bad\_fish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); bad\_fish.gravityScale = 0 bad\_fish:setLinearVelocity(-45,0) local function clearFish(event) if (event.phase == "ended" and event.other==bad\_fish) then bad\_fish:removeSelf(); bad\_fish=nil local z= math.random(80,180) bad\_fish = display.newImage('srd2.png',x\_location + 100, z); local table = { density=1, bounce=0, friction=0, radius=17 } local physics = require("physics") timer.performWithDelay(100, function () physics.addBody(bad\_fish, "dynamic", table) end,1) bad\_fish.gravityScale = 0 bad\_fish:setLinearVelocity(-45,0) end end box:addEventListener( "collision", clearFish );
the add.physics error is gone however, ‘attempt to call method ‘setLinearVelocity’ (a nil value)’ still persists.
Any suggestion/help is appreciated.
Thanks.
Hello, Ive also had this problem… To fix it i simply called the object that got removed “nill”… So
display.remove(object) \<-- use display.remove if possible object = nill
Hope this helps! Good Luck!
Oh and also another suggestion is not to use UNDERSCORE… its really messes with your code and sometimes gives you random errors that aren’t supposed to be there…
@SonicX278
Thanks for the input Sonic. This is the modified code according to your suggestion
local function clearFish(event) if (event.phase == "ended" and event.other==badfish) then display.remove(badfish) badfish=nil if badfish== nil then local z= math.random(80,180) badfish = display.newImage('srd2.png',x\_location + 100, z); local table = { density=1, bounce=0, friction=0, radius=17 } timer.performWithDelay(100, function () physics.addBody(badfish, "dynamic", table) end,1) badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end end end
However, the issue still persists. One note, If I comment out the setLinearVelocity line, the code runs without error.
However, in that case gravityScale = 0 line does not do anything. The second spawned object reacts to normal gravity as if that line badfish.gravityScale = 0 was not there.
So I am not sure what exactly is going on here.
Can you make a sample project and post the code? So I can just post it in a main.lua and see and try to fix the error? Thanks!
Here you go. tested on a single main.lua file same error.
local y\_location = display.contentCenterY local physics = require ( "physics" ) physics.start() local bg = display.newImage('background.png', x\_location, y\_location) box=display.newRect(x\_location , y\_location, 2,320) physics.addBody(box,"static", {isSensor= true}) badfish = display.newImage('srd2.png',x\_location+ 100, y\_location); physics.addBody(badfish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) local function clearFish(event) if (event.phase == "ended" and event.other==badfish) then display.remove(badfish) badfish=nil if badfish== nil then local z= math.random(80,180) badfish = display.newImage('srd2.png',x\_location + 100, z); local table = { density=1, bounce=0, friction=0, radius=17 } timer.performWithDelay(100, function () physics.addBody(badfish, "dynamic", table) end,1) function setter() badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end timer.performWithDelay(50,setter,1) end end end box:addEventListener( "collision", clearFish )
Same error and situation.
Thanks.
Please add these two lines to TOP of the previous code. Forgot to add these two lines.
display.setStatusBar( display.HiddenStatusBar ) local x\_location = display.contentCenterX
I will be able to test and fix in an hour or so.
Im working on it right now…
Can you please post the images??
Well one is just a fish. and one is a blue backgorund
https://openclipart.org/detail/120667/fish-silhouette-by-rones That’s ‘srd2.png’
That’s the fish
Can’t seem to be able to attach images so can’t attach background. But it’s pretty much a solid blue background covering the whole screen.
Sorry about that.
Thanks.
Sorry but i cant seem to get your code to work… can you possibly just make the images
display.newRect()
?
Because i cant seem to get the images and stuff to work properly…
Thanks!
https://docs.coronalabs.com/api/library/display/newRect.html
display.setStatusBar( display.HiddenStatusBar ) local x\_location = display.contentCenterX local y\_location = display.contentCenterY local physics = require ( "physics" ) physics.start() local bg = display.newRect( x\_location, y\_location, 320, 480) bg:setFillColor(0,0,1) box=display.newRect(x\_location , y\_location, 10,480) box:setFillColor(1,0,0) physics.addBody(box,"static", {isSensor= true}) local y= math.random(80,180) badfish = display.newCircle( x\_location+ 100, y, 17); physics.addBody(badfish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) local function clearFish(event) if (event.phase == "ended" and event.other==badfish) then display.remove(badfish) badfish=nil if badfish== nil then local z= math.random(80,180) badfish = display.newCircle( x\_location + 100, z, 17); local table = { density=1, bounce=0, friction=0, radius=17 } timer.performWithDelay(100, function () physics.addBody(badfish, "dynamic", table) end,1) function setter() badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end timer.performWithDelay(50,setter,1) end end end box:addEventListener( "collision", clearFish )
Here you go. blue background, red thin box the sensor (isSensor = true) and the white ball would be the physics object.
Thanks.
Can you explain what is supposed to happen? Like so the white ball goes through the line and then what happens?
So what I would like is when the white ball passes through the wall and the event.phase==“ended” that ball that just passed should get removed/de-spanwed and a new ball would spawn. The new spawned ball should have same properties as the previous one. Only difference being since the y location is randomly generated it would be in a different height. By same property I mean, gravityScale=0 and setLinearVelocity(-45,0). However when the second ball spawns, it does not take on the properties I assigned. As in
gravityScale =0 does not work ( new spawned ball still reacts to gravity) and setLinearVelocity gives an error.
So my code spawns an object but not the way I want. The process is supposed to continue on, therefore I added a collision event listener to the function, until of course I remove that listener.
Well i got the error to stop…
display.setStatusBar( display.HiddenStatusBar ) local x\_location = display.contentCenterX local y\_location = display.contentCenterY local physics = require ( "physics" ) physics.start() local bg = display.newRect( x\_location, y\_location, 320, 480) bg:setFillColor(0,0,1) local box=display.newRect(x\_location , y\_location, 10,480) box:setFillColor(1,0,0) physics.addBody(box,"static", {isSensor= true}) local y= math.random(80,180) local badfish = display.newCircle( x\_location+ 100, y, 17); physics.addBody(badfish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) local function clearFish(event) if (event.phase == "ended" and event.other==badfish) then display.remove(badfish) badfish=nil if badfish ~= nil then local z= math.random(80,180) badfish = display.newCircle( x\_location + 100, z, 17); local table = { density=1, bounce=0, friction=0, radius=17 } timer.performWithDelay(100, function () physics.addBody(badfish, "dynamic", table) end,1) function setter() badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end timer.performWithDelay(50,setter,1) end end end box:addEventListener( "collision", clearFish )
The error happened because this line of code
if badfish == nil then
was supposed to be this
if badfish ~= nil then
BUTTTTTTTTTTTTT
When i was typing my answer your replied with the explanation and i instantly knew your problem…
So heres the new code that does what you wanted it to do… ( From what i understood… )
display.setStatusBar( display.HiddenStatusBar ) local x\_location = display.contentCenterX local y\_location = display.contentCenterY local timer1 local timer2 local physics = require ( "physics" ) physics.start() local bg = display.newRect( x\_location, y\_location, 320, 480) bg:setFillColor(0,0,1) local box=display.newRect(x\_location , y\_location, 10,480) box:setFillColor(1,0,0) physics.addBody(box,"static", {isSensor= true}) local y= math.random(80,180) local function spawnFishOnce() local y= math.random(80,180) badfish = display.newCircle( x\_location+ 100, y, 17 ); physics.addBody(badfish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end timer1 = timer.performWithDelay( 1, spawnFishOnce, 1 ) local function clearFish(event) if ( event.phase == "ended" and event.other == badfish ) then display.remove(badfish) --badfish=nil local function spawnNew() print("hi") local y= math.random(80,180) badfish = display.newCircle( x\_location+ 100, y, 17 ); physics.addBody(badfish, "dynamic", { density=1, bounce=0, friction=0, radius=17 } ); badfish.gravityScale = 0 badfish:setLinearVelocity(-45,0) end timer2 = timer.performWithDelay( 50, spawnNew, 1 ) end end box:addEventListener( "collision", clearFish )
So to explain what i did…
So when the game starts then i spawned a fish once and then that function stops…(spawnFishOnce)
Now when the fish goes passed the line and disappears the timer2 starts and waits 50 milliseconds and fires the spawnNew function and places the fish on a new y axis every time…
Good Luck!!
WOW.
That is exactly what I was hoping to accomplish. I thought of implementing that function within a function thing like you did. But it’s my second week coding in Lua /Corona haha and I kinda lost track.
That was great. Thank you VERY MUCH. I was stuck in this for hours.
Cheers.