How to run MovieClip ( who is an external swf ) function from flex - actionscript-3

I try to call swf function ( setPoints(nr:int):void; ) from flex.
[Embed(source="img/anim/x2.swf")]
[Bindable]
public static var points:Class;
public function strikeAnimation(area:SpriteVisualElement, strike:int):void{
var mc:MovieClip = new points() as MovieClip;
area.addChild(mc);
//how to run?
area.mc.setpoints(strike);
}
Any ideas?

I tried call function like this -
area.getChildIndex(area.getChildIndex(mc)).setpoints(strike);
The reason you received an error with the line of code above is that getChildIndex expects a DisplayObject and returns an Integer.
So, this probably worked:
area.getChildIndex(mc)
But, it returned an integer which you can't use as the input to another getChildIndex call because an integer is not the same as a child.
Something like this should get you an instance of your child:
var myChild :MovieClip = area.getChildAt(area.getChildIndex(mc))
However, the setpoints method is not a documented method of a movieclip; so to execute the setpoints method on your custom movie clip you're going to have to convert it to your custom type. Conceptually something like this:
(myChild as myCustomType).setpoints(strike);

Related

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

As3 Adding MC from Libary and accesing content inside loaded MC

I have a movieClip I am loading from the Libary and I have properly LINKED it to export with a name of myMC. This movieclip contains another movieClip and some properties. Lets call the movieClip inside: insideMC.
Here is my code:
function loadScreen()
{
var newMC:MovieClip = new myMC();
addChild(newMC);
loadButtons();
}
function loadButtons()
{
newMC.insideMC.addEventListener(MouseEvent.CLICK, homeButtons);
}
loadScreen();
HOWEVER, when I call the function loadButtons() within the loadScreen() function then I get this error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at iRosary_fla::MainTimeline/loadButtons()[iRosary_fla.MainTimeline::frame1:83]
at iRosary_fla::MainTimeline/loadScreen()[iRosary_fla.MainTimeline::frame1:110]
at iRosary_fla::MainTimeline/frame1()[iRosary_fla.MainTimeline::frame1:103]
It is not seeing the insideMC. Perhaps because it's calling to fast or not loaded yet. It is calling and loading the newMC tho. Just the function loadButtons() is not working because it is not seeing the insideMC movieClip. I am sure this is an easy fix but I can't find it anywhere. Thanks
newMC is a local variable in your loadScreen() method, therefore it has no scope in your loadButtons() method.
Declare newMC as a class member variable and it will have scope in loadButtons()
for example :
// in class declarations
public var newMC:MovieClip;
function loadScreen()
{
newMC = new myMC();
addChild(newMC);
loadButtons();
}
It's important to understand that :
var newMC:MovieClip = new myMC();
Creates a local variable. From your comments, it sounds like you did have newMC as a class variable. So you assumed that the above line was assigning the new instance to your class member newMC, and not the local variable you created.
Not completely sure this is your problem. But to access a movie clip within a movie clip you have to give that "insideMC" an instance name within the first movie clip. Otherwise you'll reference an object that you haven't added to the stage - a null object.
Tutorial on instance names here

AS2 to AS3 migration create empty movie clip with dynamic name

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

AS3 Removechild Addchild issue/error

I have been looking for an answer for hours:
My program:
Step I) When I click on a button, it displays a bitmap through addchild;
Step II) When I click on another button, it should remove the bitmap through removechild;
Step I) works perfectly but step II) doesn't work.
You'll find below some parts of my code:
First, I declare:
public var ajoutcarte4:MovieClip;
Secondly, In the main function I wrote:
var ajoutcarte4:Bitmap = new Bitmap();
Then, in a sub function triggered by the first button, I add the Bitmap to the stage (fl_bitmap is a function returning a Bitmap item):
ajoutcarte4 = fl_bitmap(couleur4+figure4);
ajoutcarte4.x=445;
ajoutcarte4.y=370;
addChild(ajoutcarte4);
So far so good but when I want to remove the child through another sub function triggered by a second button:
removeChild(ajoutcarte4);
It doesn't work because ajoutecarte4 is apparently null... Error 2007 when i get red of my condition...
change this
public var ajoutcarte4:MovieClip;
to
public var ajoutcarte4:Bitmap;
Then take out this line completely
var ajoutcarte4:Bitmap = new Bitmap();
And lastely
// add this like with this code
ajoutcarte4 = new bitMap()
ajoutcarte4 = fl_bitmap(couleur4+figure4);
ajoutcarte4.x=445;
ajoutcarte4.y=370;
addChild(ajoutcarte4);
You've declared a field ajoutcarte4 of type MovieClip, but then in your function, you declare a local variable ajoutcarte4 of type Bitmap, which is then added to the stage.
In the second function, you try to remove the field MovieClip, which has never been instantiated - and thus produces the error.
Change your declaration to this:
public var ajoutcarte4:Bitmap;
and call:
ajoutcarte4 = new Bitmap();
(without the var). Then it all should work correctly.

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.