Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Chigaimasu

[java] I'm having some code troubles...

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

/*
* CollisionTest.java
* This is the program for testing out my collision detection
* methods.  Literally!
* Update (2-5-04): It works!  Oh my freaking god it works!  Hallelujah!
* HALLE-FREAKING-LUJAH!!!!!
*/

// Imports

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class CollisionTest extends Applet implements KeyListener
{
	
	int x = 140, y = 140, a = 20, b = 20;
	int oldX, oldY;
	int[] xArray = {0,0,0,0};
	int[] yArray = {0,0,0,280};
	int[] widArray = {300,20,20,300};
	int[] higArray = {20,300,300,20};
	int rectNum = 4;
	
	Rectangle mainRect = new Rectangle(x,y,a,b);// Creates the moving square

	
	// These next four define the main walls of the applet

	Rectangle tSideWall = new Rectangle();
	Rectangle rSideWall = new Rectangle();
	Rectangle lSideWall = new Rectangle();
	Rectangle bSideWall = new Rectangle();
	
	Rectangle[] rectArray = {tSideWall,rSideWall,lSideWall,bSideWall};
	int c = 0;
	//This loop also used to be a for loop, but I changed it to try to fix the error.

       while(c < rectNum)// I get an Illegal Start of Type error here

	{
		rectArray[c].setPosition(xArray[c],yArray[c]);
		rectArray[c].setWidth(widArray[c]);
		rectArray[c].setHeight(higArray[c]);
		c++;	
	}
	
	public void init()
	{
		addKeyListener(this);
		repaint();
	}
	
	public void keyPressed(KeyEvent e)// The obligitory keyPressed method

	{
		oldX = x;
		oldY = y;
		if(e.getKeyCode() == KeyEvent.VK_NUMPAD8)
		{			
			checkUp();
			checkUObj();	
			
			repaint();
		}
		if(e.getKeyCode()==KeyEvent.VK_NUMPAD4)
		{
			checkLeft();
			checkLObj();
			repaint();
		}
		if(e.getKeyCode()==KeyEvent.VK_NUMPAD5)
		{
			checkDown();
			checkDObj();
			repaint();
		}
		if(e.getKeyCode()==KeyEvent.VK_NUMPAD6)
		{
			checkRight();
			checkRObj();
			repaint();
		}
		mainRect.setLocation(x,y);
		System.out.println("x=" + x + " y=" + y);
	}
	public void keyReleased(KeyEvent e)
	{}
	public void keyTyped(KeyEvent e)// For some reason, it doesn't think this is defined.

	{}
	
	public void paint(Graphics g)
	{
		g.setColor(Color.blue);
		g.clearRect(oldX, oldY, a, b);
		
		for(int d=0;d<xArray.length;d++)
		{
			g.fillRect(xArray[c],yArray[c],widArray[c],higArray[c]);
		}
		
	}
	// These check to wrap the square to the other side of the screen

	public void checkUp()
	{
		int yTemp = y - 10;
		if(yTemp < 0)
		{
			y = 300 + (y - b);
		}
		else
		{
			y -= 10;
		}
		
	}
	
	public void checkDown()
	{
		int yTemp = y + b;
		if(yTemp >= 300)
		{
			y = 0;
		}
		else
		{
			y+=10;
		}
		
	}
	public void checkLeft()
	{
		int xTemp = x - 10;
		if(xTemp < 0)
		{
			x = 300 + (x - a);
		}
		else
		{
			x-=10;
		}
		
	}
	public void checkRight()
	{
		int xTemp = x + a;
		if(xTemp >= 300)
		{
			x = 0;
		}
		else
		{
			x+=10;
		}
		
	}
	// These check whether there will be an immovible object on next move

	public void checkUObj()
	{
		Rectangle tempRect = new Rectangle(mainRect);
		tempRect.setLocation(x, y-10);
		for(int xx = 0; xx < rectArray.length; xx++)
		{
			if(tempRect.intersects(rectArray[xx]))
			{
			y = (int)(rectArray[xx].getY()+rectArray[xx].getHeight());
			}
		}

	}
	
	
	public void checkLObj()
	{
		Rectangle tempRect = new Rectangle(mainRect);
		tempRect.setLocation(x-10,y);
		for(int xx = 0; xx < rectArray.length; xx++)
		{
			if(tempRect.intersects(rectArray[xx]))
			{
				x = (int)(rectArray[xx].getX()+rectArray[xx].getWidth());
			}
		}
	}
	
	
	public void checkDObj()
	{
		Rectangle tempRect = new Rectangle(mainRect);
		tempRect.setLocation(x, y+10);		
		for(int xx = 0; xx < rectArray.length; xx++)
		{
			if(tempRect.intersects(rectArray[xx]))
			{
				y = (int)rectArray[xx].getY()-20;
			}
		}
	}
	
	
	public void checkRObj()
	{
		Rectangle tempRect = new Rectangle(mainRect);
		tempRect.setLocation(x+10,y);
		for(int xx = 0; xx < rectArray.length; xx++)
		{
			if(tempRect.intersects(rectArray[xx]))
			{
				x = (int)rectArray[xx].getX()-20;
			}
		}
	}// I get "<identifier> expectd" error here.

}// End CollisionTest class

Anywhere, here's my code. I've outlined the errors I'm getting, some strange ones like "Illegal start of type" and "Not defined keyTyped". Not pleasant. Any ideas? Thanks. ~Justin [edited by - Chigaimasu on February 6, 2004 8:11:53 AM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Hello,

There were an number of errors

1. undefinded code region.

while(c < rectNum)// I get an Illegal Start of Type error here {
rectArray[c].setPosition(xArray[c],yArray[c]);
rectArray[c].setWidth(widArray[c]);
rectArray[c].setHeight(higArray[c]);
c++;
}


