AS3 Problems with MovieClips manipulation - actionscript-3

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

Related

var mC:mc = new mc is adding the mc immediately?

I am a baffled noob here. I have the following code:
var mC:mc = new mc();
I do NOT instantiate mC at all with an addChild(mC);
But, later in the code, I have a loop using onEnterFrame and in this loop I have the following trace function:
if(mC){
trace("mC is here");
}
This returns "mC is here" in the output window. HUH???
The problem is that I want to use this 'if' statement to removeChild(mC); [I will be adding it in the code later with an addChild(mC); based on certain stuff that happens] but it keeps throwing dang "ERROR child of the caller" messages even with the 'if' condition...
WHAT am I doing wrong? I did not know declaring variables would add them to the stage/display list, I thought you needed an addChild(); statement. Am I smoking something I shouldn't be?
Thanks in advance, ~Frustrated Inc.
When you new up an object it exists in memory, even if you have not added it to the stage. That is why when you check if mC exists, it returns true. You want to check if it exists on the stage. Something like:
var mc:MovieClip = new MovieClip();
mc.name = "test";
if (this.getChildByName("test") != null) {
trace("mc is on stage");
}
I have not used Flash for a long time, so I did not test this code. Hopefully it works.
Complex objects in AS3 (that means anything that is not a string or a number) have a default value of null. WHen evaluated that default value of null equals false:
var mymc:MovieClip;//that MC is NOT instantiated yet so it has a default value of null
if(mymc)
{
//mymc is null so this evaluates to false
//and this statement DOES NOT execute
Now when a complex object is instantiated and exist its value would now evaluates to true
var mymc:MovieClip = new MovieClip();//that MC IS instantiated
if(mymc)
{
//mymc exits so this evaluates to true and this statement EXECUTE
//notice that "!= null" is not necessary
Now your problem has to do with display list. A DisplayObject has a parent property that is null when that object is not added to a display list, and that property point to the parent when that object is added to a display list:
var mc:MovieClip = new MovieClip()
trace(mc.parent);//this is null
addChild(mc);
trace(mc.parent);//this is not null anymore and points to the parent
SO what you mean to do is:
if(mC.parent){//this means mC has a parent and can be removed from it
trace("mC is here");
}
In your code, you just control whether your variable is null or not.
You can use contains method on the display object you are trying to add to.
If you are adding mC to some sprite named container, you can simply check whether it exists in that container with:
if (!container.contains(mC))
container.addChild(mC);
Edit: The safer method to control whether a movieclip is on the stage is to control its stage value.
if (mC.stage) {
mC.parent.removeChild(mC); // this is how you remove, if you simply want to check existence, don't remove it
}
It has to have a stage value if you added the movieclip to the stage or a container that is added to stage.
Hope it is clearer this way.

Referring to a movieclip from within another movieclip

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;

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

actionscript-3: check if movieClip exists

I have a movieclip created with the following code:
var thumbContainer:MovieClip = new MovieClip();
thumbContainer.name = "thumbContainer";
stage.addChild (thumbContainer);
If the window gets larger/smaller I want everything back in place. So I have an stage Event Listener. Now I want to see if this mc exists to put back in place. I've tried different ways but keep getting an error that does not exist.
1120: Access of undefined property thumbContainer.
if (this.getChildByName("thumbContainer") != null) {
trace("exists")
}
and
if ("thumbContainer" in this) {
trace("exists")
}
or
function hasClipInIt (mc: MovieClip):Boolean {
return mc != null && contains(mc);
}
stage.addChild (thumbContainer);
//...
if (this.getChildByName("thumbContainer") != null)
You are adding the thumbContainer to stage and checking for its existence with this. Change stage to this or this to stage.
That said, an even more appropriate way is to keep a reference to the added movie clip and check for existence with the contains method. It determines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself. The search includes the entire display list including this DisplayObjectContainer instance, grandchildren, great-grandchildren, and so on.
Hence you can easily check using stage.contains(thumbContainer);
if you are having trouble firing errors, you can always resort to a try catch
try{
/// do something that will blow up...
}catch( e:Error ){
trace( "we had an error but its not fatal now..." );
}
the problem was that 'stage' and 'this' are not the same...that's why I couldn't talk to the mc.
this works:
var thumbContainer:MovieClip = new MovieClip();
thumbContainer.name = "thumbContainer";
addChild (thumbContainer);
if (getChildByName("thumbContainer") != null) {
trace("exists")
}