Java 如何从另一个类访问对象变量?
我在网上四处看看,有些事情对我有所帮助,但我仍然没有让这件事起作用。我正在做一个简单的乒乓球游戏,但我想要它,所以我所有的代码都是很好的设置。所以我有一门课是关于球员的,包括球拍、电脑球拍、球、主屏幕和游戏屏幕。 例如,我的player1Paddle类将拥有该挡板的所有运动,计算机挡板类(player2)将拥有该挡板的所有运动,依此类推。 但是,当涉及到编码简单的人工智能的计算机桨(基本上按照球从左到右),我无法获得访问球的位置Java 如何从另一个类访问对象变量?,java,object,inheritance,nullpointerexception,libgdx,Java,Object,Inheritance,Nullpointerexception,Libgdx,我在网上四处看看,有些事情对我有所帮助,但我仍然没有让这件事起作用。我正在做一个简单的乒乓球游戏,但我想要它,所以我所有的代码都是很好的设置。所以我有一门课是关于球员的,包括球拍、电脑球拍、球、主屏幕和游戏屏幕。 例如,我的player1Paddle类将拥有该挡板的所有运动,计算机挡板类(player2)将拥有该挡板的所有运动,依此类推。 但是,当涉及到编码简单的人工智能的计算机桨(基本上按照球从左到右),我无法获得访问球的位置 if(player2Pong.position.x > ba
if(player2Pong.position.x > ballPong.position.x)
它带来了一个错误。现在我已经实例化了ball类,它可以工作了,但是我基本上制作了另一个球,但是运动不在那里。下面是代码的一部分,可供尝试和帮助。我知道有很多代码,可能还有一些不需要的。
在我的游戏屏幕上,我称之为Player2移动-
player2Pong.Player2Movement();
这是演奏者班的大多数学生
public class Player2Pong {
public Vector2 position;
String textureLoc;
Texture Player2Texture;
float stateTime;
Rectangle player2Bounds;
String movement;
int speed = 2;
int playerXVector = 5;
SpriteBatch batch;
//PlayerPong playerpong;
Player2Pong player2pong;
Ball ballPong;
public int paddle_player2_Width = 80;
public int paddle_player2_Height = 10;
//PlayerPong player;
public Player2Pong(Vector2 position, Ball ball){
Player2Texture = new Texture(Gdx.files.internal(("paddle_player2.png")));
this.position = position;
player2Bounds = new Rectangle(position.x,position.y,paddle_player2_Width, paddle_player2_Height);
//this.player2pong = player2pong;
this.ballPong = ball;
}
public void update(){
}
public void Player2Movement(){
player2pong = new Player2Pong(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() - (Gdx.graphics.getHeight()/8)), ballPong);
System.out.println(player2pong.position.x);
if(player2pong.position.x > ballPong.getPosition().x){
System.out.println("right");
player2pong.position.x -= playerXVector;
}
else if(player2pong.position.x < ballPong.getPosition().x){
System.out.println("right");
player2pong.position.x += playerXVector;
}
if(player2pong.position.x > Gdx.graphics.getWidth() - player2pong.getPaddle_player2_Width()-5){
player2pong.position.x -= playerXVector;
}
if(player2pong.getPosition().x < 3){
player2pong.position.x += playerXVector;
}
}
public void draw(SpriteBatch batch){
batch.draw(Player2Texture, position.x, position.y, paddle_player2_Width, paddle_player2_Height);
}
public class Player2Pong{
公共向量2位置;
线纹织物;
纹理播放器2纹理;
浮动状态时间;
矩形player2边界;
弦运动;
内速度=2;
int playerXVector=5;
喷雾批;
//PlayerPong PlayerPong;
Player2Pong Player2Pong;
乒乓球;
公共内桨2号桨宽度=80;
公共内桨2号桨高度=10;
//PlayerPong玩家;
公共球员2杆(矢量位置,球){
Player2Texture=新纹理(Gdx.files.internal(((“palle_player2.png”));
这个位置=位置;
player2Bounds=新矩形(位置.x,位置.y,桨盘2宽度,桨盘2高度);
//this.player2pong=player2pong;
this.ballPong=球;
}
public void update(){
}
公共无效播放器2移动(){
player2pong=newplayer2pong(新向量2(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()/8)),ballPong);
System.out.println(player2pong.position.x);
如果(player2pong.position.x>ballPong.getPosition().x){
System.out.println(“右”);
player2pong.position.x-=playerXVector;
}
else if(player2pong.position.xGdx.graphics.getWidth()-player2pong.getpailer\u player2\u Width()-5){
player2pong.position.x-=playerXVector;
}
if(player2pong.getPosition().x<3){
player2pong.position.x+=playerXVector;
}
}
公共作废取款(SpriteBatch批量){
批量绘制(播放器2纹理,位置.x,位置.y,桨式播放器2宽度,桨式播放器2高度);
}
以下是playScreen课程的大部分内容
public class PlayScreenPong implements Screen{
private static final Color WHITE = null;
SpriteBatch batch;
PlayerPong playerPong;
Player2Pong player2Pong;
InputProcessor inputProcessor;
Game game;
Ball ballPong;
Texture paddle_player1, paddle_player2;
ArrayList<Ball> balls;
ArrayList<Player2Pong> player2;
Iterator<Ball> ballsIterator;
Vector2 position;
ShapeRenderer borderSR, ballSR;
Sound sound;
boolean rectangles = true;
Stage PlayScreenStage;
Label label;
LabelStyle style;
BitmapFont font;
int playerXVector = 10;
int player1Score = 0;
int player2Score = 0;
public PlayScreenPong(Game game){
this.game = game;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
playerPong.update();
//player2Pong.update();
borderSR.begin(ShapeType.Line);
borderSR.rect(1, 1, Gdx.graphics.getWidth()-5, Gdx.graphics.getHeight()-4);
borderSR.setColor(1,1,1,1);
borderSR.end();
batch.begin();
ballsIterator = balls.iterator();
while(ballsIterator.hasNext()){
Ball cur = ballsIterator.next();
cur.update();
cur.draw(batch);
}
batch.draw(playerPong.getPlayerTexture(), playerPong.getPosition().x, playerPong.getPosition().y, playerPong.paddle_player1_Width, playerPong.paddle_player1_Height);
player2Pong.draw(batch);
//batch.draw(player2Pong.getPlayer2Texture(), player2Pong.getPosition().x, player2Pong.getPosition().y, player2Pong.paddle_player2_Width, player2Pong.paddle_player2_Height);
batch.end();
score();
PlayScreenStage.act();
PlayScreenStage.draw();
if(rectangles == true){
ballPong.drawRectangle();
}
player2Pong.Player2Movement();
}
@Override
public void show() {
batch = new SpriteBatch();
paddle_player1 = new Texture(Gdx.files.internal("paddle_player1.png"));
paddle_player2 = new Texture(Gdx.files.internal("paddle_player2.png"));
borderSR = new ShapeRenderer();
balls = new ArrayList<Ball>();
player2 = new ArrayList<Player2Pong>();
if(Gdx.files.local("paddle_player1.dat").exists()){
try {
playerPong = new PlayerPong(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 8), "paddle_player1.png");
playerPong.setPosition(PlayerPong.readPlayer());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("File exists, reading file");
}else{
playerPong = new PlayerPong(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 8),"paddle_player1.png");
try {
PlayerPong.savePlayer(playerPong);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Player doesnt exist, creating and saving player");
}
// player2Pong = new Player2Pong(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() - (Gdx.graphics.getHeight()/8)), playerPong);
// player2.add(player2Pong);
ballPong = new Ball(new Vector2(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2), playerPong);
balls.add(ballPong);
}
public void score(){
PlayScreenStage = new Stage();
font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
style = new LabelStyle(font, Color.WHITE);
label = new Label("Player 1: " + player1Score + " : " + player2Score + " :Player 2", style);
label.setPosition(Gdx.graphics.getWidth()/2 - label.getWidth()/2, Gdx.graphics.getHeight()/25);
label.setFontScale((float) 0.8);
PlayScreenStage.addActor(label);
Gdx.input.setInputProcessor(PlayScreenStage);
batch = new SpriteBatch();
//System.out.println(ballPong.getPosition().y);
//-------------------------------------if someone scores
if(ballPong.getPosition().y< 1){
player2Score = player2Score +1;
//oppositionScore +=1;
System.out.println("goal");
//ballPong.reset();
}
if(ballPong.getPosition().y > Gdx.graphics.getHeight()-1 -ballPong.getBallSizeY()){
player1Score = player1Score + 1;
//ballPong.reset();
}
}
公共类PlayScreenPong实现屏幕{
专用静态最终颜色白色=空;
喷雾批;
PlayerPong PlayerPong;
Player2Pong Player2Pong;
输入处理器输入处理器;
游戏;
乒乓球;
纹理拨片播放器1、拨片播放器2;
ArrayList球;
ArrayList player2;
迭代器;
矢量2位置;
Shaperender borderSR,ballSR;
声音;
布尔矩形=真;
舞台剧;
标签;
标签样式;
位图字体;
int playerXVector=10;
int player1Score=0;
int player2Score=0;
公共屏幕乒乓球(游戏){
这个游戏=游戏;
}
@凌驾
公共无效渲染(浮动增量){
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
playerPong.update();
//player2Pong.update();
borderSR.begin(ShapeType.Line);
borderSR.rect(1,1,Gdx.graphics.getWidth()-5,Gdx.graphics.getHeight()-4);
borderSR.setColor(1,1,1,1);
borderSR.end();
batch.begin();
ballsIterator=balls.iterator();
while(ballsIterator.hasNext()){
Ball cur=ballsIterator.next();
cur.update();
电流抽取(批量);
}
批量绘制(playerPong.getPlayerTexture(),playerPong.getPosition().x,playerPong.getPosition().y,playerPong.pailer\u player1\u宽度,playerPong.pailer\u高度);
player2Pong.draw(批处理);
//批量绘制(player2Pong.getPlayer2Texture(),player2Pong.getPosition().x,player2Pong.getPosition().y,player2Pong.pailer\u player2\u宽度,player2Pong.pailer\u高度);
batch.end();
分数();
剧本舞台;
PlayScreenStage.draw();
如果(矩形==真){
ballPong.drawRectangle();
}
player2Pong.Player2Movement();
}
@凌驾
公开展览({
批次=新的SpriteBatch();
palle_player1=新纹理(Gdx.files.internal(“palle_player1.png”);
palle_player2=新纹理(Gdx.files.internal(“palle_player2.png”);
borderSR=新的shaperender();
balls=新的ArrayList();
player2=新的ArrayList();
如果(Gdx.files.local(“palle\u player1.dat”).exists()){
试一试{
playerPong=newplayerpong(新矢量2(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/8),“pailer_player1.png”);
playerPong.setPosition(playerPong.readPlayer());
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
System.out.println(“文件存在,正在读取文件”);
}否则{
playerPong=newplayerpong(新矢量2(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/8),“pailer_player1.png”);
试一试{