如何编写检查数组索引的 if 语句?

发布于 2025-01-10 00:40:32 字数 1215 浏览 1 评论 0原文

我有一个名为 setMonster() 的方法,位于名为“main”的包中名为“AssetSetter”的类中,它根据来自名为“MON_GreenSlime”的不同包的另一个类中的信息创建新的怪物。

AssetSetter 类内部的 SetMonster() 看起来像这样(实际上列表要长得多,但你得到了夹具):

public void setMonster(){

        gp.monster[0] = new MON_GreenSlime(gp);
        gp.monster[0].worldX = gp.tileSize * 26;
        gp.monster[0].worldY = gp.tileSize * 20;

        gp.monster[1] = new MON_GreenSlime(gp);
        gp.monster[1].worldX = gp.tileSize * 22;
        gp.monster[1].worldY = gp.tileSize * 18;

        gp.monster[2] = new MON_GreenSlime(gp);
        gp.monster[2].worldX = gp.tileSize * 24;
        gp.monster[2].worldY = gp.tileSize * 17;

    }

因此,在 MON_GreenSlime 类内部,我有一个名为 setAction() 的方法,它将 AI 移动设置为怪物。在这个 setAction() 内部,我想做一个 if 语句,检查 m 中 gp.monster[] 的索引作为其条件。沿着这个思路的东西(如果这可行的话)

if (gp.monster[index] index == 0){
  //do something
} 

else if (gp.monster[index] index == 1){
  //do something
}

//And so on... (The index is just as an example) Like I explained, I don't know how I would write such an if statement

单个怪物的AI移动是由它的默认x和y位置控制的,因此当我创建超过1个怪物时,我觉得我需要某种if或switch语句来处理每个怪物单独的数组索引。

如果您知道一个简单的解决方案,请告诉我:)我对 Java 还很陌生,所以如果它很明显,请对我宽容一些

I have this method called setMonster(), in a class called 'AssetSetter' in a package called 'main', which creates new monsters based on information in another class from a different package called 'MON_GreenSlime'.

SetMonster() inside of the AssetSetter class looks like this(the list is much longer in reality but you get the jig):

public void setMonster(){

        gp.monster[0] = new MON_GreenSlime(gp);
        gp.monster[0].worldX = gp.tileSize * 26;
        gp.monster[0].worldY = gp.tileSize * 20;

        gp.monster[1] = new MON_GreenSlime(gp);
        gp.monster[1].worldX = gp.tileSize * 22;
        gp.monster[1].worldY = gp.tileSize * 18;

        gp.monster[2] = new MON_GreenSlime(gp);
        gp.monster[2].worldX = gp.tileSize * 24;
        gp.monster[2].worldY = gp.tileSize * 17;

    }

So inside of the MON_GreenSlime class I have a method called setAction() which sets AI movement to the monster. Inside this setAction() I want to make an if statement that checks the index of the gp.monster[] from the m as its condition. Something along the lines of this(if this would work)

if (gp.monster[index] index == 0){
  //do something
} 

else if (gp.monster[index] index == 1){
  //do something
}

//And so on... (The index is just as an example) Like I explained, I don't know how I would write such an if statement

The individual monsters AI movement is controlled by it's default x and y location, therefore when I create more than 1 monster I feel like I need some sort of if or switch statement to treat each array index individually.

Someone please let me know if you know of a simple solution :) I'm pretty new to Java, so go easy on me if its obvious

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文