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.
1. 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"??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
| 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
.length();k++)
{
if(levelArray.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[k] = element;
collisionArray[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[k]!=null)
{
collisionList[listcount] = collisionArray[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[k]!=null)
elementsArray[k].paintElement(g);
}
}
}
public void paintGround(Graphics g)
{
for(int i=0;i<levelArray[1].length();i++)
{
g.drawImage(groundImage,i*100,400,applet);
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| 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; } } |