gpt4 book ai didi

java - "x cannot be resolved or is not a field"。我怎样才能解决这个问题?加工

转载 作者:行者123 更新时间:2023-11-30 05:43:40 27 4
gpt4 key购买 nike

我的 Enemy 变量在 GameScene 函数中不“存在”。我正在尝试制作一个正在处理的游戏,但由于某种原因,当我尝试访问一个类时,我收到错误消息“x 无法解析或不是一个字段”,我将在下面发布我的代码以及所有类。

Scenes s = Scenes.Title; //this array stores the three scenes
Enemy [] en = new Enemy[100]; //spawns the monsters
Player p;
ArrayList<Bullet> bulletList = new ArrayList<Bullet>();

PImage background;
PImage player;
PImage enemy;

boolean isDead;

void setup() {
size(500, 500);
textSize(18);
text("", 0, 0);
background = loadImage("backgrounddetailed5.png");
player = loadImage("survivor-idle_handgun_0_up.png");
enemy = loadImage("Monster.png");



for (int n = 0; n < en.length; n++) {
en[n] = new Enemy();
}

p = new Player(width/2, height/2);
}

void draw() {
background(background);
if (s == Scenes.Title) {
TitleScene();
} else if (s == Scenes.Gameplay) {
GameScene(10);
if (p.all_enemies_are_dead == true)
{
Enemy [] wave2 = new Enemy[20];
p.all_enemies_are_dead = false;
p.total_enemy_count = 20;
GameScene(20);
}
}

enum Scenes {
Title,
Gameplay, //1
Win, //2
Lose,
}

下面是敌人的等级:

class Enemy {
float x;
float y;
float size;
float speed = random(1, 1.5);


Enemy() {
size = 45;
x = random(0, width);
while (y < 0 && y < height ) { //this is the spawns of the enemies, i tried doing it
y = random(0, -height * 20); // like above the screen in game.
x = random(0, width);
}
}

//this function is the Ai of the enemy basically follows the player
void follow() {
if (x > p.loc.x)
{
x -= speed;
}

if (x < p.loc.x)
{
x += speed;
}

if (y > p.loc.y)
{
y -= speed;
}

if (y < p.loc.y)
{
y += speed;
}
}
}

这是我的场景类,它加载游戏开始的场景:

void TitleScene(){
fill(255);
text("HIT SPACE TO START",width/2 - 100,height/2);
if (keyPressed && key == ' '){
s = Scenes.Gameplay;
}
}

void GameScene(int number_of_enemies){

background(background);
fill(255);
text("Wave 1",10,50);
Enemy [] en = new Enemy[number_of_enemies];
for (int i =0; i < number_of_enemies; i++) {

image(enemy, en.x, en.y, en.size, en.size);
en.follow();
if (dist(p.loc.x + p.sz/2.5, p.loc.y + p.sz/2.5, en.x, en.y) < (p.sz + en.size)/2.5) {
p.loc.x = 250;
p.loc.y = 450; //restarts game
isDead = true;
}
for (Bullet b : bulletList) {
if (dist(en.x + en.size/2, en.y + en.size/2, b.p.x, b.p.y) < (en.size + b.size)/2) {
en.size = 0; //despawns monsters
p.total_enemy_count--;
if ( p.total_enemy_count <= 0 ) {
p.all_enemies_are_dead = true;
}
}
}
if (dist(en.x + en.size/2, en.y + en.size/2, en.x, en.y) < (en.size + en.size)/2) {
en.speed *= 1; //makes monsters collide with each other
}
}
for (int n = 0; n < bulletList.size(); n++) {
Bullet b = bulletList.get(n);
b.run();
}
p.run();

}

void WinScene(){
background(background);
fill(255);
text("Hit R to play again",width/2 - 100,height/2);
if (keyPressed && key == 'r')
s = Scenes.Title;
}
void LostScene(){
background(background);
fill(255);
text("You are dead, hit R to play again",width/2 - 100,height/2);

}

我遇到的错误是在 GameScene 函数中,其中显示“en.x、en.y 和 en.size”...出于某种原因,我不知道为什么 en.""之后的每个变量都是这样不存在。

最佳答案

en 是一个类型数组 Enemy[]

因此,每次引用 en 时,您都应该引用数组中要提取的索引(例如 en[i])变量,例如 en.xen.yen.size。这些应该是 en[i].xen[i].yen[i].size

关于java - "x cannot be resolved or is not a field"。我怎样才能解决这个问题?加工,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55212015/

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