Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在F250.java类中有两个循环不会运行_Java_Arrays_For Loop_Constructor_Slick2d - Fatal编程技术网

在F250.java类中有两个循环不会运行

在F250.java类中有两个循环不会运行,java,arrays,for-loop,constructor,slick2d,Java,Arrays,For Loop,Constructor,Slick2d,java类是一个gun对象,有两种方法可以绘制和更新枪中的子弹。我在F250.java类中创建了一个Bullet类和一个Bullet数组,但是Draw和Update方法中的for循环不会执行for循环中的代码来生成Bullet。我相信它和Bullet类有关,特别是Bullet类构造函数中的this.bulletImage变量。在Slick2d中,图像必须初始化,当我在F250.java类中创建bullet对象以在bullet.java类中初始化时,我无法获得作为参数的图像,因为它不是静态的或so

java类是一个gun对象,有两种方法可以绘制和更新枪中的子弹。我在F250.java类中创建了一个Bullet类和一个Bullet数组,但是Draw和Update方法中的for循环不会执行for循环中的代码来生成Bullet。我相信它和Bullet类有关,特别是Bullet类构造函数中的this.bulletImage变量。在Slick2d中,图像必须初始化,当我在F250.java类中创建bullet对象以在bullet.java类中初始化时,我无法获得作为参数的图像,因为它不是静态的或soemthing idk。也许我错了,这不是问题所在,但如果是的话,我不知道如何解决这个问题。最重要的是两个for循环不会在F250.java类中执行,感谢您阅读所有这些内容,感谢您回答这个问题的人我非常感谢您抽出时间为我解决这个繁琐的问题,谢谢您

package Guns.Human;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import Bullet.Bullet;
import GUI.Clicker;
import MOB.Player;




public class F250 {

static Clicker clicker = new Clicker();

//Gun Preferences
static String gun = "F250";

//Images
 static Image F250;
 static Image fmjBullet;

//Image Coordinates
 static int F250_X = Player.getX() + 0;
 static int F250_Y = Player.getY() + 20;
 static int bulletX = F250_X + 32;
 static int bulletY = F250_Y;

//Animations
 static Animation charIdleGun;
 static Animation shootingGun;

//Animation Rates
 private static int[] idle = {120,120,120};
 private static int[] gunRecoil ={10,10};

 //Booleans
 private static boolean holding = true;
 private static boolean gunIdle = true;
 private static boolean shooting = false;


//Bullet Array Parameters
private static Image yellowBulletImage; 
private static int bulletStartX = F250_X;
private static int bulletStartY = F250_Y;
private static int destX = clicker.getMouseX();
private static int destY = clicker.getMouseY();
private static int bulletSpeed = 20;
private static Bullet[] bullets = new Bullet[42];
private static Bullet bullet;   




public static void init(GameContainer gc, StateBasedGame sbg) throws SlickException {


    //Images
    F250 = new Image("res/Images/Guns/Human/F250/Normal Gun/F250.png",false,F250.FILTER_NEAREST);

    //bullet image
    yellowBulletImage = new Image("res/Images/Guns/Human/F250/Bullets/bullet1.png",false,yellowBulletImage.FILTER_NEAREST);

    //Bullet
    bullet = new Bullet(gun,yellowBulletImage,bulletStartX,bulletStartY,destX,destY,bulletSpeed);



    //Image Arrays
    Image[] characterIdleGunPics = {new Image("res/Images/Guns/Human/F250/Animation/F250_1.png",false,F250.FILTER_NEAREST), new Image("res/Images/Guns/Human/F250/Animation/F250_2.png",false,F250.FILTER_NEAREST),new Image("res/Images/Guns/Human/F250/Animation/F250_2.png",false,F250.FILTER_NEAREST)};
    Image[] shootingGunPics = {new Image("res/Images/Guns/Human/F250/Animation/F250_shot.png",false,F250.FILTER_NEAREST), new Image("res/Images/Guns/Human/F250/Animation/F250_1.png",false,F250.FILTER_NEAREST)};

    //Animations
    charIdleGun = new Animation(characterIdleGunPics,idle,false);
    shootingGun = new Animation(shootingGunPics,gunRecoil,false);

}



public static void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {


    Input input = gc.getInput();



    //make inventory later and make holding this gun conditional if clicked on in the inventory


    if(gunIdle == true){

        charIdleGun.draw(F250_X,F250_Y,96,96);

    }



    if(input.isMouseButtonDown(0) == true){


        shooting = true;
        gunIdle = false;

    }


    if(input.isMouseButtonDown(0) == false){

        shooting = false;
        gunIdle = true;

    }




    if(shooting == true){

        shootingGun.draw(F250_X,F250_Y,96,96);



        Image gunShootingFrame = shootingGun.getImage(1);

        if(gunShootingFrame != null){

            //Rotates the gun towards the mouse  ---  while gun is Shooting

            float distanceX = clicker.getMouseX() - F250_X;
            float distanceY = clicker.getMouseY() - F250_Y;
            float h = (float) Math.sqrt(distanceX * distanceX + distanceY * distanceY);
            float dn = (float)(h / Math.sqrt(2));
            System.out.println("in gunShootingFrame != null brace"); 





            double degrees = Math.toDegrees(Math.atan2(distanceX, distanceY));
            float angle = (float) degrees;


            //Draw the bullets (THIS LOOP WONT RUN)
            for(int i = 42 ; i == 0; i = i - 1){

                 bullets[i] = bullet;
                 bullets[i].getImage().draw(bullet.getBulletX(),bullet.getBulletY(),64,64);
                 bullets[i].getImage().setRotation(angle);
                 System.out.print("Bullet: " + i + " rendered");

            }



        }

    }



}



public static void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

    Input input = gc.getInput();





    //Rotates the gun towards the mouse  ---  while gun is Idle
    Image rotatedGunIdle1 = charIdleGun.getImage(0);
    Image rotatedGunIdle2 = charIdleGun.getImage(1);
    Image rotatedGunIdle3 = charIdleGun.getImage(2);

    float xDistance = Clicker.getMouseX() - F250_X;
    float yDistance = Clicker.getMouseY() - F250_Y;
    double degrees = Math.toDegrees(Math.atan2(yDistance, xDistance));
    float angle = (float) degrees;

    rotatedGunIdle1.setRotation(angle);
    rotatedGunIdle2.setRotation(angle);
    rotatedGunIdle3.setRotation(angle);





    //Rotates the gun towards the mouse  ---  while gun is Shooting
    Image rotatedGunShooting1 = shootingGun.getImage(0);
    Image rotatedGunShooting2 = shootingGun.getImage(1);


    rotatedGunShooting1.setRotation(angle);
    rotatedGunShooting2.setRotation(angle);



    if(gunIdle == true){

        charIdleGun.update(delta);
        charIdleGun.setPingPong(true);

    }


    if(input.isMouseButtonDown(0) == true){

        shooting = true;
        gunIdle = false;

    }


    if(input.isMouseButtonDown(0) == false){

        shooting = false;
        gunIdle = true;

    }



    if(shooting == true){

        shootingGun.update(delta);

        //Update the bullet's position. (THIS LOOP WONT RUN)
          for(int i = 42; i == 0; i = i - 1)
          {

             bullets[i] = bullet;
             bullets[i].move();
             bullets[i].getImage().draw(bullet.getBulletX(),bullet.getBulletY(),64,64);
             bullets[i].getImage().setRotation(angle);
             bullets[i].move();
             System.out.print("Bullet: " + i + " updated");


             //NOTE: Will need to determine if this hit something or went off the screen. Or otherwise, the list will get filled with invalid bullets.
             //You'll have to add that yourself. Idiot.
          }

    }








}

public static boolean ifHolding(){



    return holding;


}

public static int getRecoil(int index){

    return gunRecoil[index];

}

}







package Bullet;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.state.StateBasedGame;

public class Bullet {

String bulletImageURL = "res/Images/Guns/Human/F250/Bullets/bullet1.png";
String gun = "no gun written in parameters idiot";
private Image bulletImage;
private int startX = 0;
private int startY = 0;
private int destX = 0;
private int destY = 0;
private Point location = new Point(0,0);
private float speed = 50; //how fast this moves. ( By Default )
private float deg;
private float dx;
private float dy;

private int bulletX;
private int bulletY;




public Bullet(String gun, Image bulletImage, int startX, int startY, int destX, int destY, int speed){

   this.bulletImage = bulletImage;
   this.gun = gun;
   this.speed = speed;
   this.startX = startX;
   this.startY = startY;
   location.setLocation(startX, startY);
   this.destX = destX;
   this.destY = destY;
   recalculateVector(destX, destY);
   System.out.println("The url for gun: " + gun + " is " + bulletImageURL);

}

/**
 * Calculates a new vector based on the input destination X and Y.
 * @param destX
 * @param destY
 */
public void recalculateVector(int destX, int destY)
{
   float rad = (float)(Math.atan2(destX - startX, startY - destY));

   //Can set different speeds here, if you wanted.
   speed = 10;

   this.dx = (float) Math.sin(rad) * speed;
   this.dy = -(float) Math.cos(rad) * speed;
}

/**
 * Recalculates the vector for this bullet based on the current destination.
 */
public void recalculateVector(){

   recalculateVector(destX, destY);

}


public static void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

    bulletImage;




}





public void move() // Moves this bullet.
{
   float bulletX = location.getX();
   float bulletY = location.getY();

   bulletX += dx;
   bulletY += dy;

   location.setLocation(bulletX, bulletY);
}


public int getBulletX(){

    return bulletX;

}


public int getBulletY(){

    return bulletY;

}


public Image getImage(){

    return bulletImage;

}


 }
这是您的循环:

 for(int i = 42 ; i == 0; i = i - 1){
i
设置为
42
。然后中间部分指出,只有当
i==0
false
时,循环才会继续,因为您刚刚将其设置为
42
。因此循环停止,甚至从未执行过一次迭代

你可能想要

for(int i = 42 ; i > 0; i = i - 1){

这将循环直到
i
小于或等于
0
,并且
i
将从
42
下降到
1
,这意味着循环将执行
43

噢,顺便说一句,你看到的那些评论是侮辱性的,对我来说,我觉得很有趣,不是你们。在未来,尽量避免发布太多代码。只发布你认为适合你的部分代码。其余部分并不是非常必要。只是有两个类,问题可能来自另一个类的构造函数,干扰了我的for循环问题。当我这样做时,我得到了2014年7月24日17:38:22 PDT错误:42 java.lang.ArrayIndexOutOfBoundsException:42@KevinKhachi:它实际上是数组的长度,最高可转位点为41。因此,要么将循环起点更改为41,要么使用
子弹。长度-1
(更容易看出魔法数字的来源)。天哪,谢谢你我爱你,MAKATO非常感谢你,你是有史以来在一个问题上帮助我的最热情的人谢谢你非常友好的Sir对不起,我打电话给你MAKATO,我真的想感谢Zach,他给了我答案,它不允许我编辑它,这可能需要一些数学依据你是如何移动你的子弹。也许你应该看看它是否有用。如果没有帮助,我建议你提出一个新的问题。名字没问题,我很高兴能帮上忙:)