| | | trafalmadorian |  |
| Posted: Mon Jun 09, 2008 3:40 pm Post subject: Creating a custom component is causing a strange scoping iss |  |
| |  | |
I am a fairly new user to Flash and Actionscript, but I have a fair amount of experience working with C and Java. I'm currently working with Actionscript 2 in Macromedia Studio 8. I was trying to create a new component for a project I am working on, and I followed the tutorial in the help and all testing works while I am in context of the flash document where I export the component from. However, I try testing my component in a new blank document and I get some strange behavior.
I am able to create a component on the stage, and adjust the properties perfectly fine. However, when I test the file, the variables for colors go out of scope. That is, the adjustments I made to the component through the parameters panel, and saw updated on the stage aren't demonstrated at runtime. The Number variable I used however is preserved. I did some traces, and it appears that this is happening because at runtime in the new blank document, the variables in the onEnterFrame method go out of scope, where they weren't before.
It seems really strange to me that I should be able to edit the color on stage but have it revert while it is running. My intuition says that if it wasn't going to work in the new document, that it shouldn't work in either case.
Anyway, here is the code for the .as file, I hope I'm just doing something stupid.
import mx.core.UIObject; import flash.geom.ColorTransform; import flash.geom.Transform;
class Skillbar extends UIObject { static var symbolName:String = "skillbar"; static var symbolOwner:Object = Object(Skillbar); var className:String = "skillbar"; private var boundingBox_mc:MovieClip; private var skillbar:MovieClip; private var barColorTransform1:ColorTransform; private var barColorTransform2:ColorTransform; /*Percent filled getter an setter*/ private var __percentFilled:Number; [Inspectable(defaultValue=100)] public function get percentFilled():Number { return __percentFilled; } public function set percentFilled(newPercent:Number) { if(newPercent > 255) newPercent = 255; if(newPercent < 0) newPercent = 0; __percentFilled = newPercent; invalidate(); } /*first bar color getter and setter*/ private var __barColor1:Color; [Inspectable(defaultValue = (Color(skillbar.bar_anim.bar_col1)))] public function get barColor1():Color { return __barColor1; } public function set barColor1(newColor:Color) { __barColor1 = newColor; var newRed1 = (__barColor1&0x00FF0000)>>>16; var newGreen1 = (__barColor1&0x0000FF00)>>>8; var newBlue1 = (__barColor1&0x000000FF); barColorTransform1 = new ColorTransform(0,0,0,0,newRed1,newGreen1,newBlue1,255); invalidate(); } /*second bar color getter and setter*/ private var __barColor2:Color; [Inspectable(defaultValue = (Color(skillbar.bar_anim.bar_col2)))] public function get barColor2():Color { return __barColor2 } public function set barColor2(newColor:Color) { __barColor2 = newColor; var newRed2 = (__barColor2&0x00FF0000)>>>16; var newGreen2 = (__barColor2&0x0000FF00)>>>8; var newBlue2 = (__barColor2&0x000000FF); barColorTransform2 = new ColorTransform(0,0,0,0,newRed2,newGreen2,newBlue2,255); invalidate(); } function Skillbar() { } function init():Void { super.init(); useHandCursor = false; boundingBox_mc._visible = false; boundingBox_mc._width = 0; boundingBox_mc._height = 0; } function createChildren():Void { skillbar = createObject("skillbar_mc", "skillbar", 0); size(); } function size() { invalidate(); } function draw() { super.draw(); skillbar.maskbar_mc._xscale = __percentFilled; skillbar.bar_anim.bar_col1.transform.colorTransform = barColorTransform1; skillbar.bar_anim.bar_col2.transform.colorTransform = barColorTransform2; skillbar.bar_anim.onEnterFrame = function () { var currentColorTransform1 = this._parent._parent.barColorTransform1; var currentColorTransform2 = this._parent._parent.barColorTransform2; this.bar_col1.transform.colorTransform = currentColorTransform1; this.bar_col2.transform.colorTransform = currentColorTransform2; } } } |
|