Weird error in keylistoner

Spacebar giving me trouble

Page 1 of 1

14 Replies - 708 Views - Last Post: 06 April 2009 - 07:28 PM Rate Topic: -----

#1 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Weird error in keylistoner

Posted 03 April 2009 - 10:30 PM

ok, so this code is 15 classes, and a text file large, so i dont think i will be putting all that out there. I think ill just put where i THINK the problem is
(i didnt put this into homework, because it really doesnt have a due date, and even with this problem it can be turned in. Plus this is just more of a what did i do wrong type question)

So i got a strange problem with my code for my final project, its not a huge problem, i can try to deal with never being able to use the space bar, but it would be nice. so its really not a big deal if this error is still in here, but it would be nice to at least understand what is going on, and seeing as the teacher couldnt find it i would like to call upon the people of DIC to help me out


The problem is that when the space bar is clicked the movement of the player and enemy goes crazy, if never clicked the movement is perfect, if clicked once, there is a 1 position jump, if clicked 30 times, a 30 position jump occurs


(this is in my Play class, it gets run when the user starts the game play, its only the button listener part though, if you think the problem lays else were i can show you some more)
private class buttonList implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) //on the buy button
		{
			String Sender = ((JButton)(e.getSource())).getText();
			if(Sender.equals("Start Game"))
			{
				start.setLocation(1000, 1000);
				remove(changeItems);
//				choice.setLocation(1000, 1000);
				choice.setSize(0, 0);
				start.addKeyListener(new keyList());
				repaint();
			}
			if(Sender.equals("Change Items"))
			{
				start.setLocation(choice.getWidth()-190, choice.getHeight()-25);
				changeItems.setSize(0, 0);
				createItemSelectionButtons();				
				choice.repaint();
			}
			if(Sender.equals("Guns"))
			{
				equipChoice = 1;//Guns
			}
			if(Sender.equals("Hopper"))
			{
				equipChoice = 2;//Guns
			}
			if(Sender.equals("Paint"))
			{
				equipChoice = 3;//Guns
			}

		}
		
		
		private void createItemSelectionButtons() 
		{
			GunButton.setLocation(80,20);
			GunButton.setSize(100, 25);
			HopperButton.setLocation(185,20);
			HopperButton.setSize(100, 25);
			PaintButton.setLocation(290,20);
			PaintButton.setSize(100, 25);
			
			choice.add(GunButton);
			choice.add(HopperButton);
			choice.add(PaintButton);
			
			PaintButton.addActionListener(new buttonList());
			GunButton.addActionListener(new buttonList());
			HopperButton.addActionListener(new buttonList());
		}
		
	}
	private class keyList implements KeyListener //Player's movement
	{
		public void keyPressed(KeyEvent e)//Dudes Movement
		{
			if(e.getKeyCode()!=32)
			{
				repaint();
			
				int xloc = player.getXLoc();
				int yloc = player.getYLoc();
				int Kc = e.getKeyCode();
//				System.out.print(Kc);
				//player still has actions left
				if(player.getActionsLeft() >= 1)// && enemy.getTotalActions()>0)
				{
					if(Kc == 38) //UP
					{
						System.out.println(" - Up");
						if(yloc-1>=0)
						{
							player.setPosition(xloc, yloc-1);
							player.setMovementsUsed(movement);
						}
					}
					if(Kc == 37) // LEFT
					{
		//				System.out.println(" - Left");
						if(xloc-1>=0)
						{
							player.setPosition(xloc-1, yloc);
							player.setMovementsUsed(movement);
						}
					}
					if(Kc == 40) //DOWN
					{
		//				System.out.println(" - Down");
						if(yloc+1<map.X)
						{
							player.setPosition(xloc, yloc+1);
							player.setMovementsUsed(movement);
						}
					}
					if(Kc == 39) //RIGHT
					{
		//				System.out.println(" - Right");
						if(xloc+1<map.Y)
						{
							player.setPosition(xloc+1, yloc);
							player.setMovementsUsed(movement);
						}
					}
					if(playerCanShoot)
					{
						if(Kc == 83)//83=S //Space bar = 32
						{
							player.shoot();
							player.setMovementsUsed(shoot);
	//						player.shoot();
						}
					}
					if(Kc==37 ||Kc == 38 || Kc==39 || Kc==40)
					{
						playerdirection = Kc;
					}
				}
				//AI, move to another class if possibe
				if(player.getActionsLeft()==0 && enemy.getMovementsLeft() >= 1)
					while(enemy.getMovementsLeft() >= 1)
					{
						int SPlayerY = player.getYLoc(), SPlayerX = player.getXLoc(), Yloc =					  enemy.getYLoc(), Xloc = enemy.getXLoc();
						System.out.println(enemy.getMovementsLeft());
						if(enemy.getMovementsLeft()==0)
							player.setMovementsUsed(player.getTotalActions());		
						if(SPlayerX - Xloc < 0)
						{
							enemy.setPosition(Xloc-1,Yloc);
							enemydirection = 37;
						}
						if(SPlayerX - Xloc > 0)
						{
							enemy.setPosition(Xloc+1,Yloc);
							enemydirection = 39;
						}
						if(SPlayerY - Yloc > 0)
						{
							enemy.setPosition(Xloc, Yloc+1);
							enemydirection = 40;
						}
						if(SPlayerY - Yloc < 0)
						{
							enemy.setPosition(Xloc, Yloc-1);
							enemydirection = 38;
						}
						enemy.setMovementsUsed(movement);
					}
				if(player.getActionsLeft()==0 && enemy.getMovementsLeft() == 0)
				{
					player.setMovementsUsed((-player.getTotalActions()));
					enemy.setMovementsUsed((-enemy.getTotalActions()));
				}
				repaint();
				Kc = 0;
			}
			
//		System.out.println("\n movement:" + player.getActionsLeft());
		}
		public void keyReleased(KeyEvent e) {}
		public void keyTyped(KeyEvent e) {}
	}



