[Resolved] [Loop Help] How to detect if a physics body is available?

Hello People,

I’ve been wondering how to do this for a while, is it possible to create variables with loops? like

[lua] for i = 1, 10 do
local ball…i…isAvail = false
end [/lua]

My problem is that I am re-applying linear impulses to balls, to make them move around, but if one gets removed, the impulses stop because it can’t detect the ball. My idea was to put a variable, and set it to true when the ball is spawned, and then only apply the impulse if the ball is available. Does anyone know if this is possible?

This is the code I’m using to re-apply the force.
[lua]
local function boost (e)
for i = 1, 10 do
ball[i]:setLinearVelocity(math.random(-100,100),math.random(-100,100))
end
end
timer.performWithDelay(1000, boost, 0)[/lua]

This is what I want to use:
[lua]local function boost (e)
for i = 1, 10 do
if ball…i…isAvailable = true then

ball[i]:setLinearVelocity(math.random(-100,100),math.random(-100,100))
end
end
end
timer.performWithDelay(1000, boost, 0) [/lua]

Any help would be much appreciated.
Ollie [import]uid: 67534 topic_id: 25358 reply_id: 325358[/import]

Another thing, do I need to use the tostring() command?

Thank you very much.
Ollie [import]uid: 67534 topic_id: 25358 reply_id: 102526[/import]

Try this instead;

[lua]for i = 1, #ball do
ball[i].isAvail = false
end[/lua]

That assumes the ball is in a table called ball, obviously, which I gather it is from your code.

RE tostring(), it is used to change a number to a string. For example if you want to manipulate it like a string.

There is also tonumber()

Eg;

[lua]local myVar = 1
print (myVar+myVar) – prints 2
myVar = tostring(myVar)
print (myVar+myVar) – get an error[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 25358 reply_id: 102667[/import]

Ah thank you very much Peach, one last thing. What do the two dots either side of the i do, when display a new image. for example:
[lua] display.newImage(“ball”…i…".png")[/lua]
? I have learned to do it but have never really understood why…

Thank you
Ollie [import]uid: 67534 topic_id: 25358 reply_id: 102712[/import]

@ollie: Those are string concentrators. Basically its saying:

“ball + i + .png” and making that a whole string.

[import]uid: 84637 topic_id: 25358 reply_id: 102733[/import]