我正在为我的编程类制作一个简单的Java applet游戏,但遇到了另一个问题。在游戏中,用户可以使用向上和向下箭头在y轴上的四个“车道”之间移动。空间将绘制子弹在车道上沿x轴移动的图像,并在子弹到达x轴上的特定点时将其移除。现在,我正在努力做到这一点,以便一次可以在屏幕上显示多个子弹。

我一直在看MyBringBack的YouTube系列节目,讨论Java Applets中的游戏开发,并找到了解决方案。我知道我应该在单独的类中将“播放器”作为一个对象,使我可以轻松地调用它的多个实例。问题在于,在YouTube系列中,他在使用图像时正在使用Shapes。我知道,如果我看着他这样做,应该不会太困难,但是我很难确定Object类中还需要哪些方法和变量。

我想,因为我必须为子弹做这件事,所以我也可能对其他对象(“玩家”,“牛”,“ fastZombie”,“ tankyZombie”,normalZombie”和“ bloodSplat”)也是如此。将涉及“玩家”的所有内容放在单独的类中,但是当我这样做时,ZombieAttackMain现在无法访问原始ZombieAttackMain类中使用的变量,而新类也无法访问ZombieAttackMain中的变量。我将发布ZombieAttackMain的原始代码,如果有人有任何建议,将不胜感激。谢谢!

编辑:在ZombieAttackMain中,我无法再访问PLAYER_X_POSITION和playerYPosition。在我创建的播放器类中,它无法访问POSITION_1_Y,POSITION_2_Y,POSITION_3_Y或POSITION_4_Y。

码:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ZombieAttackMain extends Applet implements Runnable, KeyListener{
    //all the comments with $ I removed and put in another class called Player and could not be accessed by ZombieAttackMain
    //all the comments with # I copied and put in Player class
    //all the comments with % could not be accessed by Player class
    private Image background, normalZombie, fastZombie, tankyZombie,bullet, bloodSplat, cow1, cow2, cow3, cow4;
    private Image player;      //$
    private Graphics bufferGraphics;
    private Image offScreen;

    private final int POSITION_1_Y = 100;    //%
    private final int POSITION_2_Y = 255;    //%
    private final int POSITION_3_Y = 400;    //%
    private final int POSITION_4_Y = 550;    //%
    private final int PLAYER_X_POSITION = 250; //$
    private final int NORMAL_ZOMBIE_DX = -2;
    private final int FAST_ZOMBIE_DX = -3;
    private final int TANKY_ZOMBIE_DX = -1;
    private final int BULLET_DX = 10;
    private final int BULLET_STARTING_X_POSITION = 405;

    private int zombieStartingXPosition = 1000;
    private int playerYPosition = POSITION_3_Y; //$
    private int zombieYPosition;
    private int bulletXPosition, bulletYPosition;
    private int bloodSplatX, bloodSplatY;

    private boolean showBullet;
    private boolean showFastZombie;
    private boolean showTankyZombie;
    private boolean showNormalZombie;
    private boolean showCow;


    public void init(){
        setSize(1100, 700);
        normalZombie = getImage(getCodeBase(), "NormalZombie.png");
        tankyZombie = getImage(getCodeBase(), "TankyZombie.png");
        fastZombie = getImage(getCodeBase(), "FastZombie.png");
        player = getImage(getCodeBase(), "Player.png");
        bullet = getImage(getCodeBase(), "Bullet.png");
        bloodSplat = getImage(getCodeBase(), "BloodSplat.png");
        background = getImage(getCodeBase(), "Background.jpg");
        cow1 = getImage(getCodeBase(), "Cow.png");
        cow2 = getImage(getCodeBase(),"Cow.png");
        cow3 = getImage(getCodeBase(),"Cow.png");
        cow4 = getImage(getCodeBase(),"Cow.png");
        addKeyListener(this);
    }
    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }

    public void run() {
        while(true){
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public void update(Graphics g){
        if(offScreen == null){
            offScreen = createImage(this.getWidth(), this.getHeight());
            bufferGraphics = offScreen.getGraphics();
        }
        bufferGraphics.setColor(getBackground());
        bufferGraphics.fillRect(0,0,this.getWidth(),this.getHeight());

        bufferGraphics.setColor(getForeground());
        paint(bufferGraphics);

        g.drawImage(offScreen,0,0,this);
    }

    public void paint(Graphics g){
        g.drawImage(background,0,0,null);
        g.drawImage(cow1,-15,115,null);
        g.drawImage(cow2,-15,255,null);
        g.drawImage(cow3,-15,400,null);
        g.drawImage(cow4,-15,550,null);
        g.drawImage(normalZombie,zombieStartingXPosition,100,null);
        g.drawImage(tankyZombie,zombieStartingXPosition,400,null);
        g.drawImage(fastZombie,zombieStartingXPosition,255,null);
        g.drawImage(player,PLAYER_X_POSITION,playerYPosition, null); //$
        g.drawImage(bloodSplat,bloodSplatX,bloodSplatY,null);
        if(showBullet){
                    g.drawImage(bullet,bulletXPosition,bulletYPosition,this);
            bulletXPosition += BULLET_DX;
            if(bulletXPosition > 1100){
                showBullet = false;
            }
        }
    }

    public void moveUp(Image player){         //$
        if(playerYPosition == POSITION_2_Y){  //$
            playerYPosition = POSITION_1_Y;   //$
        }                                     //$
        if(playerYPosition == POSITION_3_Y){  //$
            playerYPosition = POSITION_2_Y;   //$
        }                                     //$
        if(playerYPosition == POSITION_4_Y){  //$
            playerYPosition = POSITION_3_Y;   //$
        }                                     //$
    }                                         //$

    public void moveDown(Image player){       //$
        if(playerYPosition == POSITION_3_Y){  //$
            playerYPosition = POSITION_4_Y;   //$
        }                                     //$
        if(playerYPosition == POSITION_2_Y){  //$
            playerYPosition = POSITION_3_Y;   //$
        }                                     //$
        if(playerYPosition == POSITION_1_Y){  //$
            playerYPosition = POSITION_2_Y;   //$
        }                                     //$
    }                                         //$

    public void spawnBullet(){
        if(showBullet){
            return;
        }
        showBullet = true;
        bulletXPosition = BULLET_STARTING_X_POSITION;
        bulletYPosition = playerYPosition +83;
    }

    public void keyPressed(KeyEvent e) {     //#
        switch(e.getKeyCode()){              //#
        case KeyEvent.VK_UP:                 //$
            moveUp(player);                 //$
            break;                          //$
        case KeyEvent.VK_DOWN:              //$
            moveDown(player);               //$
            break;                          //$
        case KeyEvent.VK_SPACE:
            spawnBullet();
            break;
        }
    }

    public void keyReleased(KeyEvent e) {   //#

    }                                       //#


    public void keyTyped(KeyEvent e) {      //#

    }                                       //#

    public void stop() {

    }

    public void destroy() {

    }
}

最佳答案

对于无法访问的变量,信息量有限。我确实同意notArefill,因为他相信您不会以正确的方式访问变量。
示例(如果说您有单独的Zombie类):

public class Zombie{
private double x;
public void setX(double x){       // Setter
this.x = x;
}
public double getX(){             // Getter
return x;
}
}


然后,当您需要引用Zombies类中的X字段时,您将执行以下操作
在您的主目录或需要设置/获取它的任何地方:

Zombie zombie = new Zombie();   // Create a new instance of your zombie or if available                        call existing zombi(say if stored in some sort of a collection)
zombie.setX(140.13);            // To set the X coordinate
zombie.getX();                  // To retrieve the X coordinate


希望能有所帮助。

07-24 09:22