AS2 to AS3 migration create empty movie clip with dynamic name - actionscript-3

I am trying to migrate as2 to as3 code. The normal solution to my problem involves using the first parameter of the createEmptyMovieClip() as the name of your movie clip. In several instances I have a dynamic value for this first parameter- so my question is How should I go about doing this?
//my code
function someFunction (){
loader_mc = this.createEmptyMovieClip("text"+value, value);
value++;
//do stuff with it
}
//normal non-dynamic solution
function someFunction (){
var text:MovieClip = new MovieClip();
addChild(text);
//do stuff with it
}

In your question, you're already saving a direct reference to the MovieClip. If that's the only reference you need to do your work, then you really don't need to give the MovieClip a name.
If you do need the name, though, you can always assign the MovieClip a name after the fact:
var myClip:MovieClip = new MovieClip();
myClip.name = "text"+value;
parent.addChild(myClip);
This will let you use the getChildByName method:
parent.getChildByName("text"+value);

Related

Replace movieclip palced manualy in timeline with movieclip from library

I have a problem that after a lot of searches I didn't manage to solve it.
I created 2 movie clips in the library (let say mc1 and mc2). Then I manually create another movie clip in timeline (using IDE), let say foo.
As background inside foo I use mc1 with instance name bg. In ActionScript I'm trying to replace the foo.bg with mc2.
Please note that I already exported mc1 and mc2 to first frame with classes mc1() and mc2().
I tried (without any luck) :
var mc1:movieClip = new mc1()
var mc2:movieClip = new mc2()
if (....)
{
foo.bg=mc2;
}
Thanks
Mistake №1: managing objects in display list is not the same thing as assigning variables.
Mistake №2: variable names and class name MUST be different. On a case-level, at least.
So, you need to do something like that:
var mc1:MovieClip = new MC1;
var mc2:MovieClip = new MC2;
// ...
// Delete existing background.
foo.removeChild(foo.bg);
// Set the new background.
foo.addChild(mc2);
foo.bg = mc2;

How to make an instance move

I got the following code and the flash events are all loaded properly. ball is a movie clip and is assigned a class.
var speedx: Number=5;
var speedy: Number=3;
var myball = new ball();
addChild(myball);
addEventListener(Event:ENTER_FRAME,ballmove);
function ballmove(e:Event):void{
myball.x+=speedx;
myball.y+=speedy;
}
But now the instance myball won't move, it is just stuck at position 0,0. Kindly help me and advice how to get this myball to move along a staright line..
If I had dragged and dropped the instance from the library, then
myball.x+=speedx;
myball.y+=speedy;
worked perfectly, but doesn't work after addChild is given.
In your code you incorrectly used Event:ENTER_FRAME when it should be : Event.ENTER_FRAME. I wonder if there is a "silent" error? When testing movies, use Ctrl+Shift+Enter to run the debugger (gives better feedback on issues/errors).
var myball = new ball(); does not mean anything. You declare instance by using this logic : var Name : Type of Data = Value, In your code here you've said only that var Name = Value. Surprisingly it works but I wonder if getting comfortable with that could lead to issues on bigger future projects?
Instance name only affects objects already on Stage. If you're adding by code (from Library) make sure you create a new instance of object by using its Linkage name.
Solution :
In the library, right-click the "ball" object and choose "properties", in there make sure "name" is ball and tick "Export for ActionScript" (you should see "Class" becomeClass: ball). OK that.
Now in your code, you can create new instance by : var myball: ball = new ball();...
Your code should look like
var speedx: Number=5;
var speedy: Number=3;
var myball: ball = new ball();
addChild(myball);
addEventListener(Event.ENTER_FRAME, ballmove);
function ballmove(e:Event):void
{
myball.x += speedx;
myball.y += speedy;
}

ActionScript 3 - use [brackets] instead of getChildByName

I have a MovieClip inside library, linkaged to MyObject and it contains a textField.
I don't know how I can access this textField without using the getChildByName method.
Apparently, the 3rd section works when object is on stage (without using addChild). But when using addChild I think there has to be some kind of casting; which I don't know how.
var childElement: MyObject = new MyObject();
childElement.name = "theChildElement";
container.addChild(childElement);
btn.addEventListener(MouseEvent.CLICK, changeText);
function changeText(event: MouseEvent): void
{
var targetBox:MovieClip = container.getChildByName(childElement.name) as MovieClip;
targetBox.textField.text = "hello"; // THIS WORKS
// This works too:
// MovieClip(container.getChildByName("theChildElement"))["textField"].text = "hello"; // THIS WORKS TOO.
// THIS DOESN'T WORK. why?
// container["theChildElement"]["textField"].text = "hello";
}
As confusing as it may seem, instance name, and name are not the same. From your code you should always be able to get to your MC by it's variable name. To get your last like to work you could just use this.
childElement["textField"].text = "hello";
There is a difference between Symbols created by the Flash IDE, which aggregate other DisplayObjects and programmatically created DisplayObjects.
When a DisplayObject is created in the Flash IDE, it's instance name can be used to resolve the instance as a property - which means it can be accessed via []. The [] can be used to access properties or keys of dynamic declared classes - like MovieClip. This necessary because you'll most likely down cast to MovieClip instead of using the symbol class created by Flash. That is not possible when simply using addChild, addChildAt or setChildAt from the DisplayObjectContainer API.
It is always the save way to access it via getChildByNameand check for null because otherwise your app, website or whatever is doomed for 1009 errors as soon as someone is changing the symbols.
I'd create a bunch of helper methods, like
// not tested
function getChildIn(parent:DisplayObjectContainer, names:Array):DisplayObject {
var child:DisplayObject, name:String;
while (names.length > 0) {
name = names.shift();
child = parent.getChildByName(name);
if (!child) {
// log it
return null;
}
if (names.length == 0) {
return child;
}
}
// log it
return null;
}
function getTextFieldIn(parent:DisplayObjectContainer, names:Array):TextField {
return getChildIn(parent, names) as TextField;
}
function getMovieClipIn(parent:DisplayObjectContainer, names:Array):MovieClip {
return getChildIn(parent, names) as MovieClip;
}
Your third method doesn't work because you are trying to call the ChildElement by it's name
without using getChildByName method. On the other hand, you shouldn't call your textField textField, because that's already an actionScript property.
Your should rather call it 'displayText' for example.
For a textField called 'displayText' contained in childElement :
function changeText(event:MouseEvent): void
{
childElement.displayText.text = "hello";
}

Not able to create dynamic instance of flash object

Ok so i'm trying to display multiple instance of an object square(movie clip) on different location on screen.
var k1=0,k=0,p=0;
for (k1=0;k1<5;k1++)
{
drawrect(k,p);
k=k+101;
}
This above code is for number of times i want to display the object om screen horizontally in line.
import flash.display.MovieClip;
function drawrect(x1:Number,y1:Number){
// statements here
trace("Hello world!");
var MC_squre:MovieClip= new MovieClip();
MC_squre.x=x1;
MC_squre.y=y1;
addChild(MC_squre);
};
this Function is trying to declare instance of movieclip SQUARE and place it at given parameter in function.i might want to make it and array of the instance later too.i tried this code with just
var square:Sprite = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x000000);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
and it worked but i want to now use it by library object not just on drawn shapes.
What you have to do is to make your object in the library accessible by actionscript.
To do that, go into the properties of that Clip, click on "Advanced", and check the box "Export for Actionscript".
When pressing "Ok", you will get a warning that such a class does not exist and it will be automatically generated. That is ok.
Whatever you put as a classname in the Properties, you then have to use to instanciate your objects. If it is for example MySquare, use that.
So your above code would look like this:
function drawrect(x1:Number,y1:Number){
// statements here
trace("Hello world!");
var MC_squre:MovieClip= new MySquare();
MC_squre.x=x1;
MC_squre.y=y1;
addChild(MC_squre);
};

ActionScript operator as

does anybody have a good explanation of the as operator?
On one hand it seems to me, that it is often better to use as instead of instantiating a new object.
But then there are situations, when this operator let's me down. For example when loading a text file in XML format through an URLLoader:
private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace("completeHandler: " + loader.data);
var x:XML = new XML(loader.data);
trace("x=" + x);
}
Why do I have to use a constructor here? Why can't I say var x:XML = loader.data as XML; and save some memory?
Thank you for any insights!
Alex
as evaluates whether a variable's type is a super class or subclass of another class. It does not create a new object. The difference to is being that while is returns a Boolean value, as returns either an object of the desired type, or null. It is used for type casts.
See the ActionScript documentation.
A typical use case would be using a MovieClip on the stage, which is retrieved by instance name:
// This will not compile:
var d:DisplayObject = stage.getChildByName ("instance1");
d.gotoAndPlay (2);
// This will play the MovieClip from frame 2
var m : MovieClip = stage.getChildByName ("instance1") as MovieClip;
m.gotoAndPlay (2);
stage.getChildByName() always returns a DisplayObject, regardless if it's really a MovieClip, Sprite, Button, etc. So if you want to use any of class MovieClip's methods, you need to type cast to MovieClip first. This does not, however, create a new MovieClip but simply ensures that you are using the correct type.