簡體   English   中英

我如何從另一個類訪問對象變量?

[英]How do I gain access to an objects variable from another class?

我已經在網上四處張望,有些事情有所幫助,但我仍然無法使它正常工作。 我正在制作一個簡單的乒乓球游戲,但我想要它,因此我的所有代碼都已確定。 所以我有一個關於player1球拍,計算機球拍,球,主屏幕以及比賽屏幕的課程。 因此,例如,我的player1Paddle類將具有該板塊的所有移動,而計算機槳葉類(player2)將具有該板的所有移動,依此類推。 但是,在編碼計算機鍵盤的簡單AI時(基本上從左到右跟隨球),我無法進入球的位置

if(player2Pong.position.x > ballPong.position.x)

它帶來一個錯誤。 現在,我已經實例化了球類,並且它起作用了,但是我基本上制作了另一個球,但是運動不存在。 這是部分代碼,可以嘗試並提供幫助。 我知道有很多代碼,可能還不需要。 在我的playScreen屏幕上,我稱player2movement-

player2Pong.Player2Movement();

這是大多數player2pong類的人。

 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);            
    }

這是大部分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();
        }

    }

就像我之前說的那樣,它有點長了,但是我要做的是對AI在player2pong類中的移動,在ball類中的球的移動以及在player類中的球員的移動進行排序,然后調用渲染方法中的方法或播放屏幕類中的顯示方法。 隨意問任何問題。 我知道這是一個簡單的答案,但我是新手:(開始在某個地方

好的,所以,我可能對此有誤,但是值得一試。

首先,您不需要每次調用PlayerMovement()函數時都創建一個新的Player2pong對象,您已經有了一個:P

其次,您永遠都不player2Pong屬性分配任何東西。 因此,當您嘗試在其上調用draw()時,沒有任何東西可以調用它!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM