我正在尝试制作Mario游戏。现在,我正在尝试创建基本框架。我的主要游戏课:

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

}

最佳答案

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

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

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

要解决此问题,只需确保子类和父级之间没有任何重复的变量名,因为它们可以有效地“隐藏”父级变量。 Java不会将其视为错误,但是会引起很多混乱。

有关更多信息,请参见hiding variables

关于java - 播放器扩展了一个抽象类而未呈现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34901757/

10-12 20:34