Targeting movieclip from an AS3 class file – reply
This is a reply to Brian Chau’s post here, but my comment didn’t go through too well, so I’ll repeat it here…
————————————-
As Tink says, it’s normally better to encapsulate the functionality of each class using events and public methods instead of exposing the internals.
For this simple example, you might rewrite it something like…
Test.as
package { import flash.display.MovieClip; public class Test extends MovieClip { private var blackbox_mc:BlackBox; public function Test() { blackbox_mc = new BlackBox(); this.stage.addChild(blackbox_mc); var myTest:Control = new Control(this); } public function positionBlackBox(x=null,y=null) : void { blackbox_mc.x = (x==null)?blackbox_mc.x:x; blackbox_mc.y = (y==null)?blackbox_mc.y:y; } } }
Control.as
package { import flash.display.MovieClip; public class Control { private var view:MovieClip; public function Control(mc:MovieClip) { view = mc; view.positionBlackBox(null,100); } } }
..although to me this still feels a bit odd as the main class is the view, it kinda shows how to break the two. The controller only knows what to view controll and what public methods it’s allowed to call on the view, nothing of how the view works internally.
If you then want to use a design time created blackbox you can simply plop the mc on stage, call it blackbox_mc and delete the instantiation references from the view, e.g….
Test.as
package { import flash.display.MovieClip; public class Test extends MovieClip { public function Test() { var myTest:Control = new Control(this); } public function positionBlackBox(x=null,y=null) : void { blackbox_mc.x = (x==null)?blackbox_mc.x:x; blackbox_mc.y = (y==null)?blackbox_mc.y:y; } } }
As the blackbox_mc is already on the views stage flash auto creates the reference, so you can just use blackbox_mc without creating it.
Hope that helps.
A
I’m trying use a Class to control a bunch of movieclips I have created and instanced in Flash. I’ve already got the .swf working using only Flash, but I’m trying to learn how to write classes. (Reading past the added array stuff, which seems to be working in Flash) My references to, for example, topTextMischief_mc.visible = true is not working in the class. I get this error:
ReferenceError: Error #1069: Property topTextMischief_mc not found on ShowHide and there is no default value.
at ShowHide()
at testingarrayreplacement_fla::MainTimeline/displayRecipe()
at testingarrayreplacement_fla::MainTimeline/mischeifIn().
Here is the Class I have tried to create:
package
{
import flash.display.MovieClip;
public class ShowHide extends MovieClip
{
public var _buttonNumber:Number;
public var elementNames:Array = ["Mischief","SnapDragon","ShanghaiLemonDrop","AppleBlossom","GingerSnap","Yazitini"];
public var subElementNames:Array = ["topText","circleText","glass"];
private var graphicState:Boolean = false;
public function ShowHide(bNum:Number) {
_buttonNumber = bNum -1;
for (var i:Number = 0; i < 6; i++) {
if (i == _buttonNumber) {
graphicState = true;
}
else {
graphicState = false;
}
for (var j:Number = 0; j < 3; j++) {
this[subElementNames[j] + elementNames[i] +”_mc”].visible = graphicState;
// trace(subElementNames[j] + elementNames[i] + graphicState);
}
}
}
}
}
What could I do to get this working?
I’m afraid the answer to me should be “learn how Flash works, oh ignorant one.” Or “Try starting at the beginning, instead of diving into the deep end of Classes too early.”
This IS only my second Flash site… I have spent hours and hours trying to figure this out from books and the web, to no avail.
Here is the comp of the site I am working on:
http://www.bolesky.com/yazi