简体   繁体   English

Java-AWT:使对象指向其他对象

[英]Java - AWT: Making objects point to other objects

I am a begginer java programmer for AWT, and I'm wondering how I make Enemy() point to Player() . 我是AWT的入门Java程序员,我想知道如何使Enemy()指向Player() Here is my code so far: 到目前为止,这是我的代码:

Player.java: Player.java:

import java.awt.Color;
import java.awt.Graphics;
public class Player extends GameObject {
    public Player(int x, int y, Color color) {
       super(x, y, color);
    }

    @Override
    public void tick() {
        x += velX;
        y += velY;
    }

    @Override
    public void render(Graphics g) {
        g.setColor(color);
        g.fillRect(x, y, 32, 32);
    }   
}

Enemy.java: Enemy.java:

import java.awt.Color;
import java.awt.Graphics;

public class Enemy extends GameObject {
    public Enemy(int x, int y, Color color) {
        super(x, y, color);
    }

    @Override
    public void tick() {
        x += velX;
        y += velY;
    }

    @Override
    public void render(Graphics g) {
        g.setColor(color);
        g.fillRect(x, y, 32, 32);
    }   
}

So how can I begin moving Enemy() towards Player() so the game actually works? 那么,如何开始将Enemy()移向Player()以便游戏真正起作用?

You need a reference to a GameObject somehow. 您需要以某种方式引用GameObject。 You're missing that in your design. 您在设计中缺少了这一点。 Somewhere, in these two classes, or another class, you should be keeping track of a reference to a GameObject variable that you can then just set to be the enemy player. 在这两个类或另一个类中的某个地方,您应该跟踪对GameObject变量的引用,然后可以将其设置为敌方玩家。 There's no way to arbitrarily make one class into another. 没有办法随意地将一个班级变成另一个班级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM