AS3: A term is undefined when adding a shape inside a movieclip - actionscript-3

I have what seems to be a very simple issue. I need to create a shape and add it inside a movie clip that is inside of another movie clip.
The code I am currently using is as follows:
var enemy_beacon:Shape = new Shape();
fullmenu_mc.menu_map_mc.addChild(enemy_beacon);
fullmenu_mc.menu_map_mc.enemy_beacon.graphics.lineStyle(1, 0xFF0000, 1);
fullmenu_mc.menu_map_mc.enemy_beacon.graphics.beginFill(0xFFBB00,1);
fullmenu_mc.menu_map_mc.enemy_beacon.graphics.drawCircle(50, 50, 25);
fullmenu_mc.menu_map_mc.enemy_beacon.graphics.endFill();
However, this code throws an Error #1010: A term is undefined and has no properties.
It seems to create the shape fine, but adding the shape (via addChild) or accessing any of its properties makes everything go haywire.
I already checked the instance names of the movie clips, everything is spelled correctly and nested correctly.
Any thoughts?

Since you have enemy_bacon instance, you can access it directly:
var enemy_beacon:Shape = new Shape();
fullmenu_mc.menu_map_mc.addChild(enemy_beacon);
enemy_beacon.graphics.lineStyle(1, 0xFF0000, 1);
enemy_beacon.graphics.beginFill(0xFFBB00,1);
enemy_beacon.graphics.drawCircle(50, 50, 25);
enemy_beacon.graphics.endFill();

The problem is that you don't give a name to your Shape.
fullmenu_mc.menu_map_mc doesn't know that your variable named enemy_beacon is the same that you've added to it's children.
Targeting children like that means that you are using their instance names. So fullmenu_mc.menu_map_mc.enemy_beacon means that you are searching a child called enemy_beacon inside menu_map_mc. And with the first two lines, you've just added some child to that menu item, but haven't specified name.
Instance names are not the same as your variables. Check this out:
var myShape:Shape = new Shape();
myShape.name = 'otherShape';
this.addChild(myShape); // you add specific item, name doesn't matter
trace (this.getChildByName('otherShape') == myShape); // you get child by NAME
// and because the child is the same as you've added, this will output: TRUE

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 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.

AS3 MovieClip name ambiguity

Summary: I create instances of various MovieClips via AS3, using MovieClip class objects that are defined in the library.
As each MC is instantiated, I push it into an array for later reference.
Finally I create an XML file that contains data related to each MC, including its name. This is the problematic part – the name has to be able to identify the respective MC when the XML is read back in. I don’t want “instance17” etc, which I assume will be meaningless in another session.
Background: I am not a career OO programmer and this is a temporary assignment, forming only a very small part of my long-term interests. It will probably be a couple of years before my next Flash project.
Create instance
Library object
Type: MovieClip, linkage _brakepipe
Instantiation
var brakepipe: _brakepipe = new _brakepipe();
shapes.push(brakepipe);
Then later
var clip: MovieClip = shapes(i);
Trace (clip);
This yields
[object _breakpipe]
So it is giving me the class name, not the MC instance name. What property or method of MC would yield “breakpipe”? (Or even "_breakpipe" - without the "object" prefix?)
You can use an associative array. It could look like this:
var shapes:Array = new Array();
and then
shapes.push({item:_brakepipe,_name:"brakepipe"};
Essentially the curly brackets create an Object instance and the name before the colon (:) is the name you create that you want associated with the value after the colon.
so now you can do this in a loop
trace(shapes[i]._name+"\n"+shapes[i].item);
// output:
// brakepipe
// [object _brakepipe]
The nice thing about this method is you can extend it for any number of properties you want to associate with your array element, like this:
shapes.push({item:_brakepipe,_name:"brakepipe",urlLink:"http://www.sierra.com",_status:"used",_flagged:"false"};
and now
shapes[i]._status
would return the string "used". And you could change that value at runtime to "new" by doing
shapes[i]._status = "new";
The Instantiation / Then later / This yields... Seems to be unclear for me, but you may try this and change the code...
Because I'm not sure not sure about the instance name you want to store...
In your loop you may do this if clip is a MovieClip! :
var clip: MovieClip = shapes(i);
clip.name = "breakpipe_" + i
trace (clip.name);
// will output : breakpipe_1 - > breakpipe_n...
You may deal with the clip.name later by removing the extra "_number" if you want.
If i == 13
var clip: MovieClip = new MovieClip();
clip.name = "breakpipe_" + 13
trace(clip.name);
// output breakpipe_13
var pattern:RegExp = /_\d*/g;
trace(clip.name.replace(pattern,""));
//output :
//breakpipe
So here, you may push your Array or Vector with the instance name.
Am I wrong?

AS3 Multiple instance names in one MC

I apologize for how confusing this question is.
I have a Movie Clip that is a car. In the car movie clip there are four different angles to the car. (e.g. left, right, front back). I dynamically change the body color of the car. In each angle of the car, the body of the car has an instance name "body." I change the color with the code :
var tempcar = "car_mc" + i;
var myNewTransform = new ColorTransform();
myNewTransform.color = 0x000000 //in real life this is a random value
this[tempcar].body.transform.colorTransform = myNewTransform;
Everything works fine, until I tell the car movie clip to gotoAndPlay the frame "front," where we see the front side of the car, and I try and apply the color change again to the body of the front of the car. I get the error :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Is there a better way to do what I am trying to do?
That's the old ActionScript 2 way of handling things. In ActionScript the container is not always a MovieClip, which would except the hash to access a dynamic field. Also, if you'd added it to the display list via addChild, the result would be different as well, since it is not the case in ActionScript 3, that could address the child automatically.
You should use an Array to store and access dynamically created instances.
// clazz would be the symbol
function createInstance(container:DisplayObjectContainer, clazz:Class, list:Array):Sprite
{
const child:MovieClip = new clazz() as MovieClip;
if (!child) throw new ArgumentError("Wrong type given");
return list[list.length] = container.addChild(child);
}
function getInstanceAt(index:int, list:Array):Sprite
{
return list[index] as Sprite;
}

AS3 - nulling elements within a Vector structure

I'm using a Vector structure in ActionScript 3 to store references to a custom class. My Vector is fixed length for performance reasons, and also because it needs to map 1:1 to another data structure.
I'm trying to figure out how I "remove" an element from this fixed list. I don't want the length of the Vector to change, I just want the value of that element to be "null". Or more specifically, when I test for the truth of that element (eg: if (myVector[index]) { // do something... }), I want that test to fail for that element, since it's "empty".
So naturally, I've tried:
myVector[index] = null;
but that throws a TypeError, as does
myVector[index] = Vector.<MyClass>([]);
So what's the process for "emptying" an element of a Vector?
There must be something wrong with your index variable because
myVector[0] = null;
does not throw a TypeError when I try that. Is index an int or uint?
if you're receiving:
TypeError: Error #1009: Cannot access
a property or method of a null object
reference
than you probably haven't added your class to the object you are trying to manage (IE: you haven't added your Display objects to the stage that you are trying to nullify)
posting more code could help, but something like the following should work for you:
var spriteA:Sprite = new Sprite();
var spriteB:Sprite = new Sprite();
var spriteC:Sprite = new Sprite();
var spritesVector:Vector.<Sprite> = new <Sprite>[spriteA, spriteB, spriteC];
spritesVector.fixed = true;
trace(spritesVector); //[object Sprite],[object Sprite],[object Sprite]
spritesVector[1] = null;
trace(spritesVector); //[object Sprite],null,[object Sprite]