[Resolved] I'm trying to change the color of balls from green to red when they pass a line

I have 1000 little balls falling from the sky at a rate of 5/second that I want to change color when they pass a certain point. I can only get it to work if I launch the balls 1 every 1.2 seconds.

I think it has to do with the fact that I have more than one ball on the screen when I try to turn them red
[lua] function ballDrop()
ball = display.newCircle( 0, 0, 2)
ball:setFillColor(0, 255, 0)
ball.x = 50
ball.y = 50
ball.name = ‘ball’
physics.addBody(ball, {bounce=1.0, radius = 2,density = .1, friction=0.7})
ball:addEventListener(“collision”, ball)
function turnRed(event)

if ball.x > 0 and ball.x < 600 and ball.y > 200 and ball.y < 300 then
ball:setFillColor(200, 0, 0)
end
end
Runtime:addEventListener(“enterFrame”, turnRed)

end

tmr1 = timer.performWithDelay(200, ballDrop,1000) [/lua]
So what would be the best way to have multiple balls on the screen be able to turn red in the turnRed function? Thanks [import]uid: 75779 topic_id: 30233 reply_id: 330233[/import]

Your function would probably work if you gave every single ball an individual number, as in turn it into a table. Here’s some sample code for a different way to do what you are doing:

[code]
display.setStatusBar( display.HiddenStatusBar )

physics = require(“physics”)
physics.start()
physics.setGravity(0,5)

ball = {}
i = 1

function turnRed( event )
if event.phase == “began” then
if event.object1.name == “line” and event.object2.name == “ball” then
event.object2:setFillColor(255,0,0)
end
end
end
Runtime:addEventListener( “collision”, turnRed )

function createBall()
ranX = math.random( 80, 240 )
ball[i] = display.newCircle( ranX, 40, 5 )
ball[i]:setFillColor(0, 255, 0)
ball[i].name = “ball”
physics.addBody(ball[i], { isSensor = true } )
i = i + 1
end

timer.performWithDelay( 200, createBall, 0 )

line = display.newRect( 0, 240, 320, 2 )
line.name = “line”
physics.addBody( line, “static”, { isSensor = true } )
– line.alpha = 0 this is if you want the line to be invisble
[/code] [import]uid: 77199 topic_id: 30233 reply_id: 121092[/import]

Thank you for the help! It works perfect! [import]uid: 75779 topic_id: 30233 reply_id: 121096[/import]

@ rmckee282002

It’s my impression that you create several displayobjects with no access via a variable anymore.
Despite the color change problem, that hatethinkingofnames solved elegantly, you went into more problems with hundreds of non accessible display objects, so can’t destroy them anymore, because the variable ball is only a reference to the latest displayobject.
[import]uid: 98393 topic_id: 30233 reply_id: 121142[/import]

Excellent point, because now I want to put a ‘collision’ event Listener on the balls so when they hit the floor something will happen
[lua]floor1 = display.newRect(-200, h, w+400, 10)
floor1.name = ‘floor1’
floor1:setFillColor(255,0,0)
physics.addBody(floor1, “static”,{bounce = 0, friction = 2} )

function createBall()
ranX = math.random( 80, 240 )
ball[i] = display.newCircle( 40, 40, 2 )
ball[i]:setFillColor(0, 255, 0)
ball[i].name = “ball”
ball[i]:addEventListener(“collision”, ball[i])
physics.addBody(ball[i],“dynamic”, {bounce=.001, radius = 2,density = .1, friction=0.7, })
i = i + 1
end
function ball[i]:collision (e)
if e.other.name == ‘floor1’ then

print(“collision”)
ball[i]:removeEventListener(“collision”, ball[i])
end
end

