当我在 "player object" 上使用名为 movePosition() 的方法时,它会更改由该 class 制作的所有其他对象的 x 位置
When I use a method called movePosition() on a "player object", it changes the x position for all other objects made from that class
我想设置玩家对象的速度使其 x 位置增加 2,我还想设置实体对象的速度使其 x 位置增加 3。
但是,在每个 tick 结束时,对象玩家和实体的 x 位置都增加了 5,并且两者同时移动。
我真的很感激你能分享的任何帮助。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Frame extends JPanel implements ActionListener {
// Initialize ObjectOne objects
ObjectOne obj = new ObjectOne();
ObjectOne objRM = new ObjectOne();
ObjectOne entity = new ObjectOne();
ObjectOne player = new ObjectOne();
// Create timer object
Timer tm = new Timer(10, this);
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Create menus
obj.drawGRect("Your current score: ", 0, 0, 25, g);
objRM.drawGRect("N/A.", 0, 23, 25, g);
// Create players
player.setY(2);
player.drawGRect(20, 20, g);
repaint();
entity.setY(40);
entity.drawGRect(20, 20, g);
repaint();
}
public Frame() {
setBackground(Color.BLACK);
tm.start();
}
public static void main(String[] args) {
Frame frClass = new Frame();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(800, 100);
frame.add(frClass);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
player.setPosition(2);
repaint();
entity.setPosition(3);
repaint();
//The positions for both player and entity will be incremented by 5 every tick.
System.out.println(player.getX());
}
}
对象一class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
public class ObjectOne {
private static int x;
private static int y;
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
// Creates rectangle without text
public void drawGRect(int w, int h, Graphics g) {
Graphics2D g2d = (Graphics2D) g;
//Creates white rectangle
Rectangle rect = new Rectangle(x, y, w, h);
g.setColor(Color.WHITE);
g2d.fill(rect);
//creates black rectangle
Rectangle rectG = new Rectangle(x + 2, y + 2, w - 4, h - 4);
g2d.setColor(Color.BLACK);
g2d.fill(rectG);
}
//Creates rectangle with text
public void drawGRect(String s, int x, int y, int h, Graphics g) {
int w = s.length() * 6 + 15;
Graphics2D g2d = (Graphics2D) g;
//Creates white rectangle
Rectangle rect = new Rectangle(x, y, w, h);
g2d.setColor(Color.WHITE);
g2d.fill(rect);
//Creates black rectangle
Rectangle rectG = new Rectangle(x + 2, y + 2, w - 4, h - 4);
g2d.setColor(Color.BLACK);
g2d.fill(rectG);
//Creates text
g2d.setColor(Color.WHITE);
g2d.drawString(s, x + 14, y + (h / 2) + 5);
}
//Redefines x integer
public void setPosition(int speed) {
x += speed;
}
}
private static int x;
private static int y;
static
modifier 使得整个程序只有一个 x
和 y
的副本,而不是每个 class 实例的副本.删除 static
.
private int x;
private int y;
我想设置玩家对象的速度使其 x 位置增加 2,我还想设置实体对象的速度使其 x 位置增加 3。
但是,在每个 tick 结束时,对象玩家和实体的 x 位置都增加了 5,并且两者同时移动。
我真的很感激你能分享的任何帮助。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Frame extends JPanel implements ActionListener {
// Initialize ObjectOne objects
ObjectOne obj = new ObjectOne();
ObjectOne objRM = new ObjectOne();
ObjectOne entity = new ObjectOne();
ObjectOne player = new ObjectOne();
// Create timer object
Timer tm = new Timer(10, this);
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Create menus
obj.drawGRect("Your current score: ", 0, 0, 25, g);
objRM.drawGRect("N/A.", 0, 23, 25, g);
// Create players
player.setY(2);
player.drawGRect(20, 20, g);
repaint();
entity.setY(40);
entity.drawGRect(20, 20, g);
repaint();
}
public Frame() {
setBackground(Color.BLACK);
tm.start();
}
public static void main(String[] args) {
Frame frClass = new Frame();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(800, 100);
frame.add(frClass);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
player.setPosition(2);
repaint();
entity.setPosition(3);
repaint();
//The positions for both player and entity will be incremented by 5 every tick.
System.out.println(player.getX());
}
}
对象一class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
public class ObjectOne {
private static int x;
private static int y;
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
// Creates rectangle without text
public void drawGRect(int w, int h, Graphics g) {
Graphics2D g2d = (Graphics2D) g;
//Creates white rectangle
Rectangle rect = new Rectangle(x, y, w, h);
g.setColor(Color.WHITE);
g2d.fill(rect);
//creates black rectangle
Rectangle rectG = new Rectangle(x + 2, y + 2, w - 4, h - 4);
g2d.setColor(Color.BLACK);
g2d.fill(rectG);
}
//Creates rectangle with text
public void drawGRect(String s, int x, int y, int h, Graphics g) {
int w = s.length() * 6 + 15;
Graphics2D g2d = (Graphics2D) g;
//Creates white rectangle
Rectangle rect = new Rectangle(x, y, w, h);
g2d.setColor(Color.WHITE);
g2d.fill(rect);
//Creates black rectangle
Rectangle rectG = new Rectangle(x + 2, y + 2, w - 4, h - 4);
g2d.setColor(Color.BLACK);
g2d.fill(rectG);
//Creates text
g2d.setColor(Color.WHITE);
g2d.drawString(s, x + 14, y + (h / 2) + 5);
}
//Redefines x integer
public void setPosition(int speed) {
x += speed;
}
}
private static int x;
private static int y;
static
modifier 使得整个程序只有一个 x
和 y
的副本,而不是每个 class 实例的副本.删除 static
.
private int x;
private int y;