F250.java类是一个枪对象,有两种方法可以绘制和更新从枪中出来的子弹。我制作了一个Bullet类并在F250.java类中制作了一个子弹数组,但是Draw和Update方法中的for循环不会执行for循环中的代码来使子弹出来。我相信它与Bullet类有关,特别是与Bullet类的构造函数中的this.bulletImage变量有关。在Slick2d中,必须初始化图像,当我在F250.java类中制作子弹对象以在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;
}
}
答案 0 :(得分:4)
这是你的循环:
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
次