#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Creating Enemies


    Hey guys, i'm having some serious trouble here trying to create an enemy for my game. My game is a very simple defense type game in which the player is surrounded by a barricade with zombies coming from all four cardinal directions(N, S, E, W) at random points respective to the length of the barricade. I have been able to draw the player, the background, the barricade, and the bullets as well as adding collision detection between the barricade and the player. However I simply cannot seem to figure out how to get the zombies to spawn continuously, let alone spawn them at all. At first when I started this game I was using all static methods. But I have been told to not do so. This static version of the project actually does compile and run, however I cannot seem to figure out how to spawn the zombies. Following the non static advice, I made project using no statics(apart from the main) and that one will not run because some of the code is returning null.

    Here are the classes that I believe are the problem.

    Also, I am terribly sorry for the long posts and all of the code that may possibly look nonsensical to you guys, I just really need to figure this out so that I can actually progress to other things. I greatly appreciate any help you guys can give me, thanks!


    STATIC VERSION
    --------------------

    Board:
    Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import java.util.ArrayList;
    import javax.swing.JPanel;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JFrame;
    import java.util.Random;
    
    public class Board extends JPanel implements ActionListener {
        Timer time;
        static Graphics2D Drawer;
    
        static boolean mainMenu = false;
        static boolean isPlaying = false;
        static boolean barricade = false;
        static boolean challenges = false;
        static boolean controls = false;
        static boolean instructions = false;
        static boolean backstory = false;
    
        public Board() {
            addKeyListener(new InputKeyEvents());
            setFocusable(true);
            time = new Timer(10, this);
            time.start();
        }
    
        public void paint(Graphics g) {
            Drawer = (Graphics2D) g;
    
            if (barricade){
                Drawer.drawImage(LoadScreen.getLoadScreenSplash(), 0, 0, null);
                return;
            }
    
            if (mainMenu) {
                Drawer.drawImage(MenuMain.getMenuSplash(), 0, 0, null);
                return;
            }
    
            if (isPlaying){
                DrawAll.draw();
            }
    
            if (challenges) {
                Drawer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Font font = new Font("Dialog", Font.PLAIN, 32);
                Drawer.setFont(font);
                Drawer.drawString("Challenges: ", 10, 32);
                Drawer.drawString("1.) Get 5 kills", 10, 64);
                Drawer.drawString("2.) Get 10 kills", 10, 96);
                Drawer.drawString("3.) Get 25 kills", 10, 128);
                Drawer.drawString("4.) Get 50 kills", 10, 160);
                Drawer.drawString("5.) Get 100 kills", 10, 192);
                Drawer.drawString("6.) Get 10 kills without the barricade receiving any damage", 10, 224);
                Drawer.drawString("7.) Get 25 kills without the barricade receiving any damage", 10, 256);
                Drawer.drawString("8.) Get 50 kills without the barricade receiving any damage", 10, 288);
                Drawer.drawString("9.) Get 100 kills without the barricade receiving any damage", 10, 320);
            }
    
            if(backstory){
                Drawer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Font font = new Font("Dialog", Font.PLAIN, 16);
                Drawer.setFont(font);
                Drawer.drawString("Character Backstory:", 10,15);
                Drawer.drawString("Joseph had been born into the typical British Family.", 10,30);
                Drawer.drawString("As a child he had played polo and kept playing through high school.", 10,45);
                Drawer.drawString("As soon as he graduated high school, Joseph joined the marines.", 10,60);
                Drawer.drawString("He served his country for a few years before his day had come.", 10,75);
                Drawer.drawString("Joseph was scheduled to return home.", 10,90);
                Drawer.drawString("Joseph got on the plane and the rest was history.", 10,105);    
                Drawer.drawString("World Backstory:" , 10,130);
                Drawer.drawString("When WW2 ended in 1945, many german bodies laid in the soil." , 10,145);
                Drawer.drawString("The germans didn't know what to do with them." , 10,160);
                Drawer.drawString("One day a german scientist accidently released a" , 10,175);
                Drawer.drawString("chemical into the air. It just so happened that" , 10,190);
                Drawer.drawString("this chemical had reached all the bodies." , 10,205);
                Drawer.drawString("One day, a fallen german soldier rose." , 10,220);
                Drawer.drawString("Even though he was easily taken down," , 10,235);
                Drawer.drawString("the german government did not want the people to" , 10,250);
                Drawer.drawString("feel threatened. All bodies were deported to a small" , 10,265);
                Drawer.drawString("island 50 miles offof the coast of Germany. No one had " , 10,280);
                Drawer.drawString("had even tried to step foot on the island until now." , 10,295);
    
            }
    
            if(controls){
                Graphics2D Drawer = (Graphics2D)g;
                Drawer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Font font = new Font("Dialog", Font.PLAIN, 16);
                Drawer.setFont(font);
                Drawer.drawString("Controls: ", 10, 15);
                Drawer.drawString("Up: up arrow", 10, 30);
                Drawer.drawString("Down: down arrow", 10, 45);
                Drawer.drawString("Left: left arrow", 10, 60);
                Drawer.drawString("Right: right arrow", 10, 75);
                Drawer.drawString("Shoot: spacebar", 10, 90);
            }
    
            if(instructions) {
                Graphics2D Drawer = (Graphics2D)g;
                Drawer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Font font = new Font("Dialog", Font.PLAIN, 16);
                Drawer.setFont(font);
                Drawer.drawString("Directions:", 10,15);
                Drawer.drawString("", 10,20);
                Drawer.drawString("Use your player to defend off the zombies.", 10,30);
                Drawer.drawString("You have unlimited ammo, but it takes time to reload.", 10,45);
                Drawer.drawString("The zombies go down in one shot.", 10,60);
                Drawer.drawString("Your barricade can take 20 hits before it goes down.", 10,75);
                Drawer.drawString("Try and survive for as long as you can.", 10,90);
                Drawer.drawString("Good luck, you'll need it.", 10,105);
    
            }
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            ArrayList projectiles = Player.getProjectiles();
            for (int i = 0; i < projectiles.size(); i++) {
                Projectile p = (Projectile) projectiles.get(i);
                if (p.isVisible() == true) {
                    p.update();
                } else {
                    projectiles.remove(i);
                }
            }
    
            ArrayList zombies = Enemy.getZombies();
            for (int i = 0; i < zombies.size(); i++) {
                Zombie z = (Zombie) zombies.get(i);
                if (z.isVisible() == true) {
                    z.update();
                } else {
                    zombies.remove(i);
                }
            }
    
            repaint();
    
            UpdateAll.update();
        }
    
    }
    DrawAll:
    Code:
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.util.ArrayList;
    
    public class DrawAll extends Board  {
        Player p = new Player();
    
        private static Rectangle box;
    
        public static void draw() {
            drawTransparentBox();
            drawBK();
            drawPlayer();
            drawZombie();
        }
    
        private static void drawBK() {
            Drawer.drawImage(WorldBackground.getBK(), 0, 0, null);
    
            Rectangle wall1 = new Rectangle(427, 384, 427, 20);
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(wall1.x, wall1.y, wall1.width, wall1.height);
    
            Rectangle wall2 = new Rectangle(427, 384, 20, 256);
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(wall2.x, wall2.y, wall2.width, wall2.height);
    
            Rectangle wall3 = new Rectangle(427, 640, 447, 20);
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(wall3.x, wall3.y, wall3.width, wall3.height);
    
            Rectangle wall4 = new Rectangle(853, 384, 20, 256);
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(wall4.x, wall4.y, wall4.width, wall4.height);
    
            if(wall1.intersects(box)){
                Player.y = 404;
            }
    
            if(wall2.intersects(box)){
                Player.x = 447;
            }
    
            if(wall3.intersects(box)){
                Player.y = 611;
            }
    
            if(wall4.intersects(box)){
                Player.x = 824;
            }  
        }
    
        private static void drawPlayer() {
            if(Player.facingL){
                Drawer.drawImage(Player.facingLeft.getImage(), Player.getX(), Player.getY(), null);
            }
    
            if(Player.facingR){
                Drawer.drawImage(Player.facingRight.getImage(), Player.getX(), Player.getY(), null);
            }
    
            if(Player.facingU){
                Drawer.drawImage(Player.facingUp.getImage(), Player.getX(), Player.getY(), null);
            }
    
            if(Player.facingD){
                Drawer.drawImage(Player.facingDown.getImage(), Player.getX(), Player.getY(), null);
            }        
            // 
            //         if(){
            //             Drawer.drawImage(Enemy.getImageU(), Enemy.XU, Enemy.YU, null);
            //         }
    
            ArrayList projectiles = Player.getProjectiles();
            for (int i = 0; i < projectiles.size(); i++) {
                Projectile p = (Projectile) projectiles.get(i);
                Drawer.setColor(Color.YELLOW);
                Drawer.fillRect(p.getX(), p.getY(), 10, 5);
            }
        }
    
        private static void drawZombie() {
            Drawer.drawImage(Enemy.getImageU(), 100, 100, null);
    
            //             if(Enemy.fU){
            Drawer.drawImage(Enemy.getImageU(), Enemy.XU, Enemy.YU, null);
            //             }
    
            //             if(Enemy.fD){
            Drawer.drawImage(Enemy.getImageD(), Enemy.XD, Enemy.YD, null);
            //             }
    
            //             if(Enemy.fL){
            Drawer.drawImage(Enemy.getImageL(), Enemy.XL, Enemy.YL, null);
            //             }
    
            //             if(Enemy.fR){
            Drawer.drawImage(Enemy.getImageR(), Enemy.XR, Enemy.YR, null);
            //             }
    
            
            
            ArrayList zombies = Enemy.getZombies();
            for (int i = 0; i < zombies.size(); i++) {
                Zombie z = (Zombie) zombies.get(i);
    
                Drawer.drawImage(Enemy.getImageU(), 100, 100, null);
    
                //             if(Enemy.fU){
                Drawer.drawImage(Enemy.getImageU(), Enemy.XU, Enemy.YU, null);
                //             }
    
                //             if(Enemy.fD){
                Drawer.drawImage(Enemy.getImageD(), Enemy.XD, Enemy.YD, null);
                //             }
    
                //             if(Enemy.fL){
                Drawer.drawImage(Enemy.getImageL(), Enemy.XL, Enemy.YL, null);
                //             }
    
                //             if(Enemy.fR){
                Drawer.drawImage(Enemy.getImageR(), Enemy.XR, Enemy.YR, null);
                //             }
            }
        }
    
        public static void drawTransparentBox(){
            box = new Rectangle(Player.getX(), Player.getY(), Player.getWidth(), Player.getHeight());
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(box.x, box.y, box.width, box.height);
        }
    
    }
    Enemy
    Code:
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import java.util.ArrayList;
    import java.util.Random;
    
    public class Enemy {
        private static int Xdisplacement, Ydisplacement;
    
        public static ImageIcon enemy = new ImageIcon(Player.class.getResource("World/zdown.png"));
        public static ImageIcon zD = new ImageIcon(Player.class.getResource("World/zdown.png"));
        public static ImageIcon zU = new ImageIcon(Player.class.getResource("World/zup.png"));
        public static ImageIcon zL = new ImageIcon(Player.class.getResource("World/zleft.png"));
        public static ImageIcon zR = new ImageIcon(Player.class.getResource("World/zright.png"));
    
        private static int x, y, speedX, speedY;
        private static boolean visible;
        public static boolean fL, fR, fU, fD = false;
    
        public static ArrayList<Zombie> zombies = new ArrayList<Zombie>();
    
        static Random r = new Random();
        //moving left
        static int minXL = 1180;
        static int maxXL = 1280;
        static int minYL = 400;
        static int maxYL = 620;
    
        static int XL = r.nextInt(maxXL - minXL + 1) + minXL;
        static int YL = r.nextInt(maxYL - minYL + 1) + minYL;
    
        //moving right
        static int minXR = 0;
        static int maxXR = 100;
        static int minYR = 400;
        static int maxYR = 620;
    
        static int XR = r.nextInt(maxXR - minXR + 1) + minXR;
        static int YR = r.nextInt(maxYR - minYR + 1) + minYR;
    
        //moving up
        static int minXU = 450;
        static int maxXU = 820;
        static int minYU = 924;
        static int maxYU = 1024;
    
        static int XU = r.nextInt(maxXU - minXU + 1) + minXU;
        static int YU = r.nextInt(maxYU - minYU + 1) + minYU;
    
        //moving down
        static int minXD = 450;
        static int maxXD = 820;
        static int minYD = 0;
        static int maxYD = 100;
    
        static int XD = r.nextInt(maxXD - minXD + 1) + minXD;
        static int YD = r.nextInt(maxYD - minYD + 1) + minYD;
    
        public Enemy() {
        }
    
        public static void moveRight(){
            Zombie z = new Zombie(getX(),getY());
            zombies.add(z);
    
            fR = true;
            fL = false;
            fU = false;
            fD = false;
    
            z.setSpeedX(4);
            z.setSpeedY(0);
    
        }
    
        public static void moveLeft() {
            Zombie z = new Zombie(getX(),getY());
            zombies.add(z);
    
            fL = true;
            fR = false;
            fU = false;
            fD = false;
    
            z.setSpeedX(-4);
            z.setSpeedY(0);
        }
    
        public static void moveUp() {
            Zombie z = new Zombie(getX(),getY());
            zombies.add(z);
    
            fU = true;
            fL = false;
            fR = false;
            fD = false;
    
            z.setSpeedX(0);
            z.setSpeedY(4);
        }
    
        public static void moveDown() {
            Zombie z = new  Zombie(getX(),getY());
            zombies.add(z);
    
            fD = true;
            fL = false;
            fU = false;
            fR = false;
    
            z.setSpeedX(0);
            z.setSpeedY(-4);
        }
    
        public static ArrayList getZombies() {
            return zombies;
        }
    
        public static Image getImageU(){
            return zU.getImage();
        }
    
        public static Image getImageD(){
            return zD.getImage();
        }
    
        public static Image getImageL(){
            return zL.getImage();
        }
    
        public static Image getImageR(){
            return zR.getImage();
        }
    
        public static int getX() {
            return x;
        }
    
        public static int getY() {
            return y;
        }
    
        public static void setY(int y) {
            Enemy.y = y;
        }
    
        public static void setX(int x) {
            Enemy.x = x;
        }
    
        public static void setXDisplacement(int i){
            Xdisplacement = i;
            updateX();
        }
    
        public static void setYDisplacement(int i){
            Ydisplacement = i;
            updateY();
        }
    
        public static void update() {
            updateY();
            updateX();
        }
    
        private static void updateX(){
            x += Xdisplacement;
        }
    
        private static void updateY() {
            y += Ydisplacement; 
        }
    
        public static int getWidth(){
            return enemy.getImage().getWidth(null);
        }
    
        public static int getHeight(){
            return enemy.getImage().getHeight(null);
        }
    }
    Zombie:
    Code:
    import java.util.Random;
    public class Zombie {
    
        private static int x, y, speedX, speedY;
        private boolean visible;
        static int random;
    
        static Random r = new Random();
        //moving left
        static int minXL = 1180;
        static int maxXL = 1280;
        static int minYL = 400;
        static int maxYL = 620;
    
        static int XL = r.nextInt(maxXL - minXL + 1) + minXL;
        static int YL = r.nextInt(maxYL - minYL + 1) + minYL;
    
        //moving right
        static int minXR = 0;
        static int maxXR = 100;
        static int minYR = 400;
        static int maxYR = 620;
    
        static int XR = r.nextInt(maxXR - minXR + 1) + minXR;
        static int YR = r.nextInt(maxYR - minYR + 1) + minYR;
    
        //moving up
        static int minXU = 450;
        static int maxXU = 820;
        static int minYU = 924;
        static int maxYU = 1024;
    
        static int XU = r.nextInt(maxXU - minXU + 1) + minXU;
        static int YU = r.nextInt(maxYU - minYU + 1) + minYU;
    
        //moving down
        static int minXD = 450;
        static int maxXD = 820;
        static int minYD = 0;
        static int maxYD = 100;
    
        static int XD = r.nextInt(maxXD - minXD + 1) + minXD;
        static int YD = r.nextInt(maxYD - minYD + 1) + minYD;
    
        public Zombie(int startX, int startY){
            x = startX;
            y = startY;
            speedX = 2;
            speedY = 0;
            visible = true;
    
            random = (int)(Math.random()*4) + 1;
            if(random == 1){
                Zombie z = new Zombie(getX(),getY());
                Enemy.zombies.add(z);
    
                Enemy.fL = true;
                Enemy.fR = false;
                Enemy.fU = false;
                Enemy.fD = false;
    
                z.setSpeedX(-4);
                z.setSpeedY(0);
            }
            else if(random == 2){
                Zombie z = new  Zombie(getX(),getY());
                Enemy.zombies.add(z);
    
                Enemy.fD = true;
                Enemy.fL = false;
                Enemy.fU = false;
                Enemy.fR = false;
    
                z.setSpeedX(0);
                z.setSpeedY(-4);
            }
            else if(random == 3){
                Zombie z = new Zombie(getX(),getY());
                Enemy.zombies.add(z);
    
                Enemy.fU = true;
                Enemy.fL = false;
                Enemy.fR = false;
                Enemy.fD = false;
    
                z.setSpeedX(0);
                z.setSpeedY(4);
            }
            else{
                Zombie z = new Zombie(getX(),getY());
                Enemy.zombies.add(z);
    
                Enemy.fR = true;
                Enemy.fL = false;
                Enemy.fU = false;
                Enemy.fD = false;
    
                z.setSpeedX(4);
                z.setSpeedY(0);
            }
        }
    
        public void update(){
            x += speedX;
            y += speedY;
        }
    
        public static int getX() {
            return x;
        }
    
        public static int getY() {
            return y;
        }
    
        public int getSpeedX() {
            return speedX;
        }
    
        public int getSpeedY() {
            return speedY;
        }
    
        public boolean isVisible() {
            return visible;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public void setSpeedX(int speedX) {
            this.speedX = speedX;
        }
    
        public void setSpeedY(int speedY) {
            this.speedY = speedY;
        }
    
        public void setVisible(boolean visible) {
            this.visible = visible;
        }
    
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    NON-STATIC VERSION
    -----------------------
    There is an error in board for the zombie arraylist, it's returning null. I have no idea why

    Board:
    Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import java.util.ArrayList;
    import javax.swing.JPanel;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JFrame;
    import java.util.Random;
    
    public class Board extends JPanel implements ActionListener {
        Timer time;
        Graphics2D Drawer;
        LoadScreen l;
        MenuMain m;
        DrawAll d;
        Player h;
        Enemy q;
        UpdateAll u;
    
        boolean mainMenu = false;
        boolean isPlaying = false;
        boolean barricade = false;
        boolean challenges = false;
        boolean controls = false;
        boolean instructions = false;
        boolean backstory = false;
        public  boolean fL, fR, fU, fD = false;
        Random r = new Random();
    
        public Board() {
            addKeyListener(new InputKeyEvents());
            setFocusable(true);
            time = new Timer(10, this);
            time.start();
        }
    
        public void paint(Graphics g) {
            Drawer = (Graphics2D) g;
    
            if (barricade){
                Drawer.drawImage(l.getLoadScreenSplash(), 0, 0, null);
                return;
            }
    
            if (mainMenu) {
                Drawer.drawImage(m.getMenuSplash(), 0, 0, null);
                return;
            }
    
            if (isPlaying){
                d.draw();
            }
    
            if (challenges) {
                Drawer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Font font = new Font("Dialog", Font.PLAIN, 32);
                Drawer.setFont(font);
                Drawer.drawString("Challenges: ", 10, 32);
                Drawer.drawString("1.) Get 5 kills", 10, 64);
                Drawer.drawString("2.) Get 10 kills", 10, 96);
                Drawer.drawString("3.) Get 25 kills", 10, 128);
                Drawer.drawString("4.) Get 50 kills", 10, 160);
                Drawer.drawString("5.) Get 100 kills", 10, 192);
                Drawer.drawString("6.) Get 10 kills without the barricade receiving any damage", 10, 224);
                Drawer.drawString("7.) Get 25 kills without the barricade receiving any damage", 10, 256);
                Drawer.drawString("8.) Get 50 kills without the barricade receiving any damage", 10, 288);
                Drawer.drawString("9.) Get 100 kills without the barricade receiving any damage", 10, 320);
            }
    
            if(backstory){
                Drawer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Font font = new Font("Dialog", Font.PLAIN, 16);
                Drawer.setFont(font);
                Drawer.drawString("Character Backstory:", 10,15);
                Drawer.drawString("Joseph had been born into the typical British Family.", 10,30);
                Drawer.drawString("As a child he had played polo and kept playing through high school.", 10,45);
                Drawer.drawString("As soon as he graduated high school, Joseph joined the marines.", 10,60);
                Drawer.drawString("He served his country for a few years before his day had come.", 10,75);
                Drawer.drawString("Joseph was scheduled to return home.", 10,90);
                Drawer.drawString("Joseph got on the plane and the rest was history.", 10,105);    
                Drawer.drawString("World Backstory:" , 10,130);
                Drawer.drawString("When WW2 ended in 1945, many german bodies laid in the soil." , 10,145);
                Drawer.drawString("The germans didn't know what to do with them." , 10,160);
                Drawer.drawString("One day a german scientist accidently released a" , 10,175);
                Drawer.drawString("chemical into the air. It just so happened that" , 10,190);
                Drawer.drawString("this chemical had reached all the bodies." , 10,205);
                Drawer.drawString("One day, a fallen german soldier rose." , 10,220);
                Drawer.drawString("Even though he was easily taken down," , 10,235);
                Drawer.drawString("the german government did not want the people to" , 10,250);
                Drawer.drawString("feel threatened. All bodies were deported to a small" , 10,265);
                Drawer.drawString("island 50 miles offof the coast of Germany. No one had " , 10,280);
                Drawer.drawString("had even tried to step foot on the island until now." , 10,295);
    
            }
    
            if(controls){
                Graphics2D Drawer = (Graphics2D)g;
                Drawer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Font font = new Font("Dialog", Font.PLAIN, 16);
                Drawer.setFont(font);
                Drawer.drawString("Controls: ", 10, 15);
                Drawer.drawString("Up: up arrow", 10, 30);
                Drawer.drawString("Down: down arrow", 10, 45);
                Drawer.drawString("Left: left arrow", 10, 60);
                Drawer.drawString("Right: right arrow", 10, 75);
                Drawer.drawString("Shoot: spacebar", 10, 90);
            }
    
            if(instructions) {
                Graphics2D Drawer = (Graphics2D)g;
                Drawer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Font font = new Font("Dialog", Font.PLAIN, 16);
                Drawer.setFont(font);
                Drawer.drawString("Directions:", 10,15);
                Drawer.drawString("", 10,20);
                Drawer.drawString("Use your player to defend off the zombies.", 10,30);
                Drawer.drawString("You have unlimited ammo, but it takes time to reload.", 10,45);
                Drawer.drawString("The zombies go down in one shot.", 10,60);
                Drawer.drawString("Your barricade can take 20 hits before it goes down.", 10,75);
                Drawer.drawString("Try and survive for as long as you can.", 10,90);
                Drawer.drawString("Good luck, you'll need it.", 10,105);
    
            }
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            ArrayList projectiles = h.getProjectiles();
            for (int i = 0; i < projectiles.size(); i++) {
                Projectile p = (Projectile) projectiles.get(i);
                if (p.isVisible() == true) {
                    p.update();
                } else {
                    projectiles.remove(i);
                }
            }
    
            ArrayList zombies = q.getZombies();                         //this method is returning null
            for (int i = 0; i < zombies.size(); i++) {                        
                Zombie z = (Zombie) zombies.get(i);
                if (z.isVisible() == true) {
                    z.update();
                } else {
                    zombies.remove(i);
                }
            }
    
            repaint();
    
            u.update();
        }
    
    }
    DrawALL:
    Code:
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.util.ArrayList;
    
    public class DrawAll extends Board  {
        Player p;
        WorldBackground w;
        Enemy q;
    
        private  Rectangle box;
    
        public void draw() {
            drawTransparentBox();
            drawBK();
            drawPlayer();
            drawZombie();
        }
    
        private void drawBK() {
            Drawer.drawImage(w.getBK(), 0, 0, null);
    
            Rectangle wall1 = new Rectangle(427, 384, 427, 20);
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(wall1.x, wall1.y, wall1.width, wall1.height);
    
            Rectangle wall2 = new Rectangle(427, 384, 20, 256);
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(wall2.x, wall2.y, wall2.width, wall2.height);
    
            Rectangle wall3 = new Rectangle(427, 640, 447, 20);
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(wall3.x, wall3.y, wall3.width, wall3.height);
    
            Rectangle wall4 = new Rectangle(853, 384, 20, 256);
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(wall4.x, wall4.y, wall4.width, wall4.height);
    
            if(wall1.intersects(box)){
                p.y = 404;
            }
    
            if(wall2.intersects(box)){
                p.x = 447;
            }
    
            if(wall3.intersects(box)){
                p.y = 611;
            }
    
            if(wall4.intersects(box)){
                p.x = 824;
            }  
        }
    
        private  void drawPlayer() {
            if(p.facingL){
                Drawer.drawImage(p.facingLeft.getImage(), p.getX(), p.getY(), null);
            }
    
            if(p.facingR){
                Drawer.drawImage(p.facingRight.getImage(), p.getX(), p.getY(), null);
            }
    
            if(p.facingU){
                Drawer.drawImage(p.facingUp.getImage(), p.getX(), p.getY(), null);
            }
    
            if(p.facingD){
                Drawer.drawImage(p.facingDown.getImage(), p.getX(), p.getY(), null);
            }        
    
            ArrayList projectiles = p.getProjectiles();
            for (int i = 0; i < projectiles.size(); i++) {
                Projectile p = (Projectile) projectiles.get(i);
                Drawer.setColor(Color.YELLOW);
                Drawer.fillRect(p.getX(), p.getY(), 10, 5);
            }
        }
    
        private  void drawZombie() {
            ArrayList zombies = q.getZombies();
            for (int i = 0; i < zombies.size(); i++) {
                Zombie z = (Zombie) zombies.get(i);
    
                Drawer.drawImage(q.getImageU(), q.getX(), q.getY(), null);
    
                if(q.fU){
                    Drawer.drawImage(q.getImageU(), z.getX(), z.getY(), null);
                }
    
                if(q.fD){
                    Drawer.drawImage(q.getImageD(), z.getX(), z.getY(), null);
                }
    
                if(q.fL){
                    Drawer.drawImage(q.getImageL(), z.getX(), z.getY(), null);
                }
    
                if(q.fR){
                    Drawer.drawImage(q.getImageR(), z.getX(), z.getY(), null);
                }
            }
        }
    
        public  void drawTransparentBox(){
            box = new Rectangle(p.getX(), p.getY(), p.getWidth(), p.getHeight());
            Drawer.setColor(Color.BLACK);
            Drawer.fillRect(box.x, box.y, box.width, box.height);
        }
    
    }
    Zombie:
    Code:
    import java.util.Random;
    public class Zombie {
    
        private int x, y, speedX, speedY;
        private boolean visible;
        int random;
        Enemy e;
    
        Random r = new Random();
        //moving left
        int minXL = 1180;
        int maxXL = 1280;
        int minYL = 400;
        int maxYL = 620;
    
        int XL = r.nextInt(maxXL - minXL + 1) + minXL;
        int YL = r.nextInt(maxYL - minYL + 1) + minYL;
    
        //moving right
        int minXR = 0;
        int maxXR = 100;
        int minYR = 400;
        int maxYR = 620;
    
        int XR = r.nextInt(maxXR - minXR + 1) + minXR;
        int YR = r.nextInt(maxYR - minYR + 1) + minYR;
    
        //moving up
        int minXU = 450;
        int maxXU = 820;
        int minYU = 924;
        int maxYU = 1024;
    
        int XU = r.nextInt(maxXU - minXU + 1) + minXU;
        int YU = r.nextInt(maxYU - minYU + 1) + minYU;
    
        //moving down
        int minXD = 450;
        int maxXD = 820;
        int minYD = 0;
        int maxYD = 100;
    
        int XD = r.nextInt(maxXD - minXD + 1) + minXD;
        int YD = r.nextInt(maxYD - minYD + 1) + minYD;
    
        public Zombie(int startX, int startY){
            x = startX;
            y = startY;
            speedX = 2;
            speedY = 0;
            visible = true;
    
            random = (int)(Math.random()*4) + 1;
            if(random == 1){
                e.moveLeft();
            }
            else if(random == 2){
                e.moveDown();
            }
            else if(random == 3){
                e.moveUp();
            }
            else{
                e.moveRight();
            }
        }
    
        public void update(){
            x += speedX;
            y += speedY;
        }
    
        public int getX() {
            return x;
        }
    
        public int getY() {
            return y;
        }
    
        public int getSpeedX() {
            return speedX;
        }
    
        public int getSpeedY() {
            return speedY;
        }
    
        public boolean isVisible() {
            return visible;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public void setSpeedX(int speedX) {
            this.speedX = speedX;
        }
    
        public void setSpeedY(int speedY) {
            this.speedY = speedY;
        }
    
        public void setVisible(boolean visible) {
            this.visible = visible;
        }
    
    }
    Enemy:
    Code:
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import java.util.ArrayList;
    import java.util.Random;
    
    public class Enemy {
        private  int Xdisplacement, Ydisplacement;
    
        public  ImageIcon enemy = new ImageIcon(Player.class.getResource("World/zdown.png"));
        public  ImageIcon zD = new ImageIcon(Player.class.getResource("World/zdown.png"));
        public  ImageIcon zU = new ImageIcon(Player.class.getResource("World/zup.png"));
        public  ImageIcon zL = new ImageIcon(Player.class.getResource("World/zleft.png"));
        public  ImageIcon zR = new ImageIcon(Player.class.getResource("World/zright.png"));
    
        private  int x, y, speedX, speedY;
        private  boolean visible;
        public  boolean fL, fR, fU, fD = false;
    
        private  ArrayList<Zombie> zombies = new ArrayList<Zombie>();
    
        Random r = new Random();
        //moving left
        int minXL = 1180;
        int maxXL = 1280;
        int minYL = 400;
        int maxYL = 620;
    
        int XL = r.nextInt(maxXL - minXL + 1) + minXL;
        int YL = r.nextInt(maxYL - minYL + 1) + minYL;
    
        //moving right
        int minXR = 0;
        int maxXR = 100;
        int minYR = 400;
        int maxYR = 620;
    
        int XR = r.nextInt(maxXR - minXR + 1) + minXR;
        int YR = r.nextInt(maxYR - minYR + 1) + minYR;
    
        //moving up
        int minXU = 450;
        int maxXU = 820;
        int minYU = 924;
        int maxYU = 1024;
    
        int XU = r.nextInt(maxXU - minXU + 1) + minXU;
        int YU = r.nextInt(maxYU - minYU + 1) + minYU;
    
        //moving down
        int minXD = 450;
        int maxXD = 820;
        int minYD = 0;
        int maxYD = 100;
    
        int XD = r.nextInt(maxXD - minXD + 1) + minXD;
        int YD = r.nextInt(maxYD - minYD + 1) + minYD;
    
        public Enemy() {
        }
    
        public void moveRight(){
            Zombie z = new Zombie(getX(),getY());
            zombies.add(z);
    
            fR = true;
            fL = false;
            fU = false;
            fD = false;
    
            z.setSpeedX(4);
            z.setSpeedY(0);
    
        }
    
        public void moveLeft() {
            Zombie z = new Zombie(getX(),getY());
            zombies.add(z);
    
            fL = true;
            fR = false;
            fU = false;
            fD = false;
    
            z.setSpeedX(-4);
            z.setSpeedY(0);
        }
    
        public void moveUp() {
            Zombie z = new Zombie(getX(),getY());
            zombies.add(z);
    
            fU = true;
            fL = false;
            fR = false;
            fD = false;
    
            z.setSpeedX(0);
            z.setSpeedY(4);
        }
    
        public void moveDown() {
            Zombie z = new  Zombie(getX(),getY());
            zombies.add(z);
    
            fD = true;
            fL = false;
            fU = false;
            fR = false;
    
            z.setSpeedX(0);
            z.setSpeedY(-4);
        }
    
        public ArrayList getZombies() {
            return zombies;
        }
    
        public Image getImageU(){
            return zU.getImage();
        }
    
        public Image getImageD(){
            return zD.getImage();
        }
    
        public Image getImageL(){
            return zL.getImage();
        }
    
        public Image getImageR(){
            return zR.getImage();
        }
    
        public int getX() {
            return x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public void setXDisplacement(int i){
            Xdisplacement = i;
            updateX();
        }
    
        public void setYDisplacement(int i){
            Ydisplacement = i;
            updateY();
        }
    
        public void update() {
            updateY();
            updateX();
        }
    
        private void updateX(){
            x += Xdisplacement;
        }
    
        private void updateY() {
            y += Ydisplacement; 
        }
    
        public int getWidth(){
            return enemy.getImage().getWidth(null);
        }
    
        public int getHeight(){
            return enemy.getImage().getHeight(null);
        }
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    42
    Rep Power
    5
    The error your getting is because the arraylist contains null zombies. Are you sure you are adding zombies to the list before trying to use them? If not, it will try to draw null which will throw a NullPointerException.

    Or its happening because zombies ArrayList object is null.
    Last edited by Corpsecreate; June 3rd, 2013 at 10:28 PM.

IMN logo majestic logo threadwatch logo seochat tools logo