Using SetVariable with AS3 - Its ridiculous

I’m trying to make a flash 9 example of using AS3 and VB / VB.NET to set a variable inside the flash.

The flashplayer ocx has its method SetVariable("<variableName>", “<valueToGive>”) which sets a variable in the code to whatever you put as the second argument. This is callable with any language hosting the flashplayer.

The problem is that with AS3 this seems not to do anything… For the <variableName> I have tried:

_level0.myVar
level0.myVar
root.myVar
myVar

It works in AS2 with the textField var… But this is useless to me as I moved to AS3.

The code is in a class, and is the Document Class.

The code is below. A quick explanation follows that. What I really need to know, if anyone can help me, is:

How do I set a variable declared in the Document Class in AS3 from the flash container?


package {
 import flash.display.Sprite;
 import flash.display.Graphics;
 import flash.events.Event;
 import flash.text.TextField;
 import flash.system.fscommand;
 
 public class ColourChanger extends Sprite{
  
  public var BackColour:String = "0xFFFF00";
  public var TextColour:String = "0x000000";
  public var Text:String = "";
  
  private var oldBackColour:String = "0xFFFFFF";
  private var oldTextColour:String = "0x000000";
  private var oldText:String = "";
  
  private var bg:Sprite;
  private var sampleText:TextField;
  
  public function ColourChanger(){
   // add bg movieclip that can change colour
   bg = new Sprite();
   bg.graphics.beginFill(parseInt(BackColour),1);
   bg.graphics.drawRect(0,0,100,100);
   bg.graphics.endFill();
   addChild(bg);
   bg.width = bg.stage.stageWidth;
   bg.height = bg.stage.stageHeight;
   
   // add textfield
   sampleText = new TextField();
   sampleText.text = Text;
   sampleText.textColor = parseInt(TextColour);
   sampleText.wordWrap = true;
   addChild(sampleText);
   sampleText.x = 25;
   sampleText.y = 25;
   sampleText.width = sampleText.stage.stageWidth - 50;
   sampleText.height = sampleText.stage.stageHeight - 50;
   
   // set the text
   sampleText.text = "To change this text, set the variable Text (String)
";
   sampleText.appendText("To change the colour of the text, set variable TextColour (0x000000)
")
   sampleText.appendText("To change the colour fo the bg, set variable BackColour (0x000000)
")
   
   // set up function to monitor those vars for change;
   addEventListener(Event.ENTER_FRAME, checkForChanges);
   
  }
  private function checkForChanges(e:Event){
   if (BackColour != oldBackColour){
    changeBgColour();
   }
   if (TextColour != oldTextColour){
    changeTextColour();
   }
   if (Text != oldText){
    changeText();
   }
  }
  private function changeBgColour(){
   try{
   bg.graphics.clear();
   bg.graphics.beginFill(parseInt(BackColour),1);
   bg.graphics.drawRect(0,0,100,100);
   bg.graphics.endFill();
   oldBackColour = BackColour;
   }
   catch (e:Error){
    sampleText.text = e.toString();
   }
  }
  private function changeTextColour(){
   try{
   sampleText.textColor = parseInt(TextColour);
   oldTextColour = TextColour;
   }
   catch (e:Error){
    sampleText.text = e.toString();
   }
  }
  private function changeText(){
   try{
   sampleText.text = Text;
   oldText = Text;
   }
   catch (e:Error){
    sampleText.text = e.toString();
   }
  }
 }
}

Basically the file checks onEnterFrame for any changes in the vars at the top (BackColour,
TextColour, & Text). If it notices a change it sets the appropriate colour / text.

I have even tried taking this out of a class, and sticking it on the first frame of the root timeline. It seems like either I am missing something fundamental about flash’s display object heirarchy, or that functionality has been removed form the flash 9 ocx…

Any help would be greatly appreciated. Cheers

Hi all,
Benjamin, did you figure out how to set and get variables through these functions?

Because I have to work on a flash7 project, I need to set/get with SetVariable/GetVariable.
I can set and get a variable with these functions, but that doesn’t affect the real variable from the actionscript. It’s like creating a new variable from the container (in my case VC++ ActiveX), and then accessing this one. The actual variable from the actionscript doesn’t change at all.

It should be pretty easy, but…it just won’t work.

Thanks for any help.

DanHamm, your post was a godsend & a huge relief! Thanks for pointing us in the right direction (when a perfectly functioning c++/flash app died upon AS3 conversion of the flash shell…) We’re getting solid communication now through the loaderInfo obj, but since we’ve got a lot of legacy AS2 code that used the now-removed ‘watch()’ method, are there any onChange events you know of associated with the update of the loaderInfo.parameters obj that I could leverage to watch for incoming updates from the c++ wrapper? (REALLY don’t wanna go the setInterval route… ) Would I httpStatus or progress events? (waiting on a wrapper update at the moment so can’t test in the immediate) … Thanks for this and any further insights!