Referring to a movieclip from within another movieclip - actionscript-3

At a certain keyframe of a videoclip (called videos_mc), I want to change the visibility of another movieclip (called fundoMenu_mc). These two movie clips are nested in the same scene.
When I try:
videos_mc(parent).fundoMenu_mc.visible = false;
I get this error:
1180: Call to a possibly undefined method videos_mc.
When I try:
this.parent.fundoMenu_mc.visible = false;
I get this error:
1119: Access of possibly undefined property fundoMenu_mc through a reference with static type flash.display:DisplayObjectContainer.
How should I do it?

If both MovieClips are in the same scene and fundoMenu_mc within the videos_mc MovieClip, try this
videos_mc.fundoMenu_mc.visible = false;
If referring from an another Movieclip
MovieClip(parent).videos_mc.fundoMenu_mc.visible

You should try this
MovieClip(parent).fundoMenu_mc.visible = false;

Related

AS3 Problems with MovieClips manipulation

I'm trying to set a dynamic text called text_amount inside a movieclip called PiattoBalance in the stage from another movieclip called Bet1 in the same stage but i'm getting error 1120: undefined property PiattoBalance.
here is the code inside the Bet1 MovieClip:
stop();
visible = false;
MovieClip(root).PiattoBalance.text_amount.text = String(int(PiattoBalance.text_amount.text) + int(text_bet.text));
What's wrong?
MovieClip(root)
Why would you use "root", please don't use "root" use stage, or the container which contains your movieclip. "Rooting" died in AS2.0, bad habit to keep it.
You could check whether the PiattoBalance object exists or not.
if(MovieClip(root).PiattoBalance != null)
MovieClip(root).PiattoBalance.text_amount.text = String(int(PiattoBalance.text_amount.text) + int(text_bet.text));
If exists, then check the "PiattoBalance.text_amount".

This, stage & parent are null

Im trying to access the stage from an external class this is what I have:
Player.as (Main Class)
import flash.display.MovieClip;
public class Player extends MovieClip
{
private var _controls:Controls;
public function Player()
{
// constructor code
_controls = new Controls();
}
}
Controls.as
import flash.display.MovieClip;
public class Controls extends MovieClip
{
private var _playbtn:MovieClip;
public function Controls()
{
trace(this.getChildByName("playbtn"));
}
}
but this line trace(this.getChildByName("playbtn")); always errors I have even tried:
trace(stage.getChildByName("playbtn"));
trace(parent.getChildByName("playbtn"));
But I get the same error:
Null for this and
Cannot access a property or method of a null object reference. for
stage & parent
Is there something I am doing wrong?
I think you are mistaken as to what getChildByName actually does. It returns reference to an object with its name property set to the supplied value. It does not return a reference to an object set to a variable with the supplied name.
For a reference to be returned by getChildByName, you must instantiate an object, set its name to something, and then call addChild on a DisplayObjectContainer with that object. Then you can call getChildByName on the DisplayObjectContainer.
For future reference, stage will be null until the object is added to the stage. parent will be null until the object is used in an addChild call
Display objects get access to their 'stage' property when they are added to the stage. You haven't added the _controls to the stage ( eg addChild(_controls) ) when you try to access the stage property.
Add an event listener (Event.ADDED_TO_STAGE) to the constructor of your Controls class that points to a handler that then checks the stage property.
If you are adding everything to the stage manually, there is no need to create a new instance of Controls in the constructor of Player, you should just retrieve the reference:
_controls = this.controls; //where 'controls' is the instance name you assigned in Flash IDE.
Also, where is the 'playbtn' instace you are trying to retrieve? Is it on the stage or is inside of the Controls instance? As you are tracing this.getChildByName("playbtn"); it HAS TO be inside of Controls with the instance name playbtn assigned in the IDE (or via .name property).
You are getting indeed a null for parent and stage because you have not added the instance to the stage. What you are doing right now:
You instantiate Player, the player then instantiates the Controls (it does not retrieve the reference you have on stage, because you are calling it with the new keyword!)
The new controls have never been added to anything, therefore they have no parent and neither stage.

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

AS3 addEventListener to movieclip inside another movieclip

So, I have a movieclip named "LoginScreen" with a instance inside called "confirmbutton".
I want to add the LoginScreen to stage and set a event listener to the button inside it, but I keep getting an error.
This is my code:
var LoginScreen:loginscreen = new loginscreen;
LoginScreen.x = stage.stageWidth / 2;
LoginScreen.y = stage.stageHeight / 2;
addChild(LoginScreen);
LoginScreen.confirmbutton.addEventListener(MouseEvent.CLICK, test);
function test(e:MouseEvent):void{
trace("Sup?");
}
I get the error:
Symbol 'LoginScreen' 1046: Type was not found or was not a compile-time constant: confirmbutton.
I'm pretty sure it exists and its named properly (has that exact name without caps), so I'm guessing its probably a scope problem.
Try using a helper gettter function for getting the inner MovieClip. So where you defined the LoginScreen class put a simple getter function like this one:
public function get ConfirmButton():MovieClip { return this.getChildByName("confirmButton") as MovieClip; }
Then you can access that MovieClip from the LoginScreen object like this:
LoginScreen.ConfirmButton.addEventListener(MouseEvent.CLICK, test);
Figured it out. Renamed the button to "confirmButton" (capital "B") and started working. I guess the problem was that the "confirmbutton" was also the AS Linkage of the movieclip.

AS3 Display Object Trouble

I'm making a game in AS3. When I add an enemy to the game screen, later on I have to remove it when it dies. But I keep getting this:
[Fault] exception, information=ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
But I clearly add the enemy to the gamescreen. Could this be from passing the enemy through a bunch of functions or something?
This means that you try to remove the MovieClip (or Sprite or so) from a DisplayObjectContainer that is not its parent.
You have to be sure to call the removeChild() Method on the right DisplayObjectContainer.
For instance:
var myChild:MovieClip = new MovieClip();
var holder:MovieClip = new MovieClip();
holder.addChild(myChild);
so when you want to remove the child you have to call the removeChild Method on the holder.
holder.removeChild(myChild);
If you call removeChild() on for instance the stage you will get an error because the stage does not hold myChild as a child of itself.
So double check if you call removeChild on the right container.
PS: Sample code is always easier to debug
When dealing with the timeline, it's difficult sometimes to keep track of an object's scope , in which case you can always call the method from the object's parent property.
child.parent.removeChild( child );
if you're coding in FlashDevelop & for some reason , don't wish to or can't keep track of the parent , you could implement a couple of methods to add and remove your object from the display list, practically delegating adding & removing to the object...
in your object class , you could do the following...
private var container:DisplayObjectContainer;
public function addToDisplayList( container:DisplayObjectContainer ):void
{
this.container = container;
container.addChild( this );
}
public function remove():void
{
if( container != null )
container.removeChild( this );
}
Then you can simply do this:
var child:MovieClip = new MyObject();
child.addToDisplayList( whatever );
//later...
child.remove();