Hi,
I have an “enterframe” listener to constantly set the speed of a falling display object. On collision this display object is destroyed. When the object is destroyed my call to “setLinearVelocity” returns nil and I get an error dispite having checks to see if the object exists before I call “setLinearVelocity”. Below is my code:
function setspeed(e)
if (bangflag == false) then
if (b ~= nil) then
vx, vy = b:getLinearVelocity()
b:setLinearVelocity( vx, 110 + (level \* 10) )
end
end
end
Runtime:addEventListener("enterFrame", setspeed);
“bangflag” is a flag I set whenever a collision is detected so this should not fire during a collision. “b” is a var used to hold display objects and I check to make sure it is not nil before calling “setLinearVelocity”. Please note removing the event listener is not an option as I need it for other display objects. [import]uid: 31694 topic_id: 24155 reply_id: 324155[/import]
What is the error?
You say it occurs on the setLinearVelocity line.
Now, that is AFTER the getLinearVelocity line, so that suggests that B is OK
In the line
b:setLinearVelocity( vx, 110 + (level * 10) )
you also use variables vx , and level
Assuming vx worked because no error on the line above, what is the value of level at that point?
ps, you can also test for b like this
if b then
…
[import]uid: 108660 topic_id: 24155 reply_id: 97471[/import]
Sorry “getLinearVelocity” doesn’t work either. I had it commented out.
if b then doesn’t seem to make any difference. [import]uid: 31694 topic_id: 24155 reply_id: 97483[/import]
OK.
b is not nil.
But maybe it isn’t what you expect it to be.
if it was an Apple, you would not expect it to be able to Eat()
>> “b” is a var used to hold display objects <<
Objects plural?
Are they all physics bodies?
Is b an array or table?
try this:
local bb = event.target
if (bangflag == false) then
vx, vy = bb:getLinearVelocity()
bb:setLinearVelocity( vx, 110 + (level \* 10) )
end
[import]uid: 108660 topic_id: 24155 reply_id: 97485[/import]
Here is how my spawn function works:
function spawn()
b = bean.newBean(oldColour);
function b:collision(e)
--Code here to remove display objects that collide
end
b:addEventListener("collision", b);
beans[#beans + 1] = b;
end
I would like to set the speed of only the last bean created. I tried “beans[#beans]:setLinearVelocity( vx, 110 + (level * 10) )” but then when a the last spawned bean is removed I had it altering the speed of beans I wasn’t intending to alter. [import]uid: 31694 topic_id: 24155 reply_id: 97491[/import]
This seems to have worked:
if (b and b == beans[#beans]) then
[import]uid: 31694 topic_id: 24155 reply_id: 97510[/import]
I’d like to see the remove code.
If you do something like:
bean:removeSelf()
and do not do:
bean = nil
Then bean still exists as a table just with the display object removed which removes all of the :functions with it. You have to set it to nil to make sure the table reference is gone too.
[import]uid: 19626 topic_id: 24155 reply_id: 97524[/import]