简体   繁体   English

如何将类中的对象存储在数组中

[英]How to store objects from a class in an array

Hi so I have been tasked with creating a "falling game" and I have classes for each thing such as the user and the enemy.嗨,所以我的任务是创建一个“坠落游戏”,并且我为每个事物(例如用户和敌人)设置了课程。 I was wondering how would I go about storing the enemies in an array.我想知道如何将敌人存储在数组中。 I have done some research and trial and testing and it seems like I need to use an array of objects to store these in an array and call them from the array.我已经做了一些研究、试验和测试,似乎我需要使用一个对象数组来将它们存储在一个数组中并从数组中调用它们。 Am I right in believing so?我这样相信是对的吗? I tried storing them in an array for PImage but that did not work I am not sure how to create an array of objects.我尝试将它们存储在 PImage 的数组中,但这不起作用我不确定如何创建对象数组。 Here is my class for the enemy which is called salad lol:这是我的敌人课,叫做沙拉哈哈:

class Salad {
 float x,y;
 float speedX, speedY; //declaring variables
 PImage saladImage;

 Salad(int x, int y, int speedY) {
  this.x = x;
  this.y = y;
  this.speedY = speedY;
  saladImage = loadImage("salad.png");
  saladImage.resize (60, 52);  
 } //end of salad

 void move() {
   y=y-speedY;
   float stepY = random(-5,5);
   y = y + (int)stepY;

   float rand = random(25,475);
   int intRand = int(rand);   

   if (this.y < 0) {
    this.y = 900; // once the salads y is less than 0 they restart at 900
    this.x = intRand;
    speedY = speedY + 0.5;
   }
 } //end of void move

 //draw a salad
 void render()
 {
image(saladImage,x,y);

} //end of void render 

 void update() {
  move();
  render();
 }
}// end of alien class

Here is my main class and i wanted to store the burger and salad class in the array if that was possible:这是我的主类,如果可能的话,我想将汉堡和沙拉类存储在数组中:

PImage background;
PImage MenuBackground;
int y=0;//global variable background location
final int End = 0;
final int Active = 1;
final int Menu = 2;
int gameMode = Menu;
int score = 0;
int lives = 3;
Boolean BurgerCollisionInProgress = false;
Boolean BurgerCollisionInProgress2 = false;


Salad salad1;
Salad salad2;
Salad salad3;
Homer user1;
Burger Burger;


public void settings() {
 size(500,1000); //setup size of canvas
}

void menu() {
 background = loadImage("spaceBackground.jpg"); //image used for background
 background.resize(500,1000); //resizes the background
 gameMode = Active; 

float rand = random(25,475);
int intRand = int(rand);

float rand2 = random(25,475);
int intRand2 = int(rand2); 

float rand3 = random(25,475);
int intRand3 = int(rand3); 

float rand4 = random(25,475);
int intRand4 = int(rand4); 


 user1 = new Homer(250,100);  //declares new defender as user1
 Burger = new Burger(intRand,900,2);
 salad1 = new Salad(intRand2,900,3);
 salad2 = new Salad(intRand3,900,3);
 salad3 = new Salad(intRand4,900,3); //3 aliens declared with their x and y position and their speed they move at
 draw();
}


void setup() {
  if(gameMode == 2) {    
    MenuBackground = loadImage("simpMenu.png");
    MenuBackground.resize(540,1000);
    image(MenuBackground, 0, y);
    textAlign(CENTER);
    textSize(40);
    fill(252, 3, 3);
    text("Press 'p' to play", 250,500);     
  } 
}


void draw () {  
  if (gameMode == Active) {        
    if(crash() == false) {
      drawBackground();//calls the drawBackground method
      textSize(32);
      fill(22,100,8);
      text("Score: " + score,75,40); 
      text("Lives: " + lives,75,80);
      salad1.update();//calls the update method which holds the move and render methods for alien
      salad2.update();
      salad3.update();
      user1.render();//calls the update method which holds the move and render methods for user
      Burger.update();//calls the update method which holds the move and render methods for burger

      if(Bcrash() == true && BurgerCollisionInProgress == false) {
      score = score+1;
      BurgerCollisionInProgress = true;
      Burger.y = 900;
      float rand = random(25,475);
      int intRand = int(rand);
      Burger.x = intRand;
      }

      if(Bcrash() == false) {
      BurgerCollisionInProgress = false;
      }


      if(crash() == true && BurgerCollisionInProgress2 == false) {
        if (lives < 1) {   gameMode = End;
            textSize(28);
            fill(22,100,8);
            text("Game Over, press 'r' to restart",50,200);
        }
        else {
          lives = lives - 1;
          BurgerCollisionInProgress2 = true;
          menu();
          //salad1.update();
          //salad2.update();
          //salad3.update();
        }
        if (crash() == false) {
          BurgerCollisionInProgress2 = false;
      }
     }      
    }
  }
}

void drawBackground() {
 image(background, 0, y); //draw background twice adjacent
 image(background, 0, y-background.width);
 y -=2;
 if(y == -background.width)
 y=0; //wrap background
}


void keyPressed() //allows the user to press the up and down keys to move the defender
{ 
  if(key == CODED) {
     if (keyCode == UP) {
       user1.y = user1.y - 7;
     }

     else if (keyCode == DOWN) {
       user1.y = user1.y + 7;
     }

     else if (keyCode == LEFT) {
       user1.x = user1.x - 7;
     }

     else if (keyCode == RIGHT) {
       user1.x = user1.x + 7;
     }

  } //key   

  if(key=='r') {
       menu();
     }

     if(key=='p') {
       menu();
     }
}


  boolean crash() {
    if(user1.crash(salad1)) {
      return true;
    }
    if(user1.crash(salad2)) {
      return true;
    }
    if(user1.crash(salad3)) {     
      return true;
    }
    return false;
  }


  boolean Bcrash() {
    if(user1.crash(Burger))
    {      
      return true;
    }
    return false;
  }

You can create an interface called Food and implement in Salad and Burger and Create a list of Food您可以创建一个名为Food的接口并在Salad and Burger并创建一个Food列表

public interface Food {}
public class Salad implements Food {}
public class Salad implements Burger {}

and Create a list by:并通过以下方式创建列表:

List<Food> list = new ArrayList<>();
Burger burger = new Burger();
Salad salad = new Salad();

list.add(burger);
list.add(salad);

, And you can check them when retrieving by , 你可以在检索时检查它们

Food food = list.get(0);
if (food instanceof Salad) {
  Salad s = (Salad) food;
  // do somesthing
} else if (food instanceof Burger) {
  Burger b = (Burger) food;
  // do somesthing
}

Here's the syntax: Let's say you want an ArrayList of Salad:语法如下: 假设您想要一个沙拉的 ArrayList:

ArrayList<Salad> mySalads = new ArrayList<Salad>();

If you have only one image for your salads you may want to load it into a global variable just once and use it from there instead of loading it into memory for each instance of Salad, but that's another thing.如果您的沙拉只有一个图像,您可能只想将它加载到全局变量中一次并从那里使用它,而不是将它加载到每个沙拉实例的内存中,但这是另一回事。

Your project looks interesting, have fun!你的项目看起来很有趣,玩得开心!

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

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