Recreating stage layout in as3 - actionscript-3

I have 27 MovieClips in my Library which are all images. At the moment they are positioned on the stage as instances of their parent and then made to function in the first frame of my actions layer. I want to recreate this layout solely in code so there is nothing on the stage. How do I do this?
Thanks in advance.
Sam

Right click on a movieclip in the library, then go to Properties.
Tick "Export for ActionScript", then check the name where it says "Class". Hit OK.
Let's say this name was "Symbol1".
Then type this script:
var symbol1:MovieClip = new Symbol1();
addChild(symbol1);
var symbol1 means you created a variable, MovieClip is the type. This MovieClip variable is a "new " Symbol1 (this was the name in the library, Properties, Class.
Then add this to the stage:
addChild(symbol1)
If you want to position it on the stage, set the coordinates of the variable:
symbol1.x = 10;
symbol1.y = 10;
puts it to (10, 10).
Depending on how many objects you have you can type this code for each one of them (don't forget to Export them for actionscript in Library->Properties).
If you have tons of movieclips and you don't want to type evertyhing, but would rather write some dynamic code, give us a hint on your library structure and how you named your objects.
Hope this helps.

Related

Using a movieClip as a variable

Previously in my code, I had a Variable, Chip. Chip moved around the screen and everything was fine.
var chip:Shape= new Shape();
chip.graphics.lineStyle(0,0);
chip.graphics.beginFill(0x3333FF,0.8);
chip.graphics.drawCircle(0,0,10);
chip.graphics.endFill();
was the code to make chip. Now I've make a movieClip and I would like to use the movie clip instead, so I know I'd start with something like
var chip:movieClip;
but I'm lost after that. How would I be able to use him in place of making the shape?
So I'd guess that you are using Flash IDE and have created a movieclip for your Chip that is now in your library and you don't know how to add it to the stage :)
Right-click on your movieclip in the library and select "Properties". In the advanced section select the "Export for actionscript" checkbox and give your movieclip a linkage name in the "Class" Input field, for example "Chip". Leave the "Base name" as MovieClip.
Now your movieclip is accessible via actionscript and you could add it to your stage:
var chip:ChipMC = new ChipMC();
addChild(chip);
trace(chip.x, chip.y);

How to access variables within MovieClip (AS3)

I'm new in AS3 and I wanna ask how to access variables inside MovieClips by its name.
in AS2 usually, I use its instance name followed by .(name of variable)
for example...
I have a variable named baru inside a MovieClip named my_mc.
usually I use my_mc.baru to reveal the value of the variable.
Now, i want to know how to do it in AS3
Thank you.
In AS3 there's a few separate but related things you need to be aware of:
All display objects (including MovieClip) can have a name property.
var mc:MovieClip = new MovieClip();
mc.name = "myMC";
myContainer.addChild(mc);
You can find a child by its name using getChildByName() on its parent.
var myMC:MovieClip = myContainer.getChildByName("myMC");
MoveClip instances can have dynamic properties that point to children (or anything at all).
myContainer.myMC = myMC;
(Note that other display object types, such as Sprite, do not allow dynamic properties. Instead you need to create a custom class with class properties.)
When you create a display object in code (as shown above), neither the name or a property on the parent will be automatically created. You can do it manually as shown above.
When you place a symbol instance in the authoring tool and give it an "instance name", both the name and a property on the parent symbol will be assigned by that "instance name". In this case you are not allowed to change the name in code.
Note that in AS2 using createEmptyMovieClip or attachMovie would assign the _name and create a property on the parent, but there's no AS3 equivalent to these functions. This is where a lot of confusion can come in.
So as you can see the way this works is really not different than AS2, except that AS2 had functions that did several things for you (create and add the MovieClip, set the name and add a property on the parent). The problem with AS2 was that you couldn't re-parent an object after it was created, which was a big limitation. In AS3 you can freely move things around.
If you just like to write code inside the MovieClip but not a *.as,you have to add a TextField in the MovieClip by hand,set it's type to Dynamic,then type the variable string in it,then get it use: MovieClip.TextField.text and don't forget: TextField.alpha=0.
It is not a wise way.

AddChild with GetChildByName

Complete AS3 noob here - I've tried Googling this, but I can't seem to find what I'm looking for (I stumbled across this, http://ub4.underblob.com/as3-naming-elements-dynamically/, but it doesn't weem to work for me).
I'm trying to dynamically add a Movieclip inside another Movieclip through an external AS3 class
Something like this:
var bullet:Bullet = new Bullet(x, y, "right");
var stageBackground:MovieClip = (stage.getChildByName("back") as MovieClip);
stageBackground.addChild(bullet);
However, while this compiles correctly, at run time, I get error #1009 - Cannot access a property or method of a null object reference.
The debug panel tells me the problem is with this line:
stageBackground.addChild(bullet);
But I can't seem to figure out what's wrong with it. I've tried recasting stageBackground as a Sprite, but that didn't change anything. I know the MovieClip back exists - when I reference it through near identical code in my document class, it works perfectly.
You are accessing stage here to find your container, which is very likely the problem.
You are probably thinking that the stage property refers to "the stage" in Adobe Flash authoring environment.
That's not true.
If you placed a MovieClip on "the stage" in the Flash IDE, it ends up on the main time line, this however, is not the thing the stage property is referencing. stage is the topmost DisplayObjectContainer in the display list. It only exists at runtime. It more or less represents the FlashPlayer window, the runtime environment that executes your .swf file.
In short: you are simply looking for your back MovieClip in the wrong place.
The property of a container that represents the main timeline is root.
Do not use root either.
As you can see, your code becomes dependent on the display list
structure of your application. You are already struggling to find the
container that you are looking for. If you change the structure, your code breaks. Even changing the name of the container (for example to something like "background") will wreak havoc.
Instead, use Events.
You are in another class and you want to fire a bullet.
So you create that bullet same as you do now:
var bullet:Bullet = new Bullet(x, y, "right");
Next, dispatch an Event to notify the rest of your code that a bullet was created and it should be placed in the appropriate container:
dispatchEvent(new BulletEvent(BulletEvent.CREATED, bullet));
(Create a custom event class BulletEvent that extends Event, with the apropriate setter and getter for a Bullet object)
Register a listener on the object of your class that creates the bullets, you will catch this event and place the bullet in the container:
var object:YourClass = new YourClass();
object.addEventListener(BulletEvent.CREATED, addBulletToContainer);
function addBulletToContainer(e:BulletEvent):void
{
// adding the bullet to the container
back.addChild(e.bullet);
}
This code would be placed in the parent of your back MovieClip.
The Flash IDE automatically creates variables behind the scenes that have the same names as the instance names. That's why the variable back is available here.
Using events here allows you to literally fire the bullet into your code with somebody else taking care of it, where it's easy to figure out the container it belongs into.

How to link functionality from Class file to DocumentClass and to Movieclips in AS3?

and i am having problem, switching from Timeline code to OOP/Document Class. I managed to build the Fla with AS3 on timeline with no problem. But totally clueless when on OOP.
I had been told that Scenes are no good and i should stick to saving my scenes as Movieclips.
My situation is as such:
I have 8 pages of PSD files, and i imported each PSD into Flash Pro, and each PSD has a few buttons and textinput. First page is Login page, second page is register page etc.
My questions are:
1.) How should i save the PSD? Do i save them as nested Animation,(Giving each item on PSD a symbol? Buttons and textinput?) Then saving that PSD as 1 movieclip(Nested Animation?)
I already tried importing the PSD with Flash Layers onto Stage,then giving each buttons and Textinput their Properties then savin them as Nested Animation, but do i call that Movieclip from the Class document? Or do i link All Movieclips in the Document Class(Main.as)?
2.) How do i access the Movieclip from Class file, I tried var login:Login = New Login, then addChild(login). That adds the movieclip but none of the functionality works, and errors saying Access of Undefined Properties for every single button's Name.
3.) And if 1 button is clicked and that links to another page(PSD) do i do the Below?
h.addEventListener(MouseEvent.CLICK, fl_ClickToGohome);
function fl_ClickToGohome(event:MouseEvent):void
{
gotoAndStop("register.as");
Thanks for your time
Programming in .as files apart from the timeline isn't too different. If you're using the document class (found in Actionscript Settings panel), you may be feeling the abrupt change in class specific programming a bit too daunting. For a smoother transition, you can simply dump your current timeline code into a new code.as file (as-is), and simply drop the following line in your timeline:
include "code.as";
This effectively just copy/pastes the code and runs it, except now you can use a proper code editor (such as Sublime Text). Furthermore, as it's not a new class, the code you write there has the same scope as your timeline.
2). Functions only have access to the namespace their created in (this is probably why your document class was throwing "Access of Undefined Properties" errors). You can always pass a reference to an object (and by extension, its namespace) through function arguments.
1). To answer your first question, you can create your movieclips, and nest them as deeply as your want, in any order you want. The important thing is to be aware of how to navigate to that object. Take for example the following stage heiarchy:
root:MainTimeline ¬
0: PSD_one:MovieClip ¬
0: backgroundImage:Bitmap
1: button:Sprite
2: txt:TextField
1: PSD_two:MovieClip
2: PSD_three:MovieClip
The timeline has 3 objects, each with a zero-based index. The first PSD is a MovieClip DisplayObjectContainer with 3 child elements, each with name properties that we can use to address the objects. In Flash IDE we label these "instances", and they automatically become properties on the parent object due to internal magic the IDE has enabled in the Actionscript Settings panel labeled "Automatically declare stage instances".
This means, to change the text on txt, we can write PSD_one.txt.text = "foo". Without this setting, you'll need to use container methods like root.getChildByName("PSD_one").getChildAt(2). Outright calling txt.text = "foo" will never work because there is no property on the current scope called that (ie., this.txt is implied).
3). gotoAndStop is a MovieClip method that controls timeline frames. If you're really going to make a clean break from the old way of doing things, you should drop your use of frames.
There are two ways you could approach displaying these PSDs.
Method 1:
You instance your PSDs on the stage with the Flash IDE and give each one a unique name that you can then reference in your code. Assuming the layout above, you may simply move each PSD offscreen (such as with PSD_two.x = this.loaderInfo.width), and swap them to the center of the screen when you want to "go" to the next "frame".
Method 2:
You've imported your PSDs into your library as you're accustomed to, but do not instance them. Instead, you jump straight into your code and when someone clicks on button h to go to fl_ClickToGohome, your function picks the library object and manually instances it, and parents it to the stage with addChild().
function fl_ClickToGohome(e:MouseEvent):void {
var psd:PSD_one = new PSD_one();
addChild(psd);
}
This sort of approach would be preferable if you're going to start getting into dynamic loading of assets. Of course, rather than having an already created PSD_one class in your library, you'll simply use URLLoader() or Loader(), and you'll want to save out compressed images, not full-blown PSDs.
I hope this helps. I know I haven't answered your questions directly, but sometimes the issue stems from manner of implementation, not specific code.
-Cheers
To answer your questions...
1: Pathing to Objects
If you're not sure what the path is to your objects, try this function. It's a trimmed down version of the one I use in my own work.
function listLayers(obj = null, indent:String = ""):void {
// If no object was provided, start with the MainTimeline
if (obj == null) { obj = root; }
for (var i:int = 0; i < obj.numChildren; i++) {
// Make a pointer to the object at this index
var item = obj.getChildAt(i);
// Get the item type
var type:String = flash.utils.getQualifiedClassName(item);
if (type.lastIndexOf("::") != -1) {type = type.split("::")[1];}
var msg:String = indent + i + ": " + item.name + ":" + type;
if (type != "TLFTextField" && item.hasOwnProperty("numChildren") && item.numChildren > 0) {
trace(msg + " ¬");
listLayers(item, indent + " ");
} else {
trace(msg);
}
}
}
This will print out the structure of your stage in much the same format as you saw above (with the PSDs). You can then use getChildByName from the root to the child (the full path must be provided, much like navigating folders on your hard drive).
2: Public/Private Namespaces
These are only to be used in dedicated classes. If you're using the include method I mentioned, you omit these as you would in your timeline code. They denote whether a property/method is accessible from "the outside" of the class. If you're just starting out, leave everything Public. You can worry about memory optimizations later when you're ready to tackle it.
3: How does the class access the buttons?
The easiest way to think of the DisplayList is as a folder structure.
You might write
C:/Users/Me
Which in the DisplayList would look more like
C.Users.Me
Of course, we don't have a root location called C, and your objects are probably called PSD_one and myButton, so we can rewrite it as follows...
root.PSD_one.myButton
This is assuming you actually have those properties pre-defined on the objects, which does not happen when dynamically creating objects. In those cases, you'd string together your commands, as follows:
root.getChildByName("PSD_one").getChildByName("myButton")
When you write a Class, it's like creating a new computer on your network. There is no direct relationship between that Class and your MainTimeline. You need to connect them.
If you instance a DisplayListObject (such as a Sprite) and add it to the stage, the object automatically gains a few properties like stage, root, and parent which until it was parented were in fact null. These are properties that the class can reference from inside itself to connect to the MainTimeline and access objects on it.
Conversely, if you wanted to you could pass a reference to the stage to the class from the constructor arguments, as follows:
var foo:MySprite = new MySprite(stage);

AS3: Making a SimpleButton from library sprites: Possible?

I'm working on a small project in AS3, and I need to make some interface buttons. I had them as separate classes at first, but then realized that it was probably overkill, and on top of that, figured out a way to simplify the event calls by making them buttons and assigning the event dispatches to their parent.
ANYWAY,
I tried remaking them using the SimpleButton class, but I can't figure out how to give the buttons any sort of design. Every tutorial on the web uses SimpleButton to make only the most bare-bones Actionscript graphics by actually drawing them with the code (why anybody would want to do that is beyond me), and my attempt at assigning a library item to the upState:
_deletebutton = new SimpleButton();
_deletebutton.upState = mc_deleteButtonUp; <--- exists in my library
doesn't do anything.
The Adobe docs say that the various states take DisplayObjects, which mean they take Sprites and MovieClips, so you should be able to do this. Does anyone know how?
THANK YOU
+1 weltraumpirat
the example in the doc generates the states by code but you can assign whatever displayObject to the different states of the button.
var btn:SimpleButton = new SimpleButton();
btn.downState = new clipFromLibDown();
btn.overState = new clipFromLibOver();
btn.upState = new clipFromLibUp();
btn.hitTestState = new clipFromLibHit();
btn.useHandCursor = true;
addChild( btn );
assuming you have 4 states called : clipFromLibDown, clipFromLibOver ... in your library, this works
You have to instantiate an item from your library in order to use it with ActionScript. To do this, click "Export for ActionScript" and assign a class or base class to mc_deleteButtonUp in the properties panel. Then use the new operator with the assigned class to instantiate it. You can start your button by using the example from Adobe's documentation, then change things to fit your own program.
You're probably not setting the hitTestState property of your SimpleButton instances. This property is a DisplayObject that defines where the user has to move the mouse to get mouse events on the SimpleButton. You will never see the DisplayObject that you set this to. I would suggest just using one of the DisplayObjects you are already using for another state.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/SimpleButton.html#hitTestState
Also you will need to use the new operator as weltraumpirat and nicoptere have already said.