Actionscript 3 passing variable from parent to child - actionscript-3

I am still having problems so I made a very basic main.swf that loads sub.swf.
Sub.swf has 2 rectangle movieclips (red and blue) that have their alpha = 0.
Both swfs compile fine and no errors but the red alpha does not get turned on so the value of "spanish" from main.swf variable must not be getting recognised in sub.swf. Any ideas?
Main.swf actionscript:
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
var child_loader:Loader = new Loader();
var req:URLRequest = new URLRequest("sub.swf");
child_loader.load(req);
child_loader.contentLoaderInfo.addEventListener(Ev ent.COMPLETE, done);
function done(e:Event):void{
addChild(child_loader);
MovieClip(child_loader.content).language = "spanish";
}
sub.swf actionscript:
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
function callFunction(_txt:String):void{
var sublanguage = (MovieClip(parent.parent).language)
if (sublanguage == "spanish"){
red.alpha = 1;
}else if (sublanguage == "english"){
blue.alpha = 1;
}
}

The problem is that AS3 is not dynamic in the way AS2 was, so the child document needs to have a property or method that the parent knows is there.
One way to do this is relatively simple:
package {
public class ChildClass extends MovieClip {
protected var _property:String;
public function get property():String {
return _property;
}
public function set property(value:String):void {
if (value != _property) {
_property = value;
//do something, because now the property has been set
}
}
}
}
You'd just apply that as the Document Class to the swf you're loading, and then in your onLoaded function, you'd do something like:
//cast to ChildClass, so you will know you have the property available
var childClass:ChildClass = Loader(event.currentTarget).contentLoaderInfo.content as ChildClass;
//if the variable content is null, the swf had a different Document Class
if (childClass) {
//now you can set your variable, you're good
childClass.property = 'foo';
}
Like most things in Actionscript, you can do it the easy way (shown above), or you can do it the right way. The right way is to use an Interface. What an Interface is is the "blueprint" for a Class. In this case, it would need to say that any Class that implements that Interface would always have a property getter and a property setter. You can't just use a variable, because Interfaces only allow functions.
But at any rate, the advantage of using the Interface, especially in your case, is that you will not have to compile in the specific Class associated with the swf that you're loading. Instead, you'd just need to compile in the "contract" for the Class, which is much lighter weight. But because the player won't cast the contentLoaderInfo.content to ISomeInterface unless it fulfills that contract, you can confidently set that property once you have done the casting.

You should be able to set up a function in your child movie that can be called from your parent. You could pass anything you want as an argument.

Related

Referring to objects with the same parent via as3 class file

recently I got many (about 70) #1119 and #1120 errors in Flash. I've searched the web, but none of the solutions has solved my problem. Attempting to find the cause of the error myself, I made a new Flash animation. The contents:
A movieClip called "nr1" without instance name.
Inside of nr1 there are two movieClips, "nr2" with the instance name "ob2" and "nr3" with the instance name "ob3". Associated with nr2 is the as3 class file "nr2.as3". Here is the code inside nr2.as3:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class nr2 extends MovieClip {
public function nr2() {
// constructor code
this.addEventListener(MouseEvent.CLICK,func1);
}
function func1(e:MouseEvent){
parent.ob3.x += 50;
}
}
}
This should refer to the object with the instance name "ob3", which has the same parent as this (nr2). Still, I get two identical #1119 errors at line 15 (parent.ob3.x += 50;). How do I refer to an object with the same parent via an as3 class file?
It isn't a good idea to set ob3's property in nr2. You could dispatch an event in nr2, and add eventListener in parent, so the parent could catch the event and do something with bo3.
If you really want to set ob3's property in nr2, try this
function func1(e:MouseEvent) {
var ob3:MovieClip = parent['ob3'] as MovieClip;
if (ob3)
{
ob3.x += 50;
}
}
Agree with Pan, it's not a good idea to control a child from another child. Let the parent (nr1) who has references to both child do the control. So you should create nr1 class
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class nr1 extends MovieClip {
public function nr1() {
// constructor code
ob2.addEventListener(MouseEvent.CLICK,func1);
}
function func1(e:MouseEvent){
ob3.x += 50;
}
}
}

How to change the value of a certain variable in the document class through another class?

here is my problem. In my document class TowerDefenseGame.as, I defined a variable Turrent1Flag:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
public class TowerDefenseGame extends MovieClip
{
public var Turrent1Flag==0;
}
public function TowerDefenseGame()
{
......
}
Now, in another class Turrent1Button.as, I need to create a mouse click event, by which the Turrent1Flag is set to 1:
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Sprite;
import TowerDefenseGame;
public class TurretButton1 extends MovieClip
{
public var ButtonBase:Sprite=new Sprite();
public var TurretBase:Sprite=new Sprite();
public var Gun:Sprite=new Sprite();
public function TurretButton1()
{
......
this.addEventListener(MouseEvent.CLICK, MouseClick);
}
public function MouseClick(event:MouseEvent):void
{
MovieClip(root).Turret1Flag = 1;
}
Well, this does not work. I am using Adobe flash cs6 and it says the value cannot be accessed. Someone know how to do this?
try this:
private static var _instance:TowerDefenseGame;
public static function get instance():TowerDefenseGame { return _instance; }
public function TowerDefenseGame()
{
_instance = this;
}
public function MouseClick(event:MouseEvent):void
{
TowerDefenseGame.instance.Turret1Flag = 1;
}
So, to start out, AS3 makes it difficult to so what you've been used to doing in AS2 on purpose, to allow for better Object Oriented practices. When you maintain high and tight walls between Classes, it becomes easier to change Class A without having any effect whatsoever on Class B. Class B only knows about the doors and windows that are the "official" ways into the house of Class A, so it doesn't matter if you move the couch. You also make it extremely easy to replace Class A with Class C that has similar doors and windows.
When you reach through the walls by introducing global state, you can't replace Class B with Class C without changing Class A, because Class A has a direct reference to Class B and knows exactly where the couch is.
One way to handle this is through Inversion of Control (IoC). So, for our house, the couch might be supplied from outside and whatever component supplied it might keep a reference to it, so it would be able to access the couch no matter where in the house it went. So, you might choose to create your TurretButton and pass that into whoever owns that, while your TowerDefenseGame keeps a reference to it and listens directly to it, changing its own flag in response to the click.
Looking at your code, you probably don't need to go that far, because I don't see any sign that your TurretButton is actually nested. In that case, you can listen directly to it, whether you're creating it on the stage or creating it through code (which I'm not a fan of). If you're using the stage in the IDE and it exists on the stage, then just create a public variable of type TurretButton (or you could probably use SimpleButton since you no longer have need for a special Class here based on the code you've shown). That instance will be available in the constructor of your TowerDefenseGame. Just add your event listener to it, and then the listener and the variable you want to change are in the same scope. Ergo, the problem you were trying to solve never existed--you were simply looking at the problem from a perspective that overcomplicated things.
If, in fact, your code is nested in a way that's not shown, you can use ActionScript 3's event system, which is fabulous, to handle the issue without introducing direct coupling and without having to create the button through code and push it down to where it's used. One way is to just listen for any mouse click (since that is a bubbling event) and look to see what was clicked. Another solution is to generate a custom event from the button that you can then listen to from the top level to change the flag. That would look something like:
package view.button {
public class TurretButton extends MovieClip {
public function TurretButton() {
super();
mouseChildren = false;
addEventListener(MouseEvent.CLICK, broadcastTurretEvent);
}
protected function broadcastTurretEvent(e:Event):void {
dispatchEvent(new Event('turretClicked', true));//the true parameter makes it bubble
}
}
}
Then your tower Class would look like
package {
public class TowerDefenseGame extends MovieClip {
//the fact that you're calling it Turret1Flag suggests you're going to have more than 1
protected var turretFlags:Array /*of Boolean*/ = [false, false];
//your turret button instances
//doesn't show nesting, this is just to indicate that
//these are named instances so you know how what they're called
//they could be nested at any level
public var turret1:TurretButton;
public var turret2:TurretButton;
//...etc.
public function TowerDefenseGame() {
super();
addEventListener('turretClicked', onTurretClicked);
}
protected function onTurretClicked(e:Event):void {
//you can also just extract the number and do the math
//to get the correct array index
switch(e.target.name) {
case 'turret1':
turretFlags[0] = !turretFlags[0];
break;
case 'turret2':
turretFlags[1] = !turretFlags[1];
break;
}
}
}
}
Note how well this scales. You don't have to have a different Class for each button to change each separate flag. It's also easy to add more buttons without a whole lot of code changes. You could take this solution further and create a custom event that has a property that says which button was clicked, and you could supply the value to use to that for the button through dependency injection, etc.
I also made the assumption in my code that flags should be boolean and that they're turning on with the first click and off with the second click, etc. You could take the same idea and change it to, for example, increment with every click. I suspect you don't actually want to do what your code was showing and just turn it on with the first click and just leave it on forever.
Another note is that if you think you might want to code in AS3 over the longer term, you should probably learn the coding conventions that are used in AS3. One of these is that Class names start with a capital letter, but properties and methods do not.
It's probably not a great habit to get into for everything, but a static variable looks like it would work here.
public class TowerDefenseGame extends MovieClip
{
public static var Turrent1Flag = 0;
}
In Turrent1Button.as:
public function MouseClick(event:MouseEvent):void
{
TowerDefenseGame.Turret1Flag = 1;
}

