Hi guys,
I have a fairly easy questoin I''ve been trying to figure out for the last couple of days. I have a 2d side scrolling space shooter that I have been working for the past couple of years.. yes years.
Anyway, I just got the animation classes all finished and working when I find out that I can''t seem to get one of the main things animated that I wantewd to.. Which is the ship tilting up and down when you move ship up or down.. you know rolling the wings up and down.
Anyway the problem seems to be in my keyboard. I need to trigger the animation as soon as the key is pressed and not again until that key is pressed.. I thought about just setting a boolean flag to catch this kind of thing, but to be honest that seems to be a real bubble gum and bailing wire solution to the problem.
As is stands depending on where I place my trigger the animation won''t start till after the key is released or will keep trying to restart while the key is held down. I tried to programmatically release the key but that brings about the latter problem agian.
If anyone has any suggestions I woul be most grateful, BTW I will post my keyboard stuff below
Key Class
public class keyJ{
private String keyName;
private int keyValue;
private boolean down;
keyJ(String strName, int intValue){
this.keyName = new String(strName);
this.keyValue = intValue;
this.down = false;
}
public void press(){
this.down = true;
}
public void release(){
this.down = false;
}
public String getName(){
return this.keyName;
}
public int getValue(){
return this.keyValue;
}
public boolean isDown(){
return this.down;
}
}
Keyboard Class
import java.util.Vector;
public class keyboardJ{
private Vector keyList;
keyboardJ(){
this.keyList = new Vector();
}
public void addKey(String name, int val){
this.keyList.addElement(new keyJ(name,val));
}
public void keyDown(int kVal){
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getValue() == kVal){
((keyJ)this.keyList.elementAt(i)).press();
}
}
}
public void keyDown(String kName){
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getName().equalsIgnoreCase(kName)){
((keyJ)this.keyList.elementAt(i)).press();
}
}
}
public void keyUp(int kVal){
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getValue() == kVal){
((keyJ)this.keyList.elementAt(i)).release();
}
}
}
public void keyUp(String kName){
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getName().equalsIgnoreCase(kName)){
((keyJ)this.keyList.elementAt(i)).release();
}
}
}
public boolean isKeyDown(String kName){
boolean bRetVal = false;
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getName().equalsIgnoreCase(kName)){
bRetVal = ((keyJ)this.keyList.elementAt(i)).isDown();
}
}
return bRetVal;
}
public int getKeyValue(String kName){
int bRetVal = -1;
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getName().equalsIgnoreCase(kName)){
bRetVal = ((keyJ)this.keyList.elementAt(i)).getValue();
}
}
return bRetVal;
}
public void report(){
//Report about data stored here
System.out.println("========= KEYBOARD REPORT =========");
System.out.println("Total keys: " + this.keyList.size());
System.out.println("========= KEYBOARD REPORT =========");
}
}
And her is where everything is triggered from the applet
Bebop is teh game object, any of the keyboard methods referenced are just piped to the object held within the game object..
public boolean keyDown(Event e, int key){
Bebop.pressKey(key);
return true;
}
public boolean keyUp(Event e, int key){
Bebop.releaseKey(key);
return true;
}
//Keyboard handling functions look like this
//Handle the keyboard
public void handleKeyboard(){
if(Bebop.gameState == 2){ //If the game is active accept controlling input
if(Bebop.isKeyDown("Up")){
Bebop.currentPlayer.theta = 90;
Bebop.currentPlayer.linearMove();
}
if(Bebop.isKeyDown("Down")){
Bebop.currentPlayer.theta = 270;
Bebop.currentPlayer.linearMove();
}
}
}
"Technological progress is like an ax in the hands of a pathological criminal."