-
Waiting for Key Input Inside a Class?
I'm rather new to Flash, and I've run into a small problem in designing my first game program. The problem is in the battle loop I have created inside a class to handle the combat aspects of the game. So far, it initializes both characters correctly and displays the enemy encounter message, but the problem comes when it checks for user input to begin the actual combat.
What I'm looking for is a command to wait for the user to press a given key (in this case space), to continue onto the next part of the loop. This is what I have so far, but it does not seem to be working.
Code:
package{
import flash.text.TextField;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Battle extends Sprite{
public var num:int;
public var damage:int;
public var encounter:String;
private var battleText:TextField;
public function battle(hero:Hero){
//Generates a text field for use in the class
battleText = new TextField();
addChild(battleText);
battleText.border = true;
battleText.width = 430;
battleText.height = 280;
battleText.x = 110;
battleText.y = 10;
battleText.wordWrap = true;
eventFinder(hero);
}
public function eventFinder(hero:Hero){
// Generates a random number to create various opponents
num = randomNumber(1,1);
if (num == 1){
var wolf:Wolf = new Wolf();
monsterEncounter(hero, wolf);
}
}
public function monsterEncounter(hero:Hero, monster:Character){
// Generates monster attributes and identifiers
monster.Initialize(monster);
// Displays a pre-writen string
battleText.text = monster.encounter;
this.addEventListener(KeyboardEvent.KEY_DOWN, battleLoop);
}
public function battleLoop(e:KeyboardEvent){
this.removeEventListener(KeyboardEvent.KEY_DOWN, battleLoop);
if(e.keyCode == 32){
// This is never displayed
trace('Congrats');
}
}
}
}
If somebody could point me in the right direction, that would be greatly appreciated. Also, if there is a way to carry over the hero and monster variables, that would simplify my work greatly. Mind you, this is all in an actionscript class, not the main timeline.
Any feedback is appreciated.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|