Adding to Stage in ActionScript 3 from a .as file

Note: Yes, I know that similar questions have been asked before. However, after following the answers in such questions I'm still stuck and can't find a solution to my problem.
I'm having a problem which requires adding DisplayObjects to the Flash stage. Since I have to Display elements of several different classes, I decided to create a class to work as an intermediary between the .as files and the addChild function called "Displayer" as shown below:
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Stage;
public class Displayer extends Sprite //I read somewhere that DisplayObject
//as an extension can't be used for this, so Sprite will have to do.
{
private var _stage:Stage;
function Displayer()
{
_stage = new Stage;
}
public function displayElement(displayable:DisplayObject)
{
_stage.addChild(displayable);
}
}
}
I compile it and there appears a problem that I don't understand: Error #2012: Can't instantiate Stage class. Evidently, something in this code is either missing or out of place, but since it's so straightforward I fail to see what the problem can be. I'm sure that it's not very complicated, I probably just need an outsider's perspective.
The Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance.
refer a following docs.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Stage;
public class Displayer extends Sprite
{
var isAddedToStage:Boolean;
public function Displayer()
{
if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
isAddedToStage = true;
}
public function displayElement(displayable:DisplayObject):void
{
if(isAddedToStage)
this.stage.addChild(displayable);
}
}
}
You don't instantiate the Stage class, as the error says. Just like you cannot instantiate the DisplayObject class (which is why you have to extend Sprite).
Basically, you have two options here:
1) You add the child from a DisplayObjectContainer instance.
var displayerInstance:Displayer = new Displayer();
this.addChild( displayerInstance );
You would run this from a DisplayObjectContainer object that has already been added to the global stage. There is only a single stage in every project, even if you embed SWFs, the stage property of the SWF is actually the stage property of the top level application. So if you have this Displayer instance nested inside a class which is nested inside another class that is created in your main application, you would have to run "addChild" in each of those classes to get the Displayer to show.
2) You cheat. This is not recommended, at all. Basically, you pass in the stage object of an object when you instantiate the Displayer class.
var displayerInstance:Displayer = new Displayer( this.stage );
public function Displayer( stage:Stage ) {
this.stage = stage;
if ( this.stage ) {
this.stage.addChild( this );
}
}
This is a method that is good for adding Singletons to the stage (except there is not constructor for a Singleton). I created a profiler just before Christmas that was a Singleton (And later found Scout, damnit) that used this method for adding things to the stage when appropriate.
Again, that second option is not recommended for this situation, but it is a possibility.
As an aside, you should never add things directly to Stage, unless there is a clear reason for doing so (such as a popup). You should follow the display list methodology, where a DisplayObjectContainer adds another DisplayObject or DisplayObject container as a child and so on and so forth so that they are all connected to the TopLevelApplication.
Ok, I think instantiating a stage class won't do because as the as3 documentation says: "The Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance."
You should instead pass a reference to the Stage object to your Displayer class and you can get a reference to the stage object, as the docs say, via a display object instance.
So the constructor might now look like:
function Displayer( stage:Stage )
{
_stage = stage;
}
Assuming that the object which instantiates your Displayer is a child of the stage you can instantiate the Displayer by
displayer = new Displayer( stage );
If you use this approach there is no need for the Displayer class to extend anything or be added to the stage ( which is required btw in the approach of bitmapdata.com ).
There is always a simple solutions.if you need to add a child element into a stage from your class you can just pass the stage into your class as a object and add the child element into it, i did this for adding an image into my stage like this.
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.display.Bitmap;
public class ImageLoader extends Sprite{
private var stageObj:Object; //create local variable to refarance stage
public function loadeimage(StageObject:Object, Url:String){ //StageObject will bring the stage refarance into the class
var reQuest:URLRequest = new URLRequest(Url);
loader.load(reQuest);
stageObj=StageObject; //make local refarance for stage inside the class
var image:Bitmap;
image=Bitmap(loader.content);
image.x = 100;
image.y = 100;
stageObj.addChild(image); // add whatever object into stage refarance and this means the real stage..
}
}
}
only the things with comments are important and you can save this file as ImageLoader.as and import it and use it like this.
import ImageLoader;
var IL:ImageLoader = new ImageLoader();
IL.loadeimage(this,"img.jpg");
its simple as that. i think this is what you have search for... good luck. (you can pass any container or parant container this or this.stage it doesn't matter your child will be a part of it.

keyboard input with actionscript

I'm writing in pure actionscript in notepad with flex as a compiler. Here's the code I have
package
{
import flash.display.*;
import mx.core.*;
import flash.events.*;
import mx.collections.*;
import flash.geom.*;
import mx.controls.*;
import flash.text.*;
import mx.events.*;
import mx.styles.*;
import mx.containers.*;
public class MAIN extends Sprite
{
public var APPLICATION:Application = Application(Application.application);
public var FRAME:int = 0;
public function MAIN()
{
addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
STEP();
}
public function STEP():void
{
FRAME ++;
STEP();
}
public function keyDownHandler(event:KeyboardEvent):void
{
var keyDownText:TextField = new TextField();
keyDownText.text = "Key code: " + event.keyCode;
this.addChild(keyDownText);
}
}
}
What I want is for whatever key I happen to press to be drawn on the screen (though actually I think it would only be the ascii number it corresponds to but that doesn't matter). Right now though everything's just blank. Another thing is because I'm not using any mxml I don't know if i've established the game loop correctly so let me know if that needs to be fixed.
Try
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
instead of
addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
Since your Sprite doesn't have a visible area, it will not receive keyboard or mouse input.
Also the STEP() function will cause a stackoverflow because it's infitely recursive.
If you want a main loop that gets called periodically, try using an ENTER_FRAME handler or a Timer.
Something like this:
public function MAIN()
{
addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
addEventListener(Event.ENTER_FRAME,STEP);
}
private function STEP(e:Event):void {
FRAME++;
}
To stop it, do this:
removeEventListener(Event.ENTER_FRAME,STEP);
Not sure why you are using ALL CAPS for some methods and variables. Although it's not a language requirement, all caps are generally reserved for constants. Method names use camelCase. And types use PascalCase (like camelCase, but the first letter is capitalized). So your class would be Main, FRAME would be fram, STEP would be step, etc. You're better off sticking to these common naming schemes, I think.
And another thing. You probably shouldn't be creating a new TextField instance everytime you want to output some text. One textfield will do it in your case, I think. So create and addChild the textfield on some kind of init method that you call when you start your class and then just use the text property of this textfield to write your messages.

ActionScript 3.0 stageWidth in custom Class

How do I access Stage Class properties in Costum Class?
Class:
package {
import Main;
import flash.events.*;
import flash.display.Sprite;
import flash.display.Stage;
public class Run extends Sprite {
var obj:a1_spr;
public function Run() {
runAssets();
}
private function runAssets():void {
obj = new a1_spr()
addChild(obj);
obj.x = stage.stageWidth/2;
}
}
}
Output:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
To expand on what Joel said, and put it into context:
Every display object has a .stage property, but that property is null until you add you display object onto the display list. So during construction, you will never be able to access it, (because it gets added afterwards)
The event ADDED_TO_STAGE gets fired when you add your object to the stage, ltting you know that the .stage property is now populated. After that happens you can access the stage from anywhere in you object.
Hope that clarifies things for you.
this.addEventListener(Event.ADDED_TO_STAGE, handleAdedToStage)
private function handleAddedToStage(event:Event):void
{
this.runAssets()
}
private function runAssets():void
{
obj = new a1_spr();
addChild(obj);
obj.x = this.stage.stageWidth/2;
}
You aren't going to have access to the stage in the constructor (unless you inject the stage into the class). Sprite has a stage property.
when flash compiles the fla assets with your .as files, there's no stage. so the code is initiated as preparation for your documentclass, you have to listen to if there's a stage so it can be rendered.
that's why you listen to ADDED_TO_STAGE , to check it's actually in the display list.
This problem occurs for all display objects, since they must be added to the display list when there's an actual stage.
get used to add that listener, and check for a stage. specially when working in a team and your doing your own components in a larger project.