if any explanations are needed for this code ill tell you anything, and thank you to anyone who can/tries to help me.

This post has been edited by virgul: 03 April 2009 - 10:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Weird error in keylistoner

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Weird error in keylistoner

Posted 04 April 2009 - 04:17 AM

I'm not sure if this is your problem but it seems that if you press the space button you get blanks put into your input-buffer and the events stored in the buffer are taken care of in order.
Maybe you can resolve the problem by putting the code in the keytyped method instead, that way you have to release the space button and press it again to get two space-events. I'm guessing it clears the buffer after each event.

I'm not sure if maybe this might cause problems too..

 if(e.getKeyCode()!=32)
			{
				repaint();
			
				int xloc = player.getXLoc();
				int yloc = player.getYLoc();
				int Kc = e.getKeyCode();



You call e.getKeyCode() twice and you may actually receive two different results from the two calls.
Maybe you want to do this..

int Kc = e.getKeyCode();
if(Kc != 32)
			{




If that wasn't it then maybe you can explain more about the problem.

This post has been edited by Gloin: 04 April 2009 - 04:18 AM

Was This Post Helpful? 1
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Weird error in keylistoner

Posted 04 April 2009 - 07:55 AM

So this is your shooting code, and you really want it to work when the user presses space (i.e. you want to change that 83 to a 32)), right?

                    if(playerCanShoot)
                    {
                        if(Kc == 83)//83=S //Space bar = 32
                        {
                            player.shoot();
                            player.setMovementsUsed(shoot);
    //                        player.shoot();
                        }
                    }


First you've commented out the player.shoot() method but I guess you know that already.

The real problem is right at the top:

            if(e.getKeyCode()!=32)


If the user presses space, this will be false and the movement and shooting code will never be reached. I've not examined your code closely but I can't think why you would want this if condition anyway. Take it out and your code should work with space.

Hope that helps and let us know if you fix it!
Was This Post Helpful? 1
  • +
  • -

#4 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Weird error in keylistoner

Posted 04 April 2009 - 05:18 PM

thanks for the responses both of you.

Just a quick note, i put the
if(e.getKeyCode()!=32)

in there because even when i didnt have space defined it would give me the error, so i thought, well what if i just tried to get it to ignore the command.

for the idea that doing it this way might help
int Kc = e.getKeyCode();
if(Kc != 32)
			{

it unfortunately doesnt work still.

For putting the movement and what not into the other listeners (keyTyped and keyReleased) this doesnt work either.

and yea i know i got
player.shoot();

commented out, it was to test if that was giving me the problem


Lets see, more details about the problem, i do believe it has something to do with the input-buffer but im not sure how to cleanse/clear this.

the actual problem is when you go to move the character, everything is fine, but once you hit the spacebar, the program starts moving u two times, then every time you hit the space bar after that it keeps going up by one move.

an interesting note, is that when shoot is set to 32, the first time you hit the space bar it says the accuracy(which is should) then the second time you hit it, it reprints it two times(a total of 3 at this point), then the third... you get the point, so i do think it has something to do with the buffer
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Weird error in keylistoner

Posted 05 April 2009 - 01:58 AM

What error happened when you didn't have space defined? At the bottom you have some code for changing direction. However, it defines no behaviour for shooting. Should it tell the movement to stop? That could be why it keeps moving. Other odd behaviour could be problems in the shoot() method.

At the moment you have the logic:
if (kc != 32) {
  if (kc == 32) {
    doStuff();
  }
}


The method doStuff() can never be reached so that first if statement is clearly wrong.
Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Weird error in keylistoner

Posted 05 April 2009 - 04:51 AM

Can you post the code as it is right now coz I'm not sure you made the changes in all the places.
You should only use e.getKeyCode() and store the result in a variable once per iteration.
Was This Post Helpful? 0
  • +
  • -

#7 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Weird error in keylistoner

Posted 05 April 2009 - 01:49 PM

here is what I got for right now. I tried the other changes and they didnt work. When I put the code into keyReleased it works the same as if it were in keyPressed, but when i put it into keyTyped, nothing seems to work

package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Play extends JPanel{
	
	//amount it costs to do things
	int movement = 1;
	int shoot = 1;
	
	Play play = this;
	boolean playerCanShoot = false;
	Character player;//, enemy = new Character();
	Enemy enemy = new Enemy();//Delete this later, if enemy gets passed
	StatSheet StatSheet;
	Container Frame;
	JButton start = new JButton("Start Game");
	JButton changeItems = new JButton("Change Items");
	JButton GunButton = new JButton("Guns");
	JButton HopperButton = new JButton("Hopper");
	JButton PaintButton = new JButton("Paint");
	Map map;
	boolean alive = true;
	int playerdirection = 39, enemydirection = 37;//facing right, left 
	int equipChoice, height, width;
	ItemChoice choice;
	String s = "src/Game/Images/Map/";
	public Image Grass = Toolkit.getDefaultToolkit().createImage(s+"Grass.png");
	public Image Rock = Toolkit.getDefaultToolkit().createImage(s+"Rock.png");
	public Image enemyPic = Toolkit.getDefaultToolkit().createImage(s+"Rock.png");
	
	
	public Play(Container frame, Character Player, StatSheet sSheet, int x, int y)
	{
		StatSheet = sSheet;
		player = Player;
		Frame = frame;
		setLayout(null);
		map = new Map(x,y);
		Frame.setSize(Frame.getToolkit().getScreenSize());
		Frame.setLocation(0, 0);
		choice = new ItemChoice();
		height = Frame.getHeight();
		width = Frame.getWidth();
		choice.setLocation((width/4+width/16),(height/4-height/20));
		choice.setSize(500, 500);
		add(choice);
		choice.createbuttons();
		enemy.setPosition(map.X-1, (int)(y/2));//0 indexed
		player.setPosition(0, (int)(y/2));
	}

	public void paintComponent(Graphics p)
	{
		super.paintComponent(p);
		paintMap(p);
		paintVisableArea(p);
	}

	private void paintVisableArea(Graphics p) 
	{
		int TopBound, LeftBound, RightBound, BottomBound;
		int rng = player.getRange(), Xloc = player.getXLoc(), Yloc = player.getYLoc();
		playerCanShoot = false;
		//following means   ? true : false	 ? is a if()   if(true) if(false)
		TopBound = Xloc-rng>0 ? Xloc-rng : 0;//advanced if Statement
		LeftBound = Yloc-rng>0 ? Yloc-rng : 0;//thank you scott, looks cool!
		BottomBound = Xloc+rng+1<map.X ? Xloc+rng+1 : map.X;
		RightBound = Yloc+rng+1<map.Y ? Yloc+rng+1 : map.Y;
		int h = getHeight()/map.Y;
		int w = getWidth()/map.X;
		for(int x = TopBound; x<BottomBound; x++)//Gravity well! for the vision
			for(int y = LeftBound; y<RightBound; y++)
			{
				if(!playerCanShoot)
				{
					playerCanShoot = x == enemy.getXLoc() && y == enemy.getYLoc() ? true : false;
//					if(playerCanShoot)
//					{
//						System.out.print("I FOUND YOU!!");
//						
//					}
				}
				int img = map.map[x][y];//1 or 2 for what is painted
				Image clear = null;
				if(img == 1)//repaints the seen versions of the picture
					clear=Rock;
				if(img == 2)
					clear=Grass;
				if(Math.abs(Xloc - x) + Math.abs(Yloc - y)<player.getRange())// creates g-well
				{
	// 				if(Math.abs(x-TopBound)+Math.abs(y-LeftBound)<=3)//fix, using g Well?
					p.drawImage(clear, x*w, y*h, w, h, this);
					if(x == enemy.getXLoc()&& y==enemy.getYLoc())
					{
						p.drawImage(enemy.getImage(enemydirection), enemy.getXLoc()*w, enemy.getYLoc()*h, this);
						//auto lock area?
					}
				}
				
 				repaint();
			}
		
		p.drawImage(player.getImage(playerdirection), player.getXLoc()*w, player.getYLoc()*h, this);
		
	}

	
	
	private void paintMap(Graphics p) 
	{
		int h = getHeight()/map.Y;
		int w = getWidth()/map.X;
		for(int x = 0; x<map.X; x++)
			for(int y = 0; y<map.Y; y++)
			{
				int img = map.map[x][y];
				p.drawImage(map.getImage(img), x*w, y*h, w, h, this);
			}
	}
	
	
	
	private class buttonList implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) //on the buy button
		{
			String Sender = ((JButton)(e.getSource())).getText();
			if(Sender.equals("Start Game"))
			{
				start.setLocation(1000, 1000);
				remove(changeItems);
//				choice.setLocation(1000, 1000);
				choice.setSize(0, 0);
				start.addKeyListener(new keyList());
				repaint();
			}
			if(Sender.equals("Change Items"))
			{
				start.setLocation(choice.getWidth()-190, choice.getHeight()-25);
				changeItems.setSize(0, 0);
				createItemSelectionButtons();				
				choice.repaint();
			}
			if(Sender.equals("Guns"))
			{
				equipChoice = 1;//Guns
			}
			if(Sender.equals("Hopper"))
			{
				equipChoice = 2;//Guns
			}
			if(Sender.equals("Paint"))
			{
				equipChoice = 3;//Guns
			}

		}
		
		
		private void createItemSelectionButtons() 
		{
			GunButton.setLocation(80,20);
			GunButton.setSize(100, 25);
			HopperButton.setLocation(185,20);
			HopperButton.setSize(100, 25);
			PaintButton.setLocation(290,20);
			PaintButton.setSize(100, 25);
			
			choice.add(GunButton);
			choice.add(HopperButton);
			choice.add(PaintButton);
			
			PaintButton.addActionListener(new buttonList());
			GunButton.addActionListener(new buttonList());
			HopperButton.addActionListener(new buttonList());
		}
		
	}
	private class keyList implements KeyListener //Player's movement
	{
		public void keyPressed(KeyEvent e)//Dudes Movement
		{
			int Kc = e.getKeyCode();
			if(Kc!=32)
			{
				repaint();
			
				int xloc = player.getXLoc();
				int yloc = player.getYLoc();
				
//				System.out.print(Kc);
				//player still has actions left
				if(player.getActionsLeft() >= 1)// && enemy.getTotalActions()>0)
				{
					if(Kc == 38) //UP
					{
						System.out.println(" - Up");
						if(yloc-1>=0)
						{
							player.setPosition(xloc, yloc-1);
							player.setMovementsUsed(movement);
						}
					}
					if(Kc == 37) // LEFT
					{
		//				System.out.println(" - Left");
						if(xloc-1>=0)
						{
							player.setPosition(xloc-1, yloc);
							player.setMovementsUsed(movement);
						}
					}
					if(Kc == 40) //DOWN
					{
		//				System.out.println(" - Down");
						if(yloc+1<map.X)
						{
							player.setPosition(xloc, yloc+1);
							player.setMovementsUsed(movement);
						}
					}
					if(Kc == 39) //RIGHT
					{
		//				System.out.println(" - Right");
						if(xloc+1<map.Y)
						{
							player.setPosition(xloc+1, yloc);
							player.setMovementsUsed(movement);
						}
					}
					if(playerCanShoot)
					{
						//space bar still giving me problems, using S for now
						if(Kc == 83)//83=S //Space bar = 32 
						{
							player.shoot();
							player.setMovementsUsed(shoot);
	//						player.shoot();
						}
					}
					if(Kc==37 ||Kc == 38 || Kc==39 || Kc==40)
					{
						playerdirection = Kc;
					}
				}
				//AI, move to another class if possibe
				if(player.getActionsLeft()==0 && enemy.getMovementsLeft() >= 1)
					while(enemy.getMovementsLeft() >= 1)
					{
						int SPlayerY = player.getYLoc(), SPlayerX = player.getXLoc(), Yloc = enemy.getYLoc(), Xloc = enemy.getXLoc();
						System.out.println(enemy.getMovementsLeft());
						if(enemy.getMovementsLeft()==0)
							player.setMovementsUsed(player.getTotalActions());		
						if(SPlayerX - Xloc < 0)
						{
							enemy.setPosition(Xloc-1,Yloc);
							enemydirection = 37;
						}
						if(SPlayerX - Xloc > 0)
						{
							enemy.setPosition(Xloc+1,Yloc);
							enemydirection = 39;
						}
						if(SPlayerY - Yloc > 0)
						{
							enemy.setPosition(Xloc, Yloc+1);
							enemydirection = 40;
						}
						if(SPlayerY - Yloc < 0)
						{
							enemy.setPosition(Xloc, Yloc-1);
							enemydirection = 38;
						}
						enemy.setMovementsUsed(movement);
					}
				if(player.getActionsLeft()==0 && enemy.getMovementsLeft() == 0)
				{
					player.setMovementsUsed((-player.getTotalActions()));
					enemy.setMovementsUsed((-enemy.getTotalActions()));
				}
				repaint();
				Kc = 0;
			}
//		System.out.println("\n movement:" + player.getActionsLeft());
		}
		public void keyReleased(KeyEvent e) {}
		public void keyTyped(KeyEvent e) {}
	}
	private class ItemChoice extends JPanel implements KeyListener{
		private ItemChoice()
		{
			setLayout(null);
		}
		private void createbuttons()
		{
			changeItems.setSize(200, 25);
			changeItems.setLocation(5, 5);
			changeItems.setBackground(Color.red);
			add(changeItems);
			changeItems.addActionListener(new buttonList());
			start.setSize(200, 25);
			start.setLocation(225, 5);
			start.setBackground(Color.green);
			add(start);
			start.addActionListener(new buttonList());
			
			add(GunButton);
			add(HopperButton);
			add(PaintButton);
		}
		
		private void itemSelection(Graphics p) 
		{
//			p.fillRect(getWidth()/4, 0, getWidth()/2, 300);
			Gun[] guns = player.CharGun;
			Hopper[] hoppers = player.CharHopper;
			Paint[] paints = player.CharPaint;
			int index = 0;
			int width = 100;
			int height = 100;
			if(equipChoice == 1)//guns
				while(guns[index]!=null)
				{
					p.drawImage(guns[index].getImage(), 100, 100+index*(height+25), width, height,this);
					index++;
				}
			if(equipChoice == 2)//Hopper
				while(hoppers[index]!=null)
				{
					p.drawImage(hoppers[index].getImage(), 100, 100+index*(height+25), width, height,this);
					index++;
				}
			if(equipChoice == 3)//Paint
				while(paints[index]!=null)
				{
					p.drawImage(paints[index].getImage(), 100, 100+index*(height+25), width, height,this);
					index++;
				}
			repaint();
		}
		
		public void paintComponent(Graphics p)
		{
			super.paintComponent(p);
			itemSelection(p);
		}
		
		public void keyPressed(KeyEvent e) {}
		public void keyReleased(KeyEvent arg0) {}
		public void keyTyped(KeyEvent arg0) {}
		
	}
}	


Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Weird error in keylistoner

Posted 05 April 2009 - 03:25 PM

Quote

I tried the other changes and they didnt work.


That really just means you've not found all the problems. If there are a few bugs, you'll have to make a few changes to fix them all. Until you do that your program won't work. At the moment we're guessing what your code is supposed to do. I'm sure your descriptions make perfect sense to you. You've designed the game and know your code intimately. All I know is that things can move about and shoot. There are a million ways that could work and another million ways to code each.

boolean playerCanShoot = false;


This is at least part of your problem. I can't find anywhere in your code that you change this to true.

Gloin, I understand it's good to just call e.getKeyCode() once for efficiency purposes but I don't think there's any logic error in calling it several times. It should always return the same value.
Was This Post Helpful? 0
  • +
  • -

#9 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Weird error in keylistoner

Posted 05 April 2009 - 03:46 PM

good point, i do know my code very well, and you guys really arnt going off of much.

other then because its like 15 files large I really just dont want to post the whole thing, mostly because of the teacher.. but would it be better if i just sent it to you guys? it might be easier that way, because then you could see what the whole project does, and i dont need to post the whole thing online.

o, and the playerCanShoot gets set to true over where it says
playerCanShoot = x == enemy.getXLoc() && y == enemy.getYLoc() ? true : false;

its a weird if statement that i was messing around with (yes it works like this and like a normal if, this was one of my first suspects)

This post has been edited by virgul: 05 April 2009 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#10 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Weird error in keylistoner

Posted 05 April 2009 - 04:08 PM

I notice you have two calls to the repaint-method inside the snippet we're discussing. What happens if you comment out the first one.
Either way that one shouldnt' be necessary.
Was This Post Helpful? 0
  • +
  • -

#11 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Weird error in keylistoner

Posted 05 April 2009 - 11:01 PM

commenting out the first repaint does nothing, in fact im not even sure why it was there in the first place.

Quick question, does taking that out, the repaint method, does that make a performance difference?
Was This Post Helpful? 0
  • +
  • -

#12 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Weird error in keylistoner

Posted 06 April 2009 - 08:10 AM

I'm actually not quite sure. What concerns me is that it's possible that one generated keyevent might cancel out another previous event while repainting the JFrame. That's why I thought you should remove it.

I was thinking yesterday about how you represent playerdirection.
if(Kc==37 ||Kc == 38 || Kc==39 || Kc==40)
					{
						playerdirection = Kc;
					}



How do you handle those values later on?
I would probably remove that if-statement anyways and do the assignments in the other 4 if-statements instead. No point in checking that twice.
Was This Post Helpful? 0
  • +
  • -

#13 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Weird error in keylistoner

Posted 06 April 2009 - 11:51 AM

for the player direction, this is what i do,

(this is part of the character class)
public Image getImage(int i)
	{
		if(i == 38)//up
			return charUp;
		if(i == 37)
			return charLeft;
		if(i == 40)
			return charDown;
		if(i == 39)
			return charRight;
		return null;
	}




the above code is called from the play class, it looks like this
for(int x = TopBound; x<BottomBound; x++)//Gravity well! for the vision
			for(int y = LeftBound; y<RightBound; y++)
			{
				if(!playerCanShoot)
				{
					playerCanShoot = x == enemy.getXLoc() && y == enemy.getYLoc() ? true : false;
//					if(playerCanShoot)
//					{
//						System.out.print("I FOUND YOU!!");
//						
//					}
				}
				int img = map.map[x][y];//1 or 2 for what is painted
				Image clear = null;
				if(img == 1)//repaints the seen versions of the picture
					clear=Rock;
				if(img == 2)
					clear=Grass;
				if(Math.abs(Xloc - x) + Math.abs(Yloc - y)<player.getRange())// creates g-well
				{
	// 				if(Math.abs(x-TopBound)+Math.abs(y-LeftBound)<=3)//fix, using g Well?
					p.drawImage(clear, x*w, y*h, w, h, this);
					if(x == enemy.getXLoc()&& y==enemy.getYLoc())
					{
						p.drawImage(enemy.getImage(enemydirection), enemy.getXLoc()*w, enemy.getYLoc()*h, this);
						//auto lock area?
					}
				}
				
 				repaint();
			}
		
		p.drawImage(player.getImage(playerdirection), player.getXLoc()*w, player.getYLoc()*h, this);
		
	}


Was This Post Helpful? 0
  • +
  • -

#14 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Weird error in keylistoner

Posted 06 April 2009 - 05:15 PM

so, i was thinking of the best way to show everyone what my problem is, then i realized, why dont i just make a .jar file... well its very embarrassing for me to say that i cant figure out how to get the pictures to show up in the jar i made. and yes, i got the actual program to run, just the pictures wont show

i have checked and ya, it looks as if the pictures are in the right place, any ideas?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Weird error in keylistoner

Posted 06 April 2009 - 07:28 PM

View Postvirgul, on 6 Apr, 2009 - 04:15 PM, said:

so, i was thinking of the best way to show everyone what my problem is, then i realized, why dont i just make a .jar file... well its very embarrassing for me to say that i cant figure out how to get the pictures to show up in the jar i made. and yes, i got the actual program to run, just the pictures wont show

i have checked and ya, it looks as if the pictures are in the right place, any ideas?

We are far from the subject of the first post
I am closing that topic
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1