package{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
public class Main extends MovieClip{
public var ability1,ability3:Ability;
public function Main(){
var ability1 = new Ability(30,30,"Ability Name","...",5,false);
addChild(ability1);
var ability2 = new Ability(60,30,"Ability Name2","...",3,false);
addChild(ability2);
var ability3 = new Ability(45,60,"Ability Name3","...",5,true);
addChild(ability3);
stage.addEventListener(MouseEvent.CLICK, Check);
trace(ability1.Points); //outputs the value
}
public function Check(event:MouseEvent):void{
trace(ability1.Points); //outputs error
}
}}
第二个跟踪给出了这个错误:“TypeError:错误#1010:一个术语未定义且没有属性。在Main / Check()”你能指出我至少吗?
感谢。
答案 0 :(得分:1)
通过在var
方法中使用Main
语句,您可以将实例分配给仅在该方法范围内的局部变量。按如下方式更新您的代码,您应该得到您期望的结果:
package{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
public class Main extends MovieClip{
public var ability1:Ability;
public var ability3:Ability;
public function Main(){
// I'm a local property scoped only to this method
var ability2 = new Ability(60,30,"Ability Name2","...",3,false);
addChild(ability2);
// We're instance properties and can be accessed from any method
// in the class (and from outside the class as well)
ability1 = new Ability(30,30,"Ability Name","...",5,false);
addChild(ability1);
ability3 = new Ability(45,60,"Ability Name3","...",5,true);
addChild(ability3);
stage.addEventListener(MouseEvent.CLICK, Check);
trace(ability1.Points); //outputs the value
}
public function Check(event:MouseEvent):void{
trace(ability1.Points); //outputs the value
trace(ability3.Points); //outputs the other value
}
}
}