1. Do you change that SPEED variable somewhere?
2. i'm not quite sure why you do this:
1
| if(c == 'w' && player.getY()+-1*SPEED > 10) |
this just checks if there is a collision at the beginning of the movement? so you could basically just run out of that rectangle you made up for collisions (if you don't stop when you touch it and just keep "w" pressed) cause it just gets checked when you press w and not everytime it changes the position.
No, I don't change it anywhere. Im trying to fix this maybe 7 hours. I tried a lot of things. What I discovered, is that this jump when I click the button very first time, I tried to block that first push, it worked, but then the movement looked very unnatural and the problem still occured when I pressed two buttons in a row(for example left + up). And the collision doesn't work too. Im so exhausted from this. I would really appreciate if you could help me to achieve what I want: Nice looking movement(I can achieve this) but without that first big jump, which destroys all collision detection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| public Game(){ initiateImages(); startGame(); addListeners(); } public void startGame(){ timer = new Timer(15,this); timer.start(); } private void addListeners(){ setFocusable(true); addKeyListener(new KeyListener(){
@Override public void keyPressed(KeyEvent e) { pressed.add(e.getKeyChar()); for(Character c : pressed){ if(c == 'w'){ speedY = -SPEED; }else if(c == 's'){ speedY = SPEED; }else if(c == 'a'){ speedX = -SPEED; }else if(c == 'd'){ speedX = SPEED; } } }
@Override public void keyReleased(KeyEvent e) { pressed.remove(e.getKeyChar()); if(e.getKeyChar()== 'w' || e.getKeyChar() == 's'){ speedY = 0; } if(e.getKeyChar()== 'a' || e.getKeyChar() == 'd'){ speedX = 0; } }
@Override public void keyTyped(KeyEvent arg0) { } }); } private void initiateImages(){ court = new ImageIcon("images/court2.jpg").getImage(); ball = new Ball(); player = new Player(); } private void update(){ if(player.getX()+speedX>10 || player.getX()+speedX<1190){ player.setX(player.getX()+speedX); } if(player.getY()+speedY>10 || player.getY()+speedY<640){ player.setY(player.getY()+speedY); } } public void paintComponent(Graphics g){ super.paintComponent(g); g.drawImage(court, 0, 0, null); Graphics2D g2d = (Graphics2D)g.create(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.setFont(new Font("Serif",Font.BOLD, 36)); g2d.setColor(Color.WHITE); g2d.drawString("Green 0 : 0 Yellow", 40, 40); g2d.setFont(new Font("Serif",Font.BOLD, 20)); g2d.drawImage(ball.getBall(),ball.getX(), ball.getY(),null); g2d.drawImage(player.getPlayer(),player.getX(),player.getY(),null); g2d.drawString("13",player.getX()+10,player.getY()+25); }
@Override public void actionPerformed(ActionEvent arg0) { update(); repaint(); }
} |
This code achieves smooth moves, but that big jump at the beginning(probably because of delay in key presses) are ruining everything.