Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Wrathnut

[java] Animation/Keyboard question

This topic is 5767 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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."

Share this post


Link to post
Share on other sites
Advertisement
You want the animation to be bound to the movement of the ship, not the pressing of a key. Yes, pressing a key makes the ship move but thats besides the point! An enemy ship moves without keypresses and should animate correctly. Your idea to use a flag seems correct to me. Have a state that your ship is in: MOVINGUP, MOVINGDOWN, etc.. and use the correct sprite based on that.

The thing with java is its implemented slightly differently on varying platforms. On some, you continue to receive keypresses while you hold down the key while on others you just get a single one followed by a keyup when you release.

Share this post


Link to post
Share on other sites
Actually jermz my code does call my images based on state. I could post the rest of my graphics code if you want. But basically there are a few main points where you would want to trigger an animation after they occur such as a collision detection or a respawn, or what have you.

In the case it would be a key press. In my object model for the game it seems that it would be cheaper to filter it out as it comes in than to constantly be checking for it within the game engine itself. I am not sure if I am making much sense here.

Also for the most part I am trying to not tie the code to any specific game. This is sort of my game library.

As for the flag thing my thoughts on that were to have a flag for the animation class like run_once or something but I also want to accurately be able to retrigger it again. As far as I can tell I am going to run into some problems if I use a delay value.

Thanks for the advice, again I appreciate all and any input.

"Technological progress is like an ax in the hands of a pathological criminal."

Share this post


Link to post
Share on other sites
The boolean method is the way that it is handled most of the time anyway.



Make it work, then
make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!