如何让多个类彼此通信

时间:2013-12-06 23:59:32

标签: java class object applet

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

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

我认为既然我必须为子弹做这件事,我也可以做其他对象(“玩家”,“牛”,“fastZombie”,“tankyZombie”,“normalZombie”和“bloodSplat”)。我通过尝试将处理“播放器”的所有内容放在一个单独的类中来开始,但是当我这样做时,ZombieAttackMain现在无法访问原始ZombieAttackMain类中使用的变量,并且新类无法访问ZombieAttackMain中的变量我将发布ZombieAttackMain的原始代码,如果有人有任何建议,我们将不胜感激。谢谢!

编辑:在ZombieAttackMain中,我无法再访问PLAYER_X_POSITION和playerYPosition。在我创建的Player类中,它无法访问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() {

    }
}

2 个答案:

答案 0 :(得分:1)

你需要将所有这些类分开是正确的。无法访问变量的原因可能是因为您将它们设置为私有。因此,您应该使用“getters”访问它们。如果你不知道如何为你的课程制作getter,如果你正在使用eclipse,你可以去源 - >生成getter和setter。因此,例如,如果您的Bullet类具有bulletXposition,那么在创建Bullet对象时从另一个类开始,假设您将其命名为“bullet”,并且可以调用bullet.getBulletXPosition等等。在我看来,你应首先阅读基础知识然后尝试制作游戏。

答案 1 :(得分:0)

对于您无法访问的变量,给出的信息量有限。我同意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

希望有所帮助。