#I have a game and Im looking to make the character also go diagonally can aanyone help?

1 messages · Page 1 of 1 (latest)

small egret
#

public class myClass {

public static void main(String[] args) {

    new MyFrame();
}

}

import java.awt.Color;
import java.awt.event.;
import javax.swing.;

public class MyFrame extends JFrame implements KeyListener{

JLabel label;
ImageIcon icon;

MyFrame(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(500,500);
    this.setLayout(null);
    this.addKeyListener(this);

    icon = new ImageIcon("rocket.png");

    label = new JLabel();
    label.setBounds(0, 0, 100, 100);
    label.setIcon(icon);
    label.setBackground(Color.red);
    label.setOpaque(true);
    this.getContentPane().setBackground(Color.black);
    this.add(label);
    this.setVisible(true);
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

    if (e.getKeyCode()== 37) { // left arrow
        label.setLocation(label.getX()-10, label.getY());
    } else if (e.getKeyCode()== 38) { // up arrow
        label.setLocation(label.getX(), label.getY()-10);
    } else if (e.getKeyCode()== 39) { // right arrow
        label.setLocation(label.getX()+10, label.getY());
    } else if (e.getKeyCode()== 40) { //down arrow
        label.setLocation(label.getX(), label.getY()+10);
    }


}

@Override
public void keyReleased(KeyEvent e) {
    //keyReleased = called whenever a button is released
    System.out.println("Event keyReleased");
}

}

woven tulipBOT
#

This post has been reserved for your question.

Hey @small egret! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.