gpt4 book ai didi

java - 无法生成一排敌人,仅生成一个敌人 :\

转载 作者:行者123 更新时间:2023-12-02 04:07:24 25 4
gpt4 key购买 nike

Controller 类:

 package com.game.src.main;

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

public class Controller
{
private LinkedList<Enemy> e = new LinkedList<Enemy>();

Random r = new Random();

Enemy TempEnemy = null;

Game game;
Textures tex;

public Controller(Game game, Textures tex)
{
this.game = game;
this.tex = tex;

**for(int x = 0; x < (Game.WIDTH * Game.SCALE); x += 21)
addEnemy(new Enemy(x, 200, tex));**
}

public void tick()
{
for(int i = 0; i < e.size(); i++)
TempEnemy = e.get(i);

TempEnemy.tick();
}

public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++)
TempEnemy = e.get(i);

TempEnemy.render(g);
}

public void addEnemy(Enemy block)
{
e.add(block);
}

public void removeEnemy(Enemy block)
{
e.remove(block);
}
}

我的构造函数中的 for-Loop 没有创建一行敌人。它应该做的是渲染一排敌人 Sprite ,但它会渲染屏幕最右角的最后一个。 :\敌人等级如下:

package com.game.src.main;

import java.awt.Graphics;

public class Enemy
{
private int x = 0;
private int y = 0;
private Textures tex = null;

public Enemy(int x, int y, Textures tex)
{
this.x = x;
this.y = y;
this.tex = tex;
}

public void tick()
{
y+= 3;
if(y >= (Game.HEIGHT * Game.SCALE))
y = 0;
}

public void render(Graphics g)
{
g.drawImage(tex.spike, x, y, null);
}
}

纹理类别:

    package com.game.src.main;

import java.awt.image.BufferedImage;

public class Textures
{
public BufferedImage player, spike;

private SpriteSheet ss;

public Textures(Game game)
{
ss = new SpriteSheet(game.getSpriteSheet());
getTextures();
}

private void getTextures()
{
player = ss.grabImage(1, 1, 44, 65);
spike = ss.grabImage(1, 3, 21, 65);

}

public void setUp()
{
player = ss.grabImage(3, 1, 43, 65);
}

public void setDown()
{
player = ss.grabImage(1, 1, 43, 65);
}

public void setLeft()
{
player = ss.grabImage(5, 1, 43, 65);
}

public void setRight()
{
player = ss.grabImage(7, 1, 43, 65);
}
}

最佳答案

括号!

改变这个

    public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++)
TempEnemy = e.get(i);

TempEnemy.render(g);
}

到此

    public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++){
TempEnemy = e.get(i);
TempEnemy.render(g);
}
}

PS:即使您只需要执行一行,也请始终为循环、if 语句等编写括号。你可以避免这种挣扎。

<小时/>

此外,您不需要“TempEnemy”,此代码的作用相同:

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

关于java - 无法生成一排敌人,仅生成一个敌人 :\,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34135092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com