ok im extremely noobie at java, and this is my first project, and its basically a Don Quixote 2dscroller, and im having problems with Collision,…
In the game, there are 2 levels of height, the groundlevel and the platform level.
- when the character jumps and is falling, and he is on the height of the platform and the character’s center position is between the left and right position of the platform, then he doesn’t fall, but otherwise, he does.
2.if character is underneath platform and tries to jump, then he hits the bottom of the platform and falls back down
ok i made it work good when there was only one platform, but when i had more than one, the animation executed alot faster than usual, because it had to calculate more, and so animation has to catch up. and its really annoying and i dont how to make collision more efficient
because basically i have a level class that reads in the level array, then it makes a element [][] array and a collision[][] array, then i converted the collision[][] array to a regular one dimensional array without null objects. Then i pass it to my character class which loops the array to check for collision…can someone provide me with a better way to do this, thats easy to understand"??
import java.applet.*;
import java.awt.*;
import java.util.*;
public class Quixote
{
int x_posRight;
int x_posLeft;
int x_posCenter;
int y_posUp;
int y_posDown;
int walkspeed = 2;
int fallspeed = 4;
int jumpspeed = -6;
int jumpspeed2 = -4;
int currentJumpSpeed=0;
int jumpCounter = 0;
int walkCounter = 0;
boolean faceLeft = false;
boolean jumping = false;
boolean falling = false;
boolean walkingLeft = false;
boolean walkingRight = false;
boolean jumpLock = false;
boolean bump = true;
boolean bump2 = false;
Component applet;
Image donFacingRight;
Image donFacingLeft;
Image donJumpingLeft;
Image donJumpingRight;
Image donWalkingLeft;
Image donWalkingRight;
Image groundImage;
Collision[] cList;
public Quixote(int xpos, int ypos, Component applet)
{
x_posLeft = xpos;
x_posRight = xpos + DQConstants.donNormalWidth;
y_posUp = ypos;
y_posDown = ypos + DQConstants .donNormalHeight;
x_posCenter = xpos + DQConstants.donNormalWidth/2;
this.applet = applet;
}
public void setImages(Image faceLeft, Image faceRight, Image walkLeft,
Image walkRight, Image jumpLeft, Image jumpRight)
{
donFacingRight = faceRight;
donFacingLeft = faceLeft;
donWalkingRight = walkRight;
donWalkingLeft = walkLeft;
donJumpingLeft = jumpLeft;
donJumpingRight = jumpRight;
}
public void setCollisionList(Collision[] collisionList)
{
cList = collisionList;
}
public int getXposLeft()
{
return x_posLeft;
}
public int getXposRight()
{
return x_posRight;
}
public int getYposUp()
{
return y_posUp;
}
public int getYposDown()
{
return y_posDown;
}
public int getXposCenter()
{
return x_posCenter;
}
public boolean getJumpLock()
{
return jumpLock;
}
public void isJumping(boolean b)
{
jumping = b;
}
public void isFaceLeft(boolean b)
{
faceLeft = b;
}
public void isWalkingLeft(boolean b)
{
walkingLeft = b;
faceLeft = b;
}
public void isWalkingRight(boolean b)
{
walkingRight = b;
faceLeft = !b;
}
public void updateXpos(boolean x)
{
boolean left = x;
if(left)
{
x_posLeft -= walkspeed;
x_posRight -= walkspeed;
x_posCenter -= walkspeed;
}
else
{
x_posLeft += walkspeed;
x_posRight += walkspeed;
x_posCenter += walkspeed;
}
}
public void updateYpos(int y)
{
int ySpeed = y;
y_posUp+=ySpeed;
y_posDown+=ySpeed;
}
public void updateQuixote()
{
if(walkingLeft)
{
updateXpos(true);
faceLeft = true;
}
else if(walkingRight)
{
updateXpos(false);
faceLeft = false;
}
if(jumping)
{
jumpLock = true;
for(int i=0;i<cList.length;i++)
{
if(y_posUp<=cList[i].getYposDown()
&&x_posCenter<=cList[i].getXposRight()+40
&&x_posCenter>=cList[i].getXposLeft()-40
&&bump2)
{
jumping = false;
jumpCounter = 0;
falling = true;
bump = false;
}
else if(jumpCounter<30)
updateYpos(jumpspeed);
else if(jumpCounter<45)
updateYpos(jumpspeed2);
else
{
jumping = false;
jumpCounter = 0;
falling = true;
}
jumpCounter++;
}
}
if(falling)
{
updateYpos(fallspeed);
if(y_posDown>=DQConstants.GROUNDPOSITION)
{
falling=false;
y_posDown = 400;
y_posUp = 400-DQConstants.donNormalHeight;
jumpLock = false;
bump = true;
}
else
{
for(int i=0;i<cList.length;i++)
{
if(y_posDown>=cList[i].getYposUp()
&&((x_posCenter>=cList[i].getXposLeft()&&x_posCenter<=cList[i].getXposRight())&&bump))
{
falling=false;
y_posDown = 200;
y_posUp = 200-DQConstants.donNormalHeight;
jumpLock = false;
bump2 = false;
}
else
{
falling = true;
bump2 = true;
}
}
}
}
for(int i=0;i<cList.length;i++)
{
if(y_posDown==200&&(x_posRight<cList[i].getXposLeft()+40||x_posLeft>cList[i].getXposRight()-40))
{
falling = true;
jumpLock = true;
}
}
}
public void paintQuixote(Graphics g)
{
g.setColor(Color.red);
g.drawString(x_posLeft + " " + x_posRight,100,100);
g.drawString(y_posDown + " " + y_posUp,100,80);
if(jumping)
if(!faceLeft)
g.drawImage(donJumpingLeft,x_posLeft,y_posUp,applet);
else
g.drawImage(donJumpingRight,x_posLeft,y_posUp,applet);
else
if(faceLeft)
g.drawImage(donFacingLeft,x_posLeft,y_posUp,applet);
else
g.drawImage(donFacingRight,x_posLeft,y_posUp,applet);
}
}
import java.applet.;
import java.awt.;
import java.net.;
import java.util.;
public class Level
{
String[] levelArray;
Element[][] elementsArray;
Element element;
Collision[][] collisionArray;
Collision [] collisionList;
Collision collision;
Component applet;
Image groundImage;
Applet app;
public Level(String[] levelArray,Component applet,Applet app)
{
this.levelArray = levelArray;
this.applet = applet;
this.app = app;
elementsArray = new Element[levelArray.length][levelArray[0].length()];
collisionArray = new Collision[levelArray.length][levelArray[0].length()];
groundImage = app.getImage(app.getCodeBase(),"ground.gif");
readLevelArray();
compressCollisionArray();
}
public void readLevelArray()
{
int collCount=0;
for(int i=0;i<levelArray.length;i++)
{
for(int k=0;k<levelArray[i].length();k++)
{
if(levelArray[i].charAt(k)=='g')
{
element = new Element("ground",i,k,applet,app);
collision = new Collision("ground",i,k);
collCount++;
}
else
{
element = null;
collision = null;
}
if(element!=null)
{
elementsArray[i][k] = element;
collisionArray[i][k] = collision;
}
}
}
collisionList = new Collision[collCount];
}
public Collision[] getCollisionList()
{
return collisionList;
}
public void compressCollisionArray()
{
int listcount=0;
for(int i=0;i<collisionArray.length;i++)
{
for(int k=0;k<collisionArray[0].length;k++)
{
if(collisionArray[i][k]!=null)
{
collisionList[listcount] = collisionArray[i][k];
listcount++;
}
}
}
}
public void paintLevel(Graphics g)
{
for(int i=0;i<levelArray.length;i++)
{
for(int k=0;k<levelArray[0].length();k++)
{
if(elementsArray[i][k]!=null)
elementsArray[i][k].paintElement(g);
}
}
}
public void paintGround(Graphics g)
{
for(int i=0;i<levelArray[1].length();i++)
{
g.drawImage(groundImage,i*100,400,applet);
}
}
}
import java.util.*;
public class Collision
{
String collideType;
int row;
int col;
int x_posLeft;
int x_posRight;
int y_posUp;
int y_posDown;
public Collision(String collideType, int row, int col)
{
this.collideType = collideType;
this.row = row;
this.col = col;
generateCollisionArea();
}
public void generateCollisionArea()
{
if(collideType.equals("ground"))
{
x_posLeft = col * 100;
x_posRight = x_posLeft + DQConstants.GROUNDWIDTH;
y_posUp = row * 100;
y_posDown = y_posUp + DQConstants.GROUNDHEIGHT;
}
}
public int getXposLeft()
{
return x_posLeft;
}
public int getXposRight()
{
return x_posRight;
}
public int getYposUp()
{
return y_posUp;
}
public int getYposDown()
{
return y_posDown;
}
}