The F250.java class is a gun object, and there are two methods that draw and update the bullets coming out of the gun. I made a Bullet class and made a bullet array in the F250.java class, but the for loops in the Draw and Update method wont execute the code in the for loops to make the bullets come out. I believe it has something to do with the Bullet class, especially with the this.bulletImage variable in the constructor of the Bullet class. In Slick2d the images have to be initialized and i cant get the image I put as a parameter when I make the bullet object in F250.java class to get initialized in Bullet.java class because its not static or soemthing idk. Maybe im wrong and thats not the problem, but if it is, I dont know how to fix that. Main thing is the two for loops wont execute in the F250.java class and thank you for reading all this and thank you to whomever answeres this question I really appreciate taking your time out for me and going through the struggle to solve this tedious problem for me thank you.
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;
}
}