Results 1 to 7 of 7
  1. #1
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Tower Defence Game - Need help to continue

    Ok so I've been following this tutorial for building a tower defence game, after an hour I try to run it and get an error. I re-watched the tutorial twice and spend two hours comparing code and I can not figure it out.

    Please, this would be such an interesting project, help me resolve this so I can continue.

    here is my full code

    Java Code:
      
    this is the image I used created in gimp

    kblpe.png at Free Image Hosting

    and this is my output

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException
    
    ad infinitum
    please help me out guys, I don't know what this error means it doesnt give me any method or point of reference I'm familier with, what is the eventqueue and why wont it behave?

    100,700,999 ziggle points for the solution

  2. #2
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Tower Defence Game - Need help to continue

    sorry I forgot the code! moving files to shared folders in order to communicate with your guest OS is tiresome

    Java Code:
    //////////////////////////////////// Screen Class
    
    package pkg;
    
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.io.*;
    
    // runnable makes this class a thread which needs a run method
    // the screen has it's own size and fits into the window (Frame)
    public class Screen extends JPanel implements Runnable
    {
    	public Thread thread = new Thread(this);
    	
    	public static Image[] tileset_ground = new Image[100]; // 100 images
    	public static Image[] tileset_air = new Image[100];
    	
    	public static int myWidth, myHeight;
    	
    	public static boolean isFirst = true;
    	
    	public static Room room;
    	public static Save save;
    	
    	public Screen()
    	{
    		thread.start();
    	}
    	
    	
    	public void define()
    	{
    		room = new Room();
    		save = new Save();
    		
    		for( int i = 0 ; i < tileset_ground.length ; i++ )
    		{
    			tileset_ground[i] = new ImageIcon("Resources/tileset_ground.png").getImage();
    			tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26))); // crop image
    		}
    		
    		for( int i = 0 ; i < tileset_air.length ; i++ )
    		{
    			tileset_air[i] = new ImageIcon("Resources/tileset_air.png").getImage();
    			tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26)));
    		}
    		
    		save.loadSave(new File("save/mission1.td"));
    	}
    	
    	// this method is called by default
    	// never do a gameloop in paint component with repaint at the end 
    	// always use run method for game loop
    	public void paintComponent(Graphics g)
    	{
    		if(isFirst)
    		{
    			myWidth = getWidth();
    			myHeight = getHeight();
    			define();
    			isFirst = false;
    		}
    		
    		// draws out a blank space on the screen which clears it
    		g.clearRect(0, 0, getWidth(), getHeight());
    		
    		room.draw(g);
    	}
    	
    	public void run()
    	{
    		while(true)
    		{		
    			if(!isFirst)
    			{
    				room.physic();
    			}
    				
    			repaint();
    			
    			try
    			{
    				// set frame rate 1 milisecond
    				Thread.sleep(1);
    			}
    			catch(Exception e)
    			{
    				
    			}
    			
    		}
    	}
    }
    
    /////////////////////////////////////////////Room class
    
    package pkg;
    
    import java.awt.*;
    
    // load up the levels where all the blocks are located
    public class Room 
    {
    	public int worldWidth = 12;
    	public int worldHeight = 9;
    	public int blockSize = 52; // double the size of the image for pixel effect
    	
    	public Block[][] block;
    	
    	public Room()
    	{
    		define();
    	}
    	
    	public void define()
    	{
    		block = new Block[worldHeight][worldWidth];
    		
    		for(int y = 0; y < block.length; y++)
    		{
    			for(int x = 0; x < block[0].length; x++)
    			{
    				block[y][x] = new Block( (Screen.myWidth/2) - ((worldWidth * blockSize)/2) + (x * blockSize), (y * blockSize), blockSize, blockSize, Value.groundGrass, Value.airAir);
    			}
    		}
    	}
    	
    	public void physic()
    	{
    		
    	}
    	
    	// called with the Screen's graphic object
    	public void draw(Graphics g)
    	{
    		for(int y = 0; y < block.length; y++)
    		{
    			for(int x = 0; x < block[0].length; x++)
    			{
    				block[y][x].draw(g);
    			}
    		}
    	}
    	
    }
    
    ///////////////////////////////////////////block Class
    
    package pkg;
    
    import java.awt.*;
    
    public class Block extends Rectangle 
    {
    	public int groundID;
    	public int airID;
    	
    	public Block(int x, int y, int width, int height, int groundID, int airID)
    	{
    		setBounds(x, y, width, height);
    		this.groundID = groundID;
    		this.airID = airID;
    	}
    	
    	public void draw(Graphics g)
    	{
    		g.drawImage(Screen.tileset_ground[groundID], x, y, width, height, null);
    		
    		if( airID != Value.airAir)
    		{
    			g.drawImage(Screen.tileset_ground[airID], x, y, width, height, null);
    		}
    	}
    }
    
    /////////////////////////////////////Frame Class
    
    package pkg;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class Frame extends JFrame
    {
    	public static String title = "Tower Defence";
    	public static Dimension size = new Dimension(700, 550);
    			
    	public Frame()
    	{
    		setTitle(title);
    		setSize(size);
    		setResizable(false); // negates a lot of bugs
    		setLocationRelativeTo(null); // centred
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		init();
    	}
    	
    	// For applet this is called instead of main method
    	public void init()
    	{
    		setLayout(new GridLayout(1, 1, 0, 0)); // sets the area to add objects to the window
    		Screen screen = new Screen();
    		add(screen);
    		setVisible(true);
    	}
    	
    	public static void main(String args[])
    	{
    		Frame frame = new Frame();
    	}
    	
    }
    
    ////////////////////////////////////Value Class
    
    package pkg;
    
    public class Value 
    {
    	public static int groundGrass = 0;
    	public static int groundRoad = 1;
    	
    	public static int airAir = -1;
    }
    
    Save Class
    
    package pkg;
    
    import java.io.*;
    import java.util.*;
    
    public class Save 
    {
    	public void loadSave(File loadPath)
    	{
    		try
    		{
    			Scanner loadScanner = new Scanner(loadPath);
    		
    			while(loadScanner.hasNext())
    			{
    				for( int y = 0 ; y < Screen.room.block.length ; y++ )
    				{
    					for( int x = 0 ; x < Screen.room.block[0].length ; x++ )
    					{
    						Screen.room.block[y][x].groundID = loadScanner.nextInt();
    					}
    				}
    				
    				for( int y = 0 ; y < Screen.room.block.length ; y++ )
    				{
    					for( int x = 0 ; x < Screen.room.block[0].length ; x++ )
    					{
    						Screen.room.block[y][x].airID = loadScanner.nextInt();
    					}
    				}
    			}
    		
    			loadScanner.close();
    			
    		}
    		catch(Exception e)
    		{
    			
    		}
    	}
    	
    }

  3. #3
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Tower Defence Game - Need help to continue

    oh and this is in the file mission1.td

    0 1 0 1 1 1 1 1 1 1 0 0
    0 1 0 1 0 0 0 0 0 1 1 1
    0 1 0 1 1 1 1 1 0 0 0 1
    0 1 0 0 0 0 0 1 0 0 0 1
    0 1 0 1 1 1 0 1 0 0 0 1
    0 1 0 1 0 1 0 1 0 1 1 1
    0 1 1 1 0 1 1 1 0 1 0 0
    0 0 0 0 0 0 0 0 0 1 0 0

    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Tower Defence Game - Need help to continue

    You'll want to post more of that error message, and you'll want to indicate which line(s) cause errors.

  5. #5
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Tower Defence Game - Need help to continue

    thats all that comes up in the console, it doesn't give me a line number - I can't seem to extract more information out of it either which is part of the problem, can you give me some advice on how I can debug this?

  6. #6
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Tower Defence Game - Need help to continue

    Quote Originally Posted by Fubarable View Post
    You'll want to post more of that error message, and you'll want to indicate which line(s) cause errors.
    Sorry honestly this didn't show up until after I enabled line numbers

    Java Code:
    at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    	at pkg.Block.draw(Block.java:19)
    	at pkg.Room.draw(Room.java:44)
    	at pkg.Screen.paintComponent(Screen.java:66)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$700(Unknown Source)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    what does this mean, there are a few number references here

    also the exception in thread to the bottom seems to be repeated

  7. #7
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: Tower Defence Game - Need help to continue

    that issue was solved by changing the -1 to 0's in mission1.td

    however I have another problem

    0 1 0 1 1 1 1 1 1 1 0 0
    0 1 0 1 0 0 0 0 0 1 1 1
    0 1 0 1 1 1 1 1 0 0 0 1
    0 1 0 0 0 0 0 1 0 0 0 1
    0 1 0 1 1 1 0 1 0 0 0 1
    0 1 0 1 0 1 0 1 0 1 1 1
    0 1 1 1 0 1 1 1 0 1 0 0
    1 0 0 0 0 0 0 0 1 1 0 1 <----- only this line seems to work

    here is the output

    788uv.png at Free Image Hosting

    you can see the line that is working corresponds to the output

    whats the issue here?

Similar Threads

  1. Tower Defense Rocket
    By pfolder in forum Java Gaming
    Replies: 0
    Last Post: 04-12-2012, 03:16 AM
  2. Replies: 2
    Last Post: 04-08-2012, 11:58 PM
  3. Tower Defense problems
    By emo bob in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 03:13 PM
  4. Java Game troubles continue
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 08:55 PM
  5. JAVA Software Engineers Needed- Defence UK
    By Nadira.Rahman in forum Jobs Offered
    Replies: 0
    Last Post: 11-06-2009, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •