Pregunta

I'm making this simple game(just for fun) and when I say simple, I really want it to be simple. Now so far I have a square that I can move anywhere I want. I'm using KeyListeners(works completely fine, I don't want to use KeyBindings for this current project) and I was wondering if I can just use loops in order to make it go both horizontally and vertically at the same time. In this code you can witness my attempt of putting in the loop and it's giving me an error under the while saying:

Syntax error on token while

I have two questions 1. Will the loop even get what I want done? if no, what do you suggest? 2. If yes, what's wrong with my while loop?

Thank you

My Code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class MyGame extends JPanel implements ActionListener, KeyListener {
    Timer t = new Timer(5, this);
    int x = 0, y = 0, velx =0, vely =0, loop = 0;

    public MyGame() {
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(x,y,50,30);
    }

    public void actionPerformed(ActionEvent e) {
        if(x < 0)
        {
            velx=0;
            x = 0;  
        }

        if(x > 530)
        {
            velx=0;
            x = 530;    
        }

        if(y < 0)
        {
            vely=0;
            y = 0;  
        }

        if(y > 330)
        {
            vely=0;
            y = 330;    
        }

        x += velx;
        y += vely;
        repaint();
    }
    while (loop = 0) {
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();

        if (code == KeyEvent.VK_DOWN){
            vely = 1;
            velx = 0;
        }
        if (code == KeyEvent.VK_UP){
            vely = -1;
            velx = 0;
        }
        if (code == KeyEvent.VK_LEFT){
            vely = 0;
            velx = -1;
        }
        if (code == KeyEvent.VK_RIGHT){
            vely = 0;
            velx = 1;
        }
    }
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {
        velx=0;
        vely=0;
    }}


    public static void main (String arge[]){

        JFrame f = new JFrame();
        MyGame s = new MyGame();
        f.add(s);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(600,400);
        f.setVisible(true);

    }
}
¿Fue útil?

Solución

First of all you cannot declare a loop outside of a method. To fix your problem you should remove the loop and instead change your vely/velx assignments. To register mutiple keys at the same time you should only set one variable to either 1 or -1 and just leave the other one be.

if (code == KeyEvent.VK_DOWN){
   vely = 1;
}

now since you are registering multiple keys you also have to differentiate between them in the keyreleased method where you should now set the appropriate velocity to 0 and let the other one stay as it is.

Otros consejos

Just remove while (loop = 0) { that is not declared inside a method or constructor.


I have already posted the complete code in the exactly same context to go horizontally and vertically at the same time.

Please have a look at below posts

enter image description here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top