Reputation: 127
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
}
}}
Second trace gives this error:"TypeError: Error #1010: A term is undefined and has no properties. at Main/Check()" Can you point me out at least?
Thanks.
Upvotes: 0
Views: 118
Reputation: 12431
By using the var
statement in your Main
method, you're assigning the instances to local variables which are only scoped within that method. Update your code as follows and you should get the results you expect:
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
}
}
}
Upvotes: 1