3
\$\begingroup\$

This code colors the enemy red:

enemies.get(i).image.setColor(0,1,0,0,1);
enemies.get(i).image.setColor(1,1,0,0,1);
enemies.get(i).image.setColor(2,1,0,0,1);
enemies.get(i).image.setColor(3,1,0,0,1);

if I set it to:
enemies.get(i).image.setColor(0,0,0,0,1);
it turns it pure black, and if I set it to
enemies.get(i).image.setColor(0,0,0,0,0);
it turns the enemy invisiable

How do I un-color enemy red? as in return to state as it was before it was colored.

Documentation link.

\$\endgroup\$
0

3 Answers 3

3
\$\begingroup\$

Don't fiddle with the alpha at all if you're not wanting to change it. Use the non-alpha version.

enemies.get(i).setCurrentColor(1,0,0);

Then create this method:

setCurrentColor(int r, int g, int b) {
    image.setColor(0,r,g,b);
    image.setColor(1,r,g,b);
    image.setColor(2,r,g,b);
    image.setColor(3,r,g,b);
}

Then when you want to return to no color, just texture, set the enemy to white:

enemies.get(i).setCurrentColor(1,1,1);

Turns out there's a setImageColor function that Eric mentioned in his answer. I checked the source:

public void setImageColor(float r, float g, float b) {
    setColor(TOP_LEFT, r, g, b);
    setColor(TOP_RIGHT, r, g, b);
    setColor(BOTTOM_LEFT, r, g, b);
    setColor(BOTTOM_RIGHT, r, g, b);
}

So, setImageColor is not any different than calling setColor on all four corners. Just in case you were wondering.

\$\endgroup\$
1
\$\begingroup\$

You need to track the last color set before changing it red, and use that when you want to restore the color.

\$\endgroup\$
1
  • \$\begingroup\$ but there is no other colors, its just enemy.png image , when he gets hit, it changes red. \$\endgroup\$
    – Matthew
    Commented Apr 3, 2013 at 16:27
1
\$\begingroup\$

An Image's default color is 1,1,1,1 (rgba) a.k.a. opaque (or non-transparent) white. Use Image.setImageColor to reset it:

enemies.get(i).image.setImageColor(1,1,1,1);

Be sure to use this method instead of applying Image.setColor to each of the image's four corners.

\$\endgroup\$
2
  • \$\begingroup\$ it says setImageColor doesnt exist \$\endgroup\$
    – Matthew
    Commented Apr 5, 2013 at 17:57
  • \$\begingroup\$ @Matthew Right, I corrected the code for your copy-pasting convenience. \$\endgroup\$
    – Eric
    Commented Apr 5, 2013 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .