ActionScript 3.0 :: Changing Parent MC With RemoveChild() And AddChild()
Sep 6, 2011
I have a ball MC that is a child of a ship MC, and when the ship fires the ball I need to make the ball MC a child of the stage instead of the ship. Here's the code I have (contained in ball.as, this function is called when the ball is fired)
public function prep():void {
ship.removeChild(this);
screen.addChild(this);
somewhat understood the basics of adding and removing children, however, I am facing another problem. After using the removeChild Function, I can't seem to add another child.For example:
ActionScript Code: var attachedMC = new (getDefinitionByName("Content1") as Class)() Page.addChild(attachedMC);
The newMC doesn't show up.If I add 'parent.' in front of the addChild then it appears.mc_LibItem has a base class of "flash.display.MovieClip" and is exporting for ActionScript and on frame one.For grins I created a new .fla and began messing around with the same addChild idea and it worked.
I'm currently making a platform game and when the player shoots i want to add the bullets to the stage. The players gun class is in charge of adding bullets. The "players gun" is a child of "players gun holder" which is a child of "player" which is a child of the stage. is there a better or more dynamic way for the "players gun" to add bullets to the stage rather than using parent.parent.parent.addChild (bullet);
I got a Parent Sprite, let's name it SpriteP, which holds inside it two other Sprites. Sprite1 and Sprite2.[code]Sprite2 is a rectangle. When I rotate it, of course, the height of its parent, SpriteP, GROWS. But I would expect that height to go back to what it should be when I'm removing Sprite2!And it does! If I remove Sprite2 while NOT being rotated, the height of the parent drops back to normal, 200.
I got a Parent Sprite, let's name it SpriteP, which holds inside it two other Sprites. Sprite1 and Sprite2.
When Sprite2 is NOT rotated:
Code: trace(SpriteP.width + ' ' + SpriteP.height); //100 250. SpriteP.removeChild(Sprite2) trace(SpriteP.width + ' ' + SpriteP.height); //100 200. -> works, 200 is good. When Sprite2 is rotated at 90:
[Code].....
And it does! If I remove Sprite2 while NOT being rotated, the height of the parent drops back to normal, 200. But if I rotate Sprite2, the Parent won't update its bounds.
For example, I have a function that is triggered by a button click This function loads an external swf.I then set a global variable swfLoaded to true, with the idea of having an if condition that executes removeChild(loader) if swfLoaded is true.When trying to execute removeChild(loader), I get an error telling me that it must be the child of the caller.I've seen the code below, which loads a default swf, but I don't want a default swf - I just want one loaded if the button is clicked, and then if another button is clicked, the original one to be removed before the next swf is loaded.How do I make this code work without loading a default swf, just loading one once someone clicks a button, and then removing it?
var Xpos:Number = 110;var Ypos:Number = 180;var swf:MovieClip;var loader:Loader = new Loader(); var defaultSWF:URLRequest = new URLRequest("swfs/eyesClosed.swf");
I need help with addChild and removeChild.I want to make a two buttons one addChild and other removeChild . Friend helped me a little bit, but I didnt uderstand the script ;]Here is the example script which my friend gave to me. In other words I want to make a button that attached hair and the other that removes.
ActionScript Code:public function changeHairs (newHair: Hair): Hair { Face.removeChild (oldHairs);[code].....
I have a MC that the mouse can drag and slide, when it gets removed from the stage when it hits an out of bounds object. I re add it to the stage at the x,y position it originally started from but when I re add it it continues to slide in the direction it was going before it was removed.
I'm making an application for the iPad that allows me to swipe movieClips that are 1024 x 768.The application starts with movieClip1, movieClip2 and movieClip3 added to the stage.When you swipe to the left... they are to move to the left. When a movieClip is off the stage to the left... I want to removeChild and then addChild the next one in the queue to the right of the visible clip. This will provide a seamless swiping experience.
I first noticed it when this function started throwing a 1009 error:
[Code]...
were throwing the 1009 error. Which is wierd on two accounts. First, if I remember right, error 1009 comes about when referering to an object that is not on the stage. Which makes NO sence when getting it from an addChild! Second, this error only starts to occur the second time I use the function.
I am putting together my file right now which is now using visual elements purely added and removed via AS3 (addChild / removeChild) methods.My question is just about the proper time / place to declare their variables.Can I declare them all at the start of the movie (frame 1) and just call them when I need them, or is it better to declare them on the frames that they are introduced on? I guess the question is really, will I ever lose those variables throughout the movie once they are declared?
I have images loading into a single mc container every time i click a button. The images are being loaded (from xml data in a data grid) on top of each other every time the button is clicked. This is the latest Ive been working with and doesnt seem to work
Loader();var reqImage:URLRequest=new URLRequest(data_grid.selectedItem.JPG); imageLoader.load(reqImage); if (imageload.numChildren>0) { imageload.removeChild(imageLoader); imageload.addChild(imageLoader);
as you guys can see it addChild with dynamics and its not hardcoded. difference between adding a movieclip from the library and loading a swf external?
because i cant get this to work. Even tho my code does addChild and removeChild i get error-messages in the output. The movie works fine but it isin't a pretty code
I want to make a two buttons one addChild and other removeChild. Here is the example script which my friend gave to me. In other words I want to make a button that attached hair and the other that removes. Should I make a separete custom class for Hair?
I am doing a project in which I need to open and close MovieClips. I have my Scene1 which loads the first MC (named "page1") then page1 plays and I would like it to unload itself and load the MC named "Page2" and so on up to page 10 ...
On the last frame of the MC "page1" I have this code: stop(); stage.addEventListener(MouseEvent.CLICK, animate2); function animate2 (event:MouseEvent):void { stage.removeChild(stage.getChildByName("clip1")); var clip2:MovieClip = new page2(); addChild (clip2); }
But all I get is this error message: TypeError: Error #2007: Parameter child must be non-null. at flash.display:isplayObjectContainer/removeChild()
I would like to know the internals of that method, in other words what exactly happens when I call that method. If someone can explian it with a list 1,2,3... would be great. including events..
I'm in the right place for this plea from yet another novice trying to learn AS3 in Flash with a couple of very good books but no one to talk to about it - and in danger of losing hair in the process.
My problem: I have a web site containing 8 pages. Each page is a separate movie clip, and each is in a Labeled frame. My intention is to have an overlay for each of the buttons in the top left panel of the home page. On the positive side I have managed to achieve the dynamic placement of an instance of a library movie clip called "sampleAR" via addChild to overlay on top of my home page. But I can't for the life of me get removeChild to rid me of the wretched thing again. My home page is in frame 1 of the movie labeled "Home". This contains a movie clip with the instance name "HomePage" on the stage. Inside the "HomePage" movie clip is my script sitting at the end where the movie stops in frame 44. The code sits here because this is the only place I've managed to get any kind of result with the following code:
Here's some code I created as an answer to another question. It pulls a library item out of the library and puts it on stage programmatically. No big deal, except that in order to get the image displayed on the screen to actually update, I had to removeChild() and then addChild() it back. Is there another way to force the screen to redraw?
[Code]....
Note that in this example, this code is on frame 1 of the timeline. It _feels_ like imageClip is not a direct reference to the thing that's actually on the display list. However, when I modified my code to replace the reference to the AS variable imageClip with a more direct reference to the element on the stage (imageClip = getChildAt(1)), that made no difference either.
It pulls a library item out of the library and puts it on stage programmatically. No big deal, except that in order to get the image displayed on the screen to actually update, I had to removeChild() and then addChild() it back. Is there another way to force the screen to redraw?
I'm trying to make a basic mouesover and mouseout that will display a help/key box on the screen. I created the help box that will be displayed as a symbol called helpbox. I updated the linkage to export to Actionscript and called the class Helpme. The object I'm using to instantiate the Helpme class is a small graphic called helpcoast. I want the "helpbox" in the new class "Helpme" to be displayed only when the mouse is over the helpcoast graphic. When I test the movie and mouse over and mouse out of the graphic once, I get no errors, but if I mouse over it, then off it, then back over again, I get this error in the output box:
I can't seem to use removeChild. Basically this is what happens.From the Document class, or another movieClip class, I can addChild (movieClip) fine but when I go to remove them I get ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. even if I go: myMov.parent.removeChild(myMov) or (getChildByName(MyMov)) or anything it always either gives the above error or in the case of using .parent: Cannot access a property or method of a null object reference.
Also using trace() to find out what the parent is it always says [ object DocClassName] and the docClass seems to return the right amount of child MovieClips. Heres my code snippet:[code]....
Do I need to create some sort of Stage for all child MovieClips to belong to?
I have recently experienced problems with attempt to remove DisplayObjects that were placed at design time.I have some class "Locator" inherited from Sprite. Several instances of it are placed at the stage at design time.Locator constructor fills parent variables and tries to remove the instance itself from parent:
ActionScript Code: public function Locator() { // give parent some important data
[code]....
All instances now process constructor, but only one of them processes event handler.
I am trying to remove a movieclip from my main timeline but it is not working. you click a button that is in a movieclip that is dynamically added to the stage and it is supposed to remove a movieclip from the main stage. I added a trace to see if it was grabbing the right movieclip and it is. The error message that I am getting is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
There is the stage, the level object then the player object in the level. The player object has a sub-object that is a weapon. When I fire the weapon a bullet is to be placed on the level stage. At the moment I have this code in the weapon class when the weapon is fired.
[Code]....
(In case it helps you know my skill level, I'm currently reading Foundation Game Design with Flash by Rex van dur Spuy and am a PHP web developer, also played around with Flex - ActionScript is still new to me)
the final coordinate of the child would be x=200, y=200. But, is there anyway that that i cant addChild with only respect to the coordinate of iamchild?
I am trying to get a circle shape and a text-field sit inside a square sprite, using addChild() method. Both the children seem not to agree sitting in the lap of their parent; and are sitting at a distance, instead. If I create a square symbol on the stage and run the application, everything is fine.