gpt4 book ai didi

java - 当我在 "player object"上使用名为 movePosition() 的方法时,它会更改由该类创建的所有其他对象的 x 位置

转载 作者:行者123 更新时间:2023-12-01 10:37:47 24 4
gpt4 key购买 nike

我想设置玩家对象的速度以将其 x 位置增加 2 倍,并且我还想设置实体对象的速度以将其 x 位置增加 3 倍。

但是,在每个刻度结束时,玩家和实体对象的 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());
}
}

ObjectOne 类:

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使得整个程序中只有一个 xy 副本,而不是该类的每个实例都有一个副本。删除静态

private int x;
private int y;

关于java - 当我在 "player object"上使用名为 movePosition() 的方法时,它会更改由该类创建的所有其他对象的 x 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34572758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com