AS3 Removechild Addchild issue/error - actionscript-3

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.

Related

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 how to get name of instance using getChildByName

Okay so I have a MovieClip called sC and need to write a code where, if you click the button (sC) then sC will dissapear. The function needs to work for multiple buttons. What I tried was
sC.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
var self;
self = MovieClip(getChildByName(event.target.name));
self.visible=false;
Now when I try this code, it gives me an error when I click sC. It says "cannot access a property or method of a null object reference.". when I try to trace(self) it outputs "null". Is there a way where I can get the name of the instance of the object which is using the clicKHandler function and then make it's visibilty equal to false (visible=false)?
Note that when I trace(event.target.name) it says "instance127".
In your code, the variable self resolves to your movieClip's name, but not the complete path to where it exists. Try setting it up like below, where target is the button that was clicked:
sC.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void
{
event.target.visible = false;
}

Trying to understand a function

Im trying to understan a function that I found on the web.
Iknow what the function does, It get the information about the webcam in your computer and post it on the textArea,
But the individual line are just a bit confused.
Any help ?
Thanks
private var camera:Camera;
private function list_change(evt:ListEvent):void {
var tList:List = evt.currentTarget as List;
var cameraName:String = tList.selectedIndex.toString();
camera = Camera.getCamera(cameraName);
textArea.text = ObjectUtil.toString(camera);
}
private var camera:Camera;
This line creates a variable of the class type Camera. It does not create an instance of the variable.
private function list_change(evt:ListEvent):void {
This line is a standard function heading. Because the argument is a ListEvent, it makes me think that this function is probably written as an event handler. Because of the name of the function, it is most like listening to the change event on a list.
var tList:List = evt.currentTarget as List;
This line creates a reference to the list that dispatched the event, which caused this handler to be executed.
var cameraName:String = tList.selectedIndex.toString();
This line converts the selectedIndex to a string. It's a bit odd to convert an index to a string, as opposed to some value. But the reason they do that looks to be on the next line..
camera = Camera.getCamera(cameraName);
This uses that camera variable (defined back in line 1) and actually gets an instance of the camera. It uses the "cameraName" which makes me think that the list that dispatched this change event contains a list of cameras available on the system.
textArea.text = ObjectUtil.toString(camera);
This converts the camera object to a string and displays it in a text area. Normally you wouldn't try to do this as it provides no valuable data. A default object will display strings as [Object object] or something similar. Perhaps the camera object has a custom string function; I don't have experience with that. Normally, you'd want to access properties of the object to get useful information, not try this on the object itself.
}
This line is the end of the function. The open bracket was in the 2nd line of code in the function definition.

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.

ActionScript 3 name property is not returning the right name...?

I experienced a problem with the name property in as3, I created this "dot" movieclip and I exported to a class,
then I anonymously created a bunch of dots using a loop. I assigned numbers as name to each dots
private function callDots(num:Number):void
{
for (var i = 0; i < subImagesTotal[num]; i++)
{
var d:Dot = new Dot();
d.x = i*23;
d.y = 0;
d.name = i;
dotContainer.addChild(d]);
}
}
so far so good, I checked that if I trace the name here, I will get the number I want.
However, it's not giving me the numbers if I trace it in other functions.
I added all of my dots to "dotContainer", and if I click on one of the dots, it will call this function
private function callFullSub(e:MouseEvent):void
{
var full_loader:Loader = new Loader();
var temp:XMLList = subImages[sub];
var full_url = temp[e.target.name].#IMG;
full_loader.load(new URLRequest(full_url));
full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);
}
e.target.name is suppose to be numbers like 1 or 2, but it's giving me "instance66" "instance70" and I
have no idea why. Because I did the same thing with loaders before and it totally worked.
Any ideas? Thanks.
christine
The e.target returns the inner most object clicked on, this could be a TextField, another MovieClip or posibly a shape (I'm not 100% of the last one) inside the "Dot".
To prevent this you could try to set the mouseChildren property to false on the Dot's when you add them. This should insure that nothing inside the dots can dispatch the click event, and thus the Dot's should do it.
Perhaps you could also in the event handler verify the target type with code like this:
private function callFullSub(e:MouseEvent):void
{
if(!e.target is Dot)
throw new Error("target in callFullSub is not Dot but: " + e.target.toString());
//The rest of you code here
}
The answer is [e.currentTarget.name] I perform this all the time!
Should return "Dot1" "Dot2", etc.
If the value you wish to return is a number or other data type other than a string (name of object) use [e.currentTarget.name.substr(3,1).toString()]
Should return 1, 2, etc.
Navee
I tried to reproduce your problem first with Flex using runtime created movieClips and then with Flash using Dot movieClip symbols exported for ActionScript. Neither application exhibited the problem.
You may already know names like "instance66" "instance70" are default enumerated instance names. So, whatever is dispatching the MouseEvent is NOT the dot instance. Perhaps you are unintentionally assigning callFullSub to the wrong targets, maybe your containers? Try assigning it to dot instance right after you create them, like this:
private function callDots(num:Number):void
{
for (var i = 0; i < subImagesTotal[num]; i++)
{
var d:Dot = new Dot();
d.x = i*23;
d.y = 0;
d.name = i;
d.addEventListener(MouseEvent.CLICK, callFullSub);
dotContainer.addChild(d]);
}
}
Be sure to temporarily comment out your original assignment.
Try this might work,..
d.name = i.toString();
You have not shown enough of your code for me to be able to give you a DEFINATE answer, I will however say this.
//After you create each loader you need to set its mouseEnabled
//property to false if you do not want it to be the target of
//Mouse Events, which may be superseding the actual intended target;
var full_loader:Loader = new Loader();
full_loader.mouseEnabled = false;
//Also you could name the loaders and see if what comes back when you click is the same.
ALSO! Add this to your Mouse Event handler for CLICK or MOUSE_DOWN:
trace(e.target is Loader); //If traces true you have an answer
I believe that the mouse events are being dispatched by the Loaders.
please provide more of your code, the code where the Loader.contentLoaderInfo's COMPLETE handler fires. I assume this is where you adding the loaders to the display list as I cannot see that now.