hannahd
06-08-2005, 11:39 AM
I have made a movie clip attached to a class called GuessingGame.
My problem is with these lines:
this.guess_input_txt.onKillFocus = this.checkGuess;
this.guess_input_txt.onSetFocus = function() {
this.error_sign_mc._visible = false;
trace(this.max) // says it's undefined
};
}
When I try to reference variables in the class inside functions on the event handlers, it says the variables are undefined, but if I retrieve them within the constructor, it works. What am I doing wrong?
Thanks
Hannah
import numberOutofRangeException;
class GuessingGame {
private var min:Number;
private var max:Number;
private var randnum;
private var error_sign_mc:MovieClip;
private var message_txt:TextField;
private var between_txt:TextField;
private var guess_input_txt:TextField;
public function GuessingGame() {
this.min = 1;
this.max = 100;
this.randnum = Math.round(Math.random() * max) + min;
this.error_sign_mc._visible = false;
this.message_txt.autoSize = true;
this.message_txt.text = "";
this.guess_input_txt.text = "";
this.between_txt.text = "between " + this.min + " and " + this.max;
this.guess_input_txt.onKillFocus = this.checkGuess;
this.guess_input_txt.onSetFocus = function() {
this.error_sign_mc._visible = false;
};
}
private function checkGuess() {
var errors = 0;
try {
if(isNaN(this.guess_input_txt.text)) {
this.guess_input_txt.text = "";
errors++;
throw "you must give a number";
}
}
catch(e:String) {
this.error_sign_mc._visible = true;
this.message_txt.text = e;
}
try {
var num = Number(this.guess_input_txt.text);
if((num < this.min) || (num > this.max)) {
this.guess_input_txt.text = "";
errors++;
throw new numberOutofRangeException(this.min, this.max);
}
}
catch(ex:numberOutofRangeException) {
this.error_sign_mc._visible = true;
this.message_txt.text = ex.getMessage();
}
if(errors==0) {
if(num > this.randnum) {
this.message_txt.text = "Too high!";
}
if(num < this.randnum) {
this.message_txt.text = "Too low!";
}
if(num==this.randnum) {
this.message_txt.text = "You've got it!!!";
}
}
}
}
My problem is with these lines:
this.guess_input_txt.onKillFocus = this.checkGuess;
this.guess_input_txt.onSetFocus = function() {
this.error_sign_mc._visible = false;
trace(this.max) // says it's undefined
};
}
When I try to reference variables in the class inside functions on the event handlers, it says the variables are undefined, but if I retrieve them within the constructor, it works. What am I doing wrong?
Thanks
Hannah
import numberOutofRangeException;
class GuessingGame {
private var min:Number;
private var max:Number;
private var randnum;
private var error_sign_mc:MovieClip;
private var message_txt:TextField;
private var between_txt:TextField;
private var guess_input_txt:TextField;
public function GuessingGame() {
this.min = 1;
this.max = 100;
this.randnum = Math.round(Math.random() * max) + min;
this.error_sign_mc._visible = false;
this.message_txt.autoSize = true;
this.message_txt.text = "";
this.guess_input_txt.text = "";
this.between_txt.text = "between " + this.min + " and " + this.max;
this.guess_input_txt.onKillFocus = this.checkGuess;
this.guess_input_txt.onSetFocus = function() {
this.error_sign_mc._visible = false;
};
}
private function checkGuess() {
var errors = 0;
try {
if(isNaN(this.guess_input_txt.text)) {
this.guess_input_txt.text = "";
errors++;
throw "you must give a number";
}
}
catch(e:String) {
this.error_sign_mc._visible = true;
this.message_txt.text = e;
}
try {
var num = Number(this.guess_input_txt.text);
if((num < this.min) || (num > this.max)) {
this.guess_input_txt.text = "";
errors++;
throw new numberOutofRangeException(this.min, this.max);
}
}
catch(ex:numberOutofRangeException) {
this.error_sign_mc._visible = true;
this.message_txt.text = ex.getMessage();
}
if(errors==0) {
if(num > this.randnum) {
this.message_txt.text = "Too high!";
}
if(num < this.randnum) {
this.message_txt.text = "Too low!";
}
if(num==this.randnum) {
this.message_txt.text = "You've got it!!!";
}
}
}
}