I’m currently working on a pet-project which for various reasons requires me to build a component in pure Actionscript, but have it be versatile enough to be loaded into either a Flash application or a Flex application. One would think that, being as Flex is basically just a framework for Actionscript components, this would not be to tall of an order, however every time I try to do some interactive combination of both platforms, I wind up with some seriously crazy behavior of some sort.
The biggest issue is, there is no real way to predict what will go wrong, or how these issue will manifest, OR how to fix them…it’s basically a hack job every time. My issues with this particular project stemmed from my Flash movie (loaded into a SWFLoader object in Flex) trying to be smart enough to scale itself. Also, any time I would adjust the width and height of elements within the Flash component (done by code executed from WITHIN the Swf, not outside of it) the elements I was trying to affect, would scale all their children according to their new size, causing quite a mess.
The way I dealt with the scaling issue, is that I stopped trying to control the size of elements that were parents of other elements, so that fixed the wonky scaling, but it really isn’t a true fix for the underlying issue: what if I NEEDED to set an explicit size on the parent without scaling the children?
The other issue I ran into was quite a weird one. Basically in one of my classes, I have a method that updates the graphics and size of the class based on arguments passed in…the code looked something like this:
// set x
x = ( ( pixelEndX * multiplier ) - ( pixelStartX * multiplier ) ) + pixelStartX;
// set y
y = ( ( pixelEndY * multiplier ) - ( pixelStartY * multiplier ) ) + pixelStartY;
// set width
width = ( ( pixelEndWidth * multiplier ) - ( pixelStartWidth * multiplier ) ) + pixelStartWidth;
// set height
height = ( ( pixelEndHeight * multiplier ) - ( pixelStartHeight * multiplier ) ) + pixelStartHeight;
What was happening, is after updating the size and position twice, the shape would then disappear. After about an hour of banging my head against a wall, I finally figured out that somehow, even though the calculations for width and height were generating the correct values, the width and height themselves were being set to 0, therefore making the object disappear. I fixed this by storing the calculated values in local variables and then applying them to the height and width properties as in below:
// set x
x = ( ( pixelEndX * multiplier ) - ( pixelStartX * multiplier ) ) + pixelStartX;
// set y
y = ( ( pixelEndY * multiplier ) - ( pixelStartY * multiplier ) ) + pixelStartY;
// set width
var newWidth : Number = ( ( pixelEndWidth * multiplier ) - ( pixelStartWidth * multiplier ) ) + pixelStartWidth;
// set height
var newHeight : Number = ( ( pixelEndHeight * multiplier ) - ( pixelStartHeight * multiplier ) ) + pixelStartHeight;
width = newWidth;
height = newHeight;
The odd thing is that I built this same functionality in Flex and it worked flawlessly with the code in the first example. Also, there is really no reason why a class should have to be tricked like that, the calculated value SHOULD be able to apply to the width or height properties without first being stored locally and, unfortunately at this point I have no answers. If anyone does know the cause and/or solution to this odd problem, please feel free to post a response!