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