the code above must be in a method or constructor

2.

setPosition is not a method of the java.awt.Rectangle class
you should use "setBounds(x,y,w,h)" instead.


3.

missing close class brace at the end of class definition


after these alterations the class did compile but does not execute properly


hople this helps




Share this post


Link to post
Share on other sites
Thank you very much for that. It fixed my problem right-quick.

But alas, I have a new one.

I''m using multiple classes in this program I''m writing, user defined ones.

IE

public class DestRect extends Rectangle


and

public class Enemy extends DestRect


For some reason when I try to compile enemy, it doesn''t work. I am attempting to import DestRect.class. Is that wrong?

~Justin

Share this post


Link to post
Share on other sites
The following does work

import java.awt.*;

/**
*
* @author
*/
public class DestRect extends Rectangle {

/** Creates a new instance of DestRect */
public DestRect() {
}

}

/**
*
* @author
*/
public class Enemy extends DestRect {

/** Creates a new instance of Enemy */
public Enemy() {
}

}


I can only assume the you are getting a No Class Def error. Makes sure that your complied classes are on the correct classpath if your are editing from dos prompt or command shell.

Another suggestion if you have not already doen so, is to download an IDE . I use Netbeans , but there are plenty of others such as eclipse and jbuilder.


I hope this helps.





Share this post


Link to post
Share on other sites
Guess who''s having code troubles again!

Alright. I''ve set up a system of intersections throughout a grid (I''m making a pac-man clone), but my enemy only detects a few of them. Here''s the code that actually sees whether it hits an intersection or not.


public void randAI(Enemy en, long seed)
{
boolean hitsInter = false;
int whichInter = 0;
int temp;

for(int c=0;c<ixArray.length;c++)
{

if(iArray[c].contains(en))
{
hitsInter = true;
whichInter = c;
System.out.println("Inside loop''s if statement. whichInter =" + c);
}
}
if(hitsInter)
{
System.out.println("Inside ''hitsInter'' if statement");
switch(iArray[whichInter].getType())
{
case 0: en.flipDirection(); System.out.println("case0");break;

case 1: temp = en.pickOfThree(seed);
if(temp == 1)
{
en.setDirLeft();
}
else if(temp == 2)
{}
else if(temp == 3)
{
en.setDirRight();
}
System.out.println("case1");System.out.println("Temp = " + temp);
break;

case 2: temp = en.pickOfTwo(seed);
if(en.getDirection() == 8)
{
if(temp == 1)
{
en.setDirLeft();
}
else if(temp == 2)
{
en.setDirRight();
}
}
else if(en.getDirection() == 6)
{
if(temp == 1)
{}
else if(temp == 2)
{
en.setDirRight();
}
}
else if(en.getDirection() == 4)
{
if(temp == 1)
{}
else if(temp == 2)
{
en.setDirLeft();
}
}
System.out.println("case2");System.out.println("Temp = " + temp);
break;
case 3: temp = en.pickOfTwo(seed);
if(en.getDirection() == 6)
{
if(temp == 1)
{
en.setDirLeft();
}
else if(temp == 2)
{
en.setDirRight();
}
}
else if(en.getDirection() == 2)
{
if(temp == 1)
{}
else if(temp == 2)
{
en.setDirRight();
}
}
else if(en.getDirection() == 8)
{
if(temp == 1)
{}
else if(temp == 2)
{
en.setDirLeft();
}
}System.out.println("case3");System.out.println("Temp = " + temp);
break;
case 4: temp = en.pickOfTwo(seed);
if(en.getDirection() == 2)
{
if(temp == 1)
{
en.setDirLeft();
}
else if(temp == 2)
{
en.setDirRight();
}
}
else if(en.getDirection() == 4)
{
if(temp == 1)
{}
else if(temp == 2)
{
en.setDirRight();
}
}
else if(en.getDirection() == 6)
{
if(temp == 1)
{}
else if(temp == 2)
{
en.setDirLeft();
}
}System.out.println("case4");System.out.println("Temp = " + temp);
break;
case 5: temp = en.pickOfTwo(seed);
if(en.getDirection() == 4)
{
if(temp == 1)
{
en.setDirLeft();
}
else if(temp == 2)
{
en.setDirRight();
}
}
else if(en.getDirection() == 8)
{
if(temp == 1)
{}
else if(temp == 2)
{
en.setDirRight();
}
}
else if(en.getDirection() == 2)
{
if(temp == 1)
{}
else if(temp == 2)
{
en.setDirLeft();
}
}System.out.println("case5");System.out.println("Temp = " + temp);
break;
case 6: if(en.getDirection() == 4)
{
en.setDirRight();
}
else if(en.getDirection() == 2)
{
en.setDirLeft();
}System.out.println("case6");
break;
case 7: if(en.getDirection() == 8)
{
en.setDirRight();
}
else if(en.getDirection() == 4)
{
en.setDirLeft();
}System.out.println("case7");
break;
case 8: if(en.getDirection() == 6)
{
en.setDirRight();
}
else if(en.getDirection() == 8)
{
en.setDirLeft();
}System.out.println("case8");
break;
case 9: if(en.getDirection() == 2)
{
en.setDirRight();
}
else if(en.getDirection() == 6)
{
en.setDirLeft();
}System.out.println("case9");
break;
default: System.out.println("cannot compute AI."); break;

}// End Switch

System.out.println("Current direction of enemy is: " + en.getDirection());

}// End If

}// End RandAI


Hopefully my method names are good enough for you to figure otu without an explanation. Any ideas?

Thanks,
~Justin

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!