Replace movieclip palced manualy in timeline with movieclip from library - actionscript-3

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;

Related

How do I access dynamic text on the stage from inside a movieclip which is inside another moveclip in AS3?

Before asking, I've read other articles for similar issues and have not been able to get it working. I have a dynamic text on the stage with the instance name of txtX. Then in action script in frame 1 of the main timeline I add an existing movie clip, mc1, using code:
var mc = new MC();
addChild(mc);
I then add another movieclip, mc2, with action script on frame 1 of the main timeline I add it as a child to mc1.
var mc2 = new MC2();
mc.addChild(mc2);
Now, in the class (created class for export AS) for mc2 I am trying to modify the text of the dynamic text but cannot for the life of me figure out how to reference it. I want to say:
stage.txtX.text = "blah blah";
or even
parent.parent.txtX.text = "blah blah";
but I usually get an error similar to:
Access of possibly undefined property txtX through a reference with static type flash.display:DisplayObjectContainer.
The error above is for the parent.parent.txtX.text line. Please tell me what I'm doing wrong. Thanks. Also I know that the variable names are nonsensical but it's just for my example. In my code the names make much more sense.
When you put a text field on your main timeline and give it an instance name of txtX, it actually is not a direct child of stage. The stage would be it's grandparent (the main/root timeline would be it's parent).
As such, if mc1 & mc2 are it's siblings (also on the main timeline), you could access your text field like so:
MovieClip(parent).txtX.text = "Hello";
Alternatively, you can access the root (main) timeline with the root keyword:
MovieClip(root).txtX.text = "Hello";
And that should work on any timeline no matter how deep/nested.
I figured it out finally. Not sure if it was the correct way to do it but in the parent MoveieClip, I created a TextField in code:
import flash.text.TextField;
import flash.text.TextFormat;
public var txtX:TextField;
txtX = new TextField(); txtX.x=0; txtX.y=0; addChild(txtX);
public function changeTxt( t )
{
tf = new TextFormat();
tf.size=4;
tf.color = 0xFFFF00;
txtX.text = t;
txtX.setTextFormat(tf);
}
Then in my MovieClip child class I called the parent's method like this:
MovieClip(parent).changeTxt( "Hello" );
A lot more work than I'd hoped for but it seems to work.

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;
}

How to activate a TextField and Duplicate Movie Clips

I want to ask,
1 How to give focus to a TextField in AS3. in AS2 usually I'm using
Selection.setFocus("<name of textfield instance>");
fscommand("activateTextField");
2.How to duplicate movieclips in AS3. in AS2 I'm using
<name of movie clip>.duplicateMovieClip();
Thanks
These are two separate questions, both of which are answered online:
Use stage.focus to set the focus on any display object:
stage.focus = name_of_textfield;
There's no direct equivalent to duplicateMovieClip, but you can do approximately the same thing by creating a new instance from the original display object class and copying over properties from the original:
function duplicateDisplayObject(target:DisplayObject):DisplayObject {
var targetClass:Class = Object(target).constructor;
var duplicate:DisplayObject = new targetClass() as DisplayObject;
// duplicate properties
duplicate.transform = target.transform;
duplicate.filters = target.filters;
duplicate.cacheAsBitmap = target.cacheAsBitmap;
duplicate.opaqueBackground = target.opaqueBackground;
if (target.scale9Grid)
duplicate.scale9Grid = target.scale9Grid;
if (target.parent)
target.parent.addChild(duplicate);
if (target.hasOwnProperty("graphics") && target["graphics"] is Graphics)
Graphics(duplicate["graphics"]).copyFrom(Graphics(target["graphics"]));
return duplicate;
}
duplicateDisplayObject(name_of_movieclip);

Actionscript 3 how to duplicate a symbol onto the next frame

Im a total beginner at using AS3. I want to know is how does one duplicate a symbol and make it appear in the next frame.
example: when the user clicks on the symbol in that frame, the same symbol will appear in the next frame. If it is not possible then how does one move that symbol to the next frame.
Thank You in advance for answering.
You should probably avoid to use timeline keyframe to achieve this kind of thing. However, if you really need to do it this way, here what I would do :
First, make sure the MovieClip you want to clone has ActionScript linkage.
Then:
my_mc.addEventListener(MouseEvent.CLICK,onClick)
function onClick(e:MouseEvent):void{
var m:MovieClip = MovieClip(e.currentTarget);
var c:Class = Object(m).constructor;
var clone:MovieClip = new c() as MovieClip;
gotoAndStop(2);
addChild(clone);
}
This will add the clone to the stage, so if you go back to frame 1, you'll see the clone. There's no way to add an object to a specific timeline frame. If you want to achieve such a thing, you have to target a container on frame 2 and add to clone to the container.
something like this :
function onClick(e:MouseEvent):void{
var m:MovieClip = MovieClip(e.currentTarget);
var c:Class = Object(m).constructor;
var clone:MovieClip = new c() as MovieClip;
gotoAndStop(2);
myContainerOnframe2.addChild(clone);
}

Edit movie clip definition as3

I'm a bit new to as3, so forgive me if these are dumb questions. Two questions...
Premise:
I'm loading a character from a swf file, and want to add avatar to it. I have him animated walking and also standing (stand_mc, walk_mc). I also have his body parts separated out, so inside each of the animations mc's is a head_mc, body_mc, etc etc.
First question, how can I access the body parts for any animation? here's my code so far:
var WalkAnim:Class = SWFLoader.getClass('walk_mc'); //Using Greensock loader; but it's the same as using appDomain.getDefinition();
var walkAnim:MovieClip = new WalkAnim();
addChild(walkAnim);
Second question, adding walkAnim just creates an instance of the mc definition. How can I edit the definition in the library to do something like..
var Hat:Class = SWFLoader.getClass('accessory_hat_mc');
var hat = new Hat();
WalkAnim:addChild(Hat)//???
So that if I have multiple instances on stage, they'll all be updated. Thanks in advance for the help!
Basically, to access child elements, you use dot syntax. That would look something like this (last line):
var WalkAnim:Class = SWFLoader.getClass('walk_mc');
var walkAnim:WalkAnim = new WalkAnim(); // I have typed your var as WalkAnim, not MovieClip.
addChild(walkAnim);
walkAnim.head_mc.rotation += 5;
To answer your second question, you wont be able to edit the definition at runtime. You can add an item to each instance though:
var Hat:Class = SWFLoader.getClass('accessory_hat_mc');
var myHat:Hat = new Hat();
walkAnim.head_mc.addChild(myHat);