another flashbuilder brainbreaker for me…
I have a class with a contstructor that should only change the source of an image.
[Bindable]
[Embed(source='../pictures/test.jpg')]
private var _picture:Class;
public function Test(newSource:*****)
{
_picture.source = newSource;
}
(the image is not an image, but a class, I am aware of this, it is meant to be so 🙂 )
The problem is that when I call the constructor, let’s say:
var test:Test = new Test(pictureAtStage.source);
Flashbuilder will give an error, becouse I can’t tell the compiler what data type “newSource” at the constructor will have…
*edit:
When i use _picture.source, the embedded source does not seem to be changed…?
Anyone knows an answer?
Are we talking about
mx.controls.Image? if so, then the source of an image can be: a Class, a Bitmap (not a BitmapData), a String (in which case it is assumed that you wanted to load it instead of using an embedded one). If you want to find a common denominator for all these, then Object is that class, however, I would rather limit it to something particular to your use case.However, if I may advise anything… don’t use
mx.controls.Image, it’s too bloated, even for Flex framework. If it must be a UIComponent – extend UIComponent and let the source be of type BitmapData – this way you will be able to manage resources better – you could reuse the same actual image for example. You could then usegraphicsproperty of the control to display the image.Another advise, if you are still here 🙂 Don’t use
[Bindable], especially the short version of it, especially not on a private variable – you will save yourself the frustration of countless hours of debugging it… Besides, in your case you aren’t going to change the value of that variable anyway…Are you still here? Well, don’t use
[Embed]on variables, use it on class definition – slightly more work for you, but this will, potentially, make your code more portable. If you embed on class the compiler will not generate a silly “something-something-Asset” class, it will use Bitmap or BitmapData – whichever your class extends. Thus, you will not introduce a dependency on Flex framework, and, in general, you will have more control over your code.EDIT: the above was written assuming that _picture (class) variable and _picture (some variable used in a function) are not the same thing. But if they are the same thing, then
Classclass is dynamic, which means that you can add to it properties at runtime (don’t know why, it’s a design decision by Adobe…), however, the compiler will act as if it’s not possible, so you would work around that by adding the property through reflection: