View Full Version : how to access movieclips from external classes
nico
July 31st, 2008, 10:43 AM
Hello, just want to ask on how to access movieclip instance name from external classes (not using the document class).
I access my movieclips using ADDED_TO_STAGE event, but i want to access it directly, i tried stage.getChildByName("instancename").alpha = 0; but still its not working, what's the ideal way of accessing movieclips instances from external classes?
Felixz
July 31st, 2008, 01:04 PM
not stage but root
saxx
July 31st, 2008, 01:19 PM
var refname:class = new class();
addChild(refname)
refname.x = 200
refname.y = 200
instead of ADDED_TO_STAGE just go stage.addChild
You shouldn't have any problems just remember that its a child of the stage from then on
class is your class you've provided from linkage in the library .x and .y are just an example of how to use it like your regularly would
Felixz
July 31st, 2008, 01:33 PM
He meant Inside Looking Out: he listens to Event.ADDED_TO_STAGE to have stage and root properties accessible.
root.getChildByName("instancename").alpha = 0;
saxx
July 31st, 2008, 02:48 PM
ah, my mistake sorry about that
nico
July 31st, 2008, 09:09 PM
root.getChildByName throws me an error:( I tried to place root.getChildByName("instacnename").alpha=0 into my constructor but it throws me an error:(
dail
August 1st, 2008, 01:53 AM
try
MovieClip(root).getChildByName("blah").alpha = 0
nico
August 1st, 2008, 04:43 AM
still its giving me errors:(
dail
August 1st, 2008, 05:02 AM
have you imported the MovieClip package?
Felixz
August 1st, 2008, 11:02 AM
The problem is that you can't use parent in constructor
use Event.ADDED_TO_STAGE
helios02
August 1st, 2008, 11:21 AM
Couldn't you make a static var in your document class and then reference from whatever external class? So in your doucument class do
public static var instance:DocClass
in its constructor do
instance = this;
Then in whatever external class do
DocClass.instance.getChildByName("instance name")
senocular
August 1st, 2008, 11:34 AM
Your error message and your class code would help
nico
August 1st, 2008, 01:41 PM
i already used added to stage event, for me it feels very limited, because for example youll be doing a function that will make a certain button dissapear at a click of a button, if that method is called into added to stage event, it will automatically fires. what i really want is to call a mc in the stage without using added to stage event (MovieClip(parent).instancename). is there any other way to access a mc on the stage withouth using added to stage event?
dail
August 1st, 2008, 06:55 PM
Have a read of this article
http://developer.yahoo.com/flash/articles/display-list.html
And yeah, I'm with Senocular, its hard to give you advice without seeing how you have structured everything.
mieff
August 1st, 2008, 08:46 PM
try creating a global variable in your external class and dump the instance name of your mc into the variable.
External Class:
external class ExternalClass
{
public static var externalVarName;
}
In your movie:
import ExternalClass;
ExternalClass.externalVarName = yourInstanceName;
Something like this. Then you can access and change the attributes of the mc from anywhere you desire.
nico
August 2nd, 2008, 12:39 PM
its just simple. when i use the document class it works
DOCUMENT CLASS:
package
{
import flash.display.MovieClip;
public class Sample extends MovieClip
{
public function Sample():void
{
box.x = 500;
}
}
}
when i use the Event ADDED_TO_STAGE it works by instantiating my class on the main timeline and adding it into the display list:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Sample extends MovieClip
{
public function Sample():void
{
addEventListener(Event.ADDED_TO_STAGE, added);
}
public function added(e:Event):void
{
MovieClip(parent).box.x = 500;
}
}
}
code on my timeline:
var mySample:Sample = new Sample();
addChild(mySample);
the problem here is if i created a method, it fires instantly because of added to stage event.
Its really frustrating heheh, i only want to access my movieclips in the stage just like im using the document class.
mieff
August 2nd, 2008, 03:23 PM
i could be wrong here but it seems like your problem stems from using the ADDED_TO_STAGE event listener.
Felixz
August 2nd, 2008, 03:35 PM
package {
import flash.display.MovieClip;
public class Sample extends MovieClip {
public function Sample():void {
}
public function moveBOX(value:Number):void {
root.box.x += value;
}
}
}
var mySample:Sample = new Sample();
addChild(mySample);
mySample.moveBOX(500);Is that what You mean?
The code which you have in constructor would fire instantly in AS2 anyways
mieff
August 2nd, 2008, 03:42 PM
i don't know if this is what you're looking for but check out this little example of what i was talking about and maybe it will spark a solution for what you're looking for.
47390
47391
lolomedia
August 6th, 2008, 01:46 PM
that makes sense. how about if i want to linkage the movie clip from the library of course and choose export for actionscript so i will have a class just for this movie clip? how do i go by doing that?
craig.clayton
August 6th, 2008, 06:37 PM
at the top of the class
import mcMovieClip;
Then create a variable
private var myMovie:mcMovieClip;
private function addMyMovie():void {
myMovie = mcMovieClip;
addChild(myMovie);
}
put addMyMovie(); inside the constructor or init function if you have one
that makes sense. how about if i want to linkage the movie clip from the library of course and choose export for actionscript so i will have a class just for this movie clip? how do i go by doing that?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.