PDA

View Full Version : Confusing problem with event handlers


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!!!";
}

}

}

}

hannahd
06-08-2005, 12:02 PM
I now have a different problem where the event handlers don't appear to get listened to. It doesn't even call the functions.

Please help :)


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;
private var onSetFocus:Function;
private var onKillFocus:Function;

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.onKillFocus = checkGuess;
this.onSetFocus = function():Void {
this.error_sign_mc._visible = false;

};

guess_input_txt.addListener(this);

}

private function checkGuess():Void {
trace("hello");
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!!!";
}

}

}

}

senocular
06-08-2005, 12:46 PM
automated response:
[useastags]