timer.performWithDelay( 200, createBall, -1 )[/lua]
It says I’m missing a ‘(’ expected near ‘[’ somewhere in the collision function I can’t see what the problem is. [import]uid: 75779 topic_id: 30233 reply_id: 121153[/import]

several mistakes, have a look at my code, I’ve tested it

-- constants  
\_H = display.contentHeight  
\_W = display.contentWidth  
  
local physics = require "physics"  
  
physics.start()  
  
local floor1 = display.newRect(-200, \_H, \_W+400, 10)  
floor1.name = "floor1"   
floor1:setFillColor(255,0,0)  
physics.addBody(floor1, "static",{bounce = 0, friction = 2} )  
  
local ball = {} -- to create table  
   
  
local function onCollision (self, event)  
  
 print(event.other.name)  
 print(self.name)  
  
 if event.other.name == "floor1" then   
 print("collision")  
 self:removeEventListener("collision",self)  
 -- sth will happen  
 -- remove ball  
 display.remove(self)  
  
 end  
  
end  
  
local i = 1 -- you need this, otherwise you'll alway have i = 1 in your loop  
  
function createBall()  
 ranX = math.random( 80, 240 )  
 ball[i] = display.newCircle( 40, 40, 2 )  
 ball[i]:setFillColor(0, 255, 0)  
 ball[i].x = ranX  
 ball[i].name = "ball"  
 ball[i].collision = onCollision  
 ball[i]:addEventListener("collision", ball[i])  
 physics.addBody(ball[i],"dynamic", {bounce=.001, radius = 2,density = .1, friction=0.7, })  
 i = i + 1  
end  
  
   
timer.performWithDelay( 200, createBall, -1 )  
  

[import]uid: 98393 topic_id: 30233 reply_id: 121161[/import]

Thank you one more thing I’m trying to change the ball color to blue when it collides with the ground.
I removed display.remove(self) and self:removeEventListener(“collision”,self) added:
ball[i]:setFillColor(0,0,250) or self:setFillColor(0,0,250). Neither works
[lua]local function onCollision (self, event)

print(event.other.name)
print(self.name)

if event.other.name == “floor1” then
print(“collision”)
– ball[i]:setFillColor(0, 0,255)
– self:setFillColor(0, 0, 255)

end[/lua]

end [import]uid: 75779 topic_id: 30233 reply_id: 121165[/import]

 if event.other.name == "floor1" then   
 print("collision")  
 self:setFillColor(0,0,250)   
 end  

Strange, it works for me!

But funny thing: not every Ball will change color to “Blue” as some balls only collide with other balls, who’ve already been on the ground. They don’t collide with the floor anymore.

pic:
http://dl.getdropbox.com/u/21552766/Bildschirmfoto%202012-08-25%20um%2020.22.40.png

maybe your floor is outside the screen? activate hybrid mode to check
[import]uid: 98393 topic_id: 30233 reply_id: 121168[/import]

Your function would probably work if you gave every single ball an individual number, as in turn it into a table. Here’s some sample code for a different way to do what you are doing:

[code]
display.setStatusBar( display.HiddenStatusBar )

physics = require(“physics”)
physics.start()
physics.setGravity(0,5)

ball = {}
i = 1

function turnRed( event )
if event.phase == “began” then
if event.object1.name == “line” and event.object2.name == “ball” then
event.object2:setFillColor(255,0,0)
end
end
end
Runtime:addEventListener( “collision”, turnRed )

function createBall()
ranX = math.random( 80, 240 )
ball[i] = display.newCircle( ranX, 40, 5 )
ball[i]:setFillColor(0, 255, 0)
ball[i].name = “ball”
physics.addBody(ball[i], { isSensor = true } )
i = i + 1
end

timer.performWithDelay( 200, createBall, 0 )

line = display.newRect( 0, 240, 320, 2 )
line.name = “line”
physics.addBody( line, “static”, { isSensor = true } )
– line.alpha = 0 this is if you want the line to be invisble
[/code] [import]uid: 77199 topic_id: 30233 reply_id: 121092[/import]

Thank you for the help! It works perfect! [import]uid: 75779 topic_id: 30233 reply_id: 121096[/import]

@ rmckee282002

It’s my impression that you create several displayobjects with no access via a variable anymore.
Despite the color change problem, that hatethinkingofnames solved elegantly, you went into more problems with hundreds of non accessible display objects, so can’t destroy them anymore, because the variable ball is only a reference to the latest displayobject.
[import]uid: 98393 topic_id: 30233 reply_id: 121142[/import]

Excellent point, because now I want to put a ‘collision’ event Listener on the balls so when they hit the floor something will happen
[lua]floor1 = display.newRect(-200, h, w+400, 10)
floor1.name = ‘floor1’
floor1:setFillColor(255,0,0)
physics.addBody(floor1, “static”,{bounce = 0, friction = 2} )

function createBall()
ranX = math.random( 80, 240 )
ball[i] = display.newCircle( 40, 40, 2 )
ball[i]:setFillColor(0, 255, 0)
ball[i].name = “ball”
ball[i]:addEventListener(“collision”, ball[i])
physics.addBody(ball[i],“dynamic”, {bounce=.001, radius = 2,density = .1, friction=0.7, })
i = i + 1
end
function ball[i]:collision (e)
if e.other.name == ‘floor1’ then

print(“collision”)
ball[i]:removeEventListener(“collision”, ball[i])
end
end

timer.performWithDelay( 200, createBall, -1 )[/lua]
It says I’m missing a ‘(’ expected near ‘[’ somewhere in the collision function I can’t see what the problem is. [import]uid: 75779 topic_id: 30233 reply_id: 121153[/import]

several mistakes, have a look at my code, I’ve tested it

-- constants  
\_H = display.contentHeight  
\_W = display.contentWidth  
  
local physics = require "physics"  
  
physics.start()  
  
local floor1 = display.newRect(-200, \_H, \_W+400, 10)  
floor1.name = "floor1"   
floor1:setFillColor(255,0,0)  
physics.addBody(floor1, "static",{bounce = 0, friction = 2} )  
  
local ball = {} -- to create table  
   
  
local function onCollision (self, event)  
  
 print(event.other.name)  
 print(self.name)  
  
 if event.other.name == "floor1" then   
 print("collision")  
 self:removeEventListener("collision",self)  
 -- sth will happen  
 -- remove ball  
 display.remove(self)  
  
 end  
  
end  
  
local i = 1 -- you need this, otherwise you'll alway have i = 1 in your loop  
  
function createBall()  
 ranX = math.random( 80, 240 )  
 ball[i] = display.newCircle( 40, 40, 2 )  
 ball[i]:setFillColor(0, 255, 0)  
 ball[i].x = ranX  
 ball[i].name = "ball"  
 ball[i].collision = onCollision  
 ball[i]:addEventListener("collision", ball[i])  
 physics.addBody(ball[i],"dynamic", {bounce=.001, radius = 2,density = .1, friction=0.7, })  
 i = i + 1  
end  
  
   
timer.performWithDelay( 200, createBall, -1 )  
  

[import]uid: 98393 topic_id: 30233 reply_id: 121161[/import]

Thank you one more thing I’m trying to change the ball color to blue when it collides with the ground.
I removed display.remove(self) and self:removeEventListener(“collision”,self) added:
ball[i]:setFillColor(0,0,250) or self:setFillColor(0,0,250). Neither works
[lua]local function onCollision (self, event)

print(event.other.name)
print(self.name)

if event.other.name == “floor1” then
print(“collision”)
– ball[i]:setFillColor(0, 0,255)
– self:setFillColor(0, 0, 255)

end[/lua]

end [import]uid: 75779 topic_id: 30233 reply_id: 121165[/import]

 if event.other.name == "floor1" then   
 print("collision")  
 self:setFillColor(0,0,250)   
 end  

Strange, it works for me!

But funny thing: not every Ball will change color to “Blue” as some balls only collide with other balls, who’ve already been on the ground. They don’t collide with the floor anymore.

pic:
http://dl.getdropbox.com/u/21552766/Bildschirmfoto%202012-08-25%20um%2020.22.40.png

maybe your floor is outside the screen? activate hybrid mode to check
[import]uid: 98393 topic_id: 30233 reply_id: 121168[/import]