播放器扩展抽象类而不进行渲染

时间:2016-01-20 13:45:14

标签: java inheritance

我正在尝试制作马里奥游戏。现在我正在尝试创建基本框架。我的主要游戏课程:

package com.arjav.jumper;

import java.awt.Canvas;
import java.awt.Color;  
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import com.arjav.jumper.creatures.Player;
import com.arjav.jumper.input.KeyManager;

public class Game implements Runnable{

Thread thread = new Thread(this);
JFrame frame ;
Handler handler = new Handler() ;
int frames = 0;
Canvas canvas ;
KeyManager km = new KeyManager();
Graphics g ;
Player player = new Player(50 , 50 , 32 , 32 , this);
BufferStrategy bs ;

public KeyManager getKeyManager() {
    return km; 
}

public Game(String title , int width , int height) {
    frame = new JFrame(title);
    frame.setSize(width , height);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(3);
    frame.setResizable(false);

    canvas = new Canvas();
    canvas.setMaximumSize(new Dimension(width , height));
    canvas.setMinimumSize(new Dimension(width , height));
    canvas.setPreferredSize(new Dimension(width , height));
    canvas.setFocusable(false);

    frame.addKeyListener(km);
    System.out.println("Key Manager added");
    handler.addCreature(new Player(50 , 50 , 50 , 50 , this));
    frame.add(canvas);

    frame.pack();
}

public static void main(String[] args) {
    Game game = new Game("Mario" , 720 , 360);
    game.thread.start();
    System.out.println("Started running");
}

public void run() {
    while(true) {
        tick();
        render();
        frames++ ;
        if(frames == 60) {
            System.out.println(frames + " FPS");
            frames = 0 ;
        }
        try {
            Thread.sleep(1000/60);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public void tick() {
    handler.tickCreature();
    km.tick();
}

public Player getPlayer() {
    return player;
}

public void render() {
    if(bs == null) canvas.createBufferStrategy(3);
    bs = canvas.getBufferStrategy();
    g = bs.getDrawGraphics();

    g.setColor(Color.RED);
    handler.renderCreature(g);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, 720, 360);

    bs.show();
    g.dispose();
}

}

我的生物课程:

package com.arjav.jumper.creatures;

import java.awt.Graphics;

public abstract class Creature {

public abstract void render(Graphics g);

public abstract void tick();

int x , y , width , height ;

public Creature(int x , int y , int width , int height) {
    this.x = x ;
    this.y = y ;
    this.width = width ;
    this.height = height ;
}

}

玩家:

package com.arjav.jumper.creatures;

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

import com.arjav.jumper.Game;

public class Player extends Creature{

int x , y , width , height , velX = 0 , velY = 0 ;
Game game ;

public Player(int x , int y , int width , int height , Game game) {
    super(x , y , width , height);
    this.game = game ;
}

@Override
public void render(Graphics g) {
    g.setColor(Color.RED);
    g.fillRect(x , y , width , height);
    System.out.println("Player rendered");
}

public void tick() {

    if(game.getKeyManager().left) x -= 3 ;
    if(game.getKeyManager().right) x += 3;
    System.out.println("Player ticked");
    System.out.println(x);

}

public void setVelX(int velX) {
    this.velX = velX;
}

public void setVelY(int velY) {
    this.velY = velY;
}



}

它打印播放器并打勾,并且它的x增加和减少。所以唯一不对的是红框不渲染。

处理程序类:

package com.arjav.jumper;

import java.awt.Graphics;
import java.util.LinkedList ;

import com.arjav.jumper.creatures.Creature;

public class Handler {

public LinkedList<Creature> creature = new LinkedList<Creature>();



public void addCreature(Creature c) {
    creature.add(c);
}

public void removeCreature(Creature c) {
    creature.remove(c);
}

public void tickCreature() {
    for(int i = 0 ; i <  creature.size() ; i ++) {
        Creature c = creature.get(i);
        c.tick();
    }
}

public void renderCreature(Graphics g) {
    for(int i = 0 ; i <  creature.size() ; i ++) {
        Creature c = creature.get(i);
        c.render(g);
    }
}

}

1 个答案:

答案 0 :(得分:1)

这里的问题是子类具有与其父类同名的变量。即: int x, y, width, height

创建玩家时,玩家的构造函数将x,y,width,height委托给父生物类。此生物构造函数根据需要将这些值保存到类成员中。

但是,播放器类包含带名称的变量 x, y, width, height 但这些基本上是不同的变量,只是名称相同。所以现在,当您在播放器中引用任何这些变量名称时,您将获得播放器的变量(未初始化,因此为0),但您打算从该生物中获取变量。

要解决此问题,只需确保您在子类和父母之间没有任何重复的变量名称,因为它们有效地隐藏了#34;父变量。 Java并不认为这是一个错误,但它可能会导致很多混乱。

有关详细信息,请参阅hiding variables