问题
Ok, this is my problem: i have a variable in a class named Mirror
public var line:Sprite = new Sprite();
i want to use that variable in another class named MovieClip. I imported the class in the MovieClip class:
public class MovieClips extends MovieClip
{
import Mirror;
public function MovieClips(radius:int, _x:int, _y:int,size:int,span:int,addl:Array)
{....
but when i try to do something like this:
if (Mirror.line.hitTestObject(ball) == true) {
speedY *= -1;
}
It shows up like an error: Acess of a possibly undefined property line through a reference with static type Class. I'm not sure what did i do wrong? Am i importing my class in the wrong way?
回答1:
You should pass your Mirror
object to your MovieClips
object like in the following example:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var mirror:Mirror = new Mirror();
var movieClips:MovieClips = new MovieClips(mirror);
}// end function
}// end class
}// end package
import flash.display.MovieClip;
class Mirror extends MovieClip {
public var line:MovieClip;
public function Mirror():void {
line = new MovieClip();
}// end function
}// end class
class MovieClips extends MovieClip {
public function MovieClip(mirror:Mirror):void {
mirror.line;
}// end function
}// end class
Also you could improve on this by making Mirror.line
read only:
class Mirror extends MovieClip {
private var _line:MovieClip;
public function get line():MovieClip {
return _line;
}// end function
public function Mirror():void {
_line = new MovieClip();
}// end function
}// end class
回答2:
If you have a class, like Mirror
and do something like Mirror.line
, that means you're trying to access a static class variable named line. Static variables/method are tied to the class, and not an instance of that class.
You haven't defined line as a static variable and thus you get the error.
I'm not sure you want it to be a static variable though and most likely you are just confusing the Mirror
class for a instance of the Mirror
class.
Somewhere you are likely creating an instance of your Mirror class:var myMirror:Mirror = new Mirror()
or dragging an instance on a timeline if using FlashPro (and then could give it an instance of 'myMirror' as an example).
You would access line
on the instance of a Mirror class. so with the above it would be myMirror.line
.
Importing a class does not give you access to any instances of that class, so if you want to be able to access an instance of Mirror
from within your MoveClips
class, you'd need to pass in a reference to it, or use the parent
keyword to get to the right scope (so if your Mirror instance and your MovieClips instance are on the same timeline/parent, you could do Mirror(MovieClip(parent).myMirror).line
- assuming your parent is of type MovieClip).
If you did want it as a static variable, just add the static access modifier to it's definition.
public static var line:Sprite
Keep in mind though that static variables are always in memory regardless of if you have any instances of them made.
来源:https://stackoverflow.com/questions/13326312/as3-importing-classes-sharing-variables-between-classes