1
votes

I want to check whether the color = a certain color then appropriately react. This is done in flash AS3.

the code I've got is if(cal_mc.color == 0x0000FF) { p1score = p1score + 25;

(cal_mc being the object)

It executes without any errors but doesn't work. Can anyone tell me what I'm doing wrong with the if statement? Preferably keeping the code as simple as possible.

1

1 Answers

4
votes

As far as I understand, you are using a simple MovieClip object in your code (cal_mc is of type MovieClip).

Short answer: There is no property "color" on MovieClip, so your first condition fails.

Long answer:

MovieClips by definition are dynamic objects, so that means they can have properties defined while your code is running:

var custom:MovieClip = new MovieClip();
custom.potato = "I am a potato!"; // creates a new property "potato" and assigns it the value of "I am a potato".
trace(custom.potato); // outputs "I am a potato";

There is no property "color" on MovieClip, so your condition statement is false in the theoretical state, but practically flash doesn't throw any errors, since MovieClips can have properties assigned at runtime, so flash cannot know, If you haven't defined "color" somewhere.

The are few solutions:

  • Either to create a custom class extending the MovieClip which would contain color property and some methods to draw the graphics you want and logic to assign the correct value to the color property when the drawing is complete.
  • If your MovieClip is a simple shape, which I assume it is — render the MovieClip to Bitmap object, and then sample the first pixel (since all of the pixels of simple shapes are of the same color) from the Bitmaps bitmapData.

Here's the code for the latter: (this code may require tweaking, it's an outline)

function sampleColorFromDisplayObject(display:DisplayObject):uint
{
    var bd:BitmapData = new BitmapData(display.width, display.height, false, 0xFF0000);
    bd.draw(display, new Matrix());
    var color:uint = bd.getPixel(0, 0);
    bd.dispose();
    return color;
}

Keep in mind, that due to various possible shapes and sizes, this code may require tweaking and may not work as intended with some shapes. I've made the code working with as big amount of shapes as possible by making the bitmapData big in dimensions and sampling the first pixel to account for certain types of circles and shapes defined in the flash IDE.