How to take the colour of the object to use in a condicion?

Hi there!
I am doing a little game that load and print a ball (is an image). This ball is changing his coluor, from red to blue, and from blue to red. I would like to do this:
if the colour of the ball us red then
–code–
end

How can I do this in Corona SDK? I dont see any function that do this.
Goodbye :wink:

You can save the current color as a string to your object:

ball = display.newImageRect(“ball.png”, width, height)

ball.currentColor = “blue”

later change it to red, however you’re changing it and make sure to do:

ball.currentColor = “red”

then you can just do:

if ball.currentColor == “red” then

    --whatever you want to do

else

    – whatever you want to do if it’s blue

end

Thank you very much, it works :wink:
Bye!!

If I’ve read the optimization thread correction, this sort of string storage/comparison should happen as an enum, correct?

Perhaps in a tight loop, it might matter.  If you’re frame rate isn’t good enough, then you could look into those optimizations.

Honestly, since the iPhone 4 I’ve rarely had to go back and optimize anything, the devices are just so damn fast.   But it gives me a nice feeling to do things the fastest way :slight_smile:

You can save the current color as a string to your object:

ball = display.newImageRect(“ball.png”, width, height)

ball.currentColor = “blue”

later change it to red, however you’re changing it and make sure to do:

ball.currentColor = “red”

then you can just do:

if ball.currentColor == “red” then

    --whatever you want to do

else

    – whatever you want to do if it’s blue

end

Thank you very much, it works :wink:
Bye!!

If I’ve read the optimization thread correction, this sort of string storage/comparison should happen as an enum, correct?

Perhaps in a tight loop, it might matter.  If you’re frame rate isn’t good enough, then you could look into those optimizations.

Honestly, since the iPhone 4 I’ve rarely had to go back and optimize anything, the devices are just so damn fast.   But it gives me a nice feeling to do things the fastest way :slight_smile: