I'm making hit detection in Eclipse and I need to check for specific TileType enum classes, AND certain actors in my game, however I can't seem to put them in the same array. What should I do?


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 | package data; import static helpers.Clock.*; import org.newdawn.slick.opengl.Texture; import data.World.*; public class Matter extends Actor{ double xmove= 0 , ymove= 0 ; public Matter(Texture texture, float x, float y, int width, int height){ super (texture, x, y, width, height); } //Physics Stuff static double g = 1 ; //gravitational acceleration static String gravityDirection = "down" ; boolean reaction = false ; int hw = getWidth()/ 2 ; //half of the hit box's width int hh = getHeight()/ 2 ; //half of the hit box's height String collided = "" ; public void physics(){ collided = "" ; //reset variable collided //Gravity acceleration if (gravityDirection.equals( "down" )){ymove += g;} if (gravityDirection.equals( "up" )){ymove -= g;} if (gravityDirection.equals( "left" )){xmove -= g;} if (gravityDirection.equals( "right" )){xmove += g;} //Hit detection float deltaX=( float ) (xmove*Delta()); //actual distance moved in the x coordinate float deltaY=( float ) (ymove*Delta()); //actual distance moved in the y coordinate //if (xmove!=0||ymove!=0){System.out.println(World.getTileAt((int)getX()+getWidth()/2, (int)getY()+getHeight()/2));} //Collision Detection TileType[] solids = {TileType.TileB}; //all classes that can be collided with for ( int c = 0 ; c<solids.length; c++){ //This for statement handles all of the tile collisions //Vertical Collisions for ( int x= 0 ; x<hw* 2 - 1 ; x+=hw/ 2 - 2 ){ if (deltaY== 0 ){ break ;} //if there is no vertical movement, then don't check for vertical collisions //Down for ( int y= 0 ; y<=deltaY; y++){ //If ymove is negative, then the loop wont activate in the first place TileType tile = World.getTileAt(( int ) getX()+x+ 1 , ( int ) getY()+hh* 2 +y); if (( int )getY()+hh* 2 +y > World.getHeight()- 1 || tile!= null && tile.equals(solids[c])){ setLocation(getX(),getY()+y); collided += "down" ; if (reaction){ deltaY/= 3 ; ymove/= 3 ; } else { deltaY= 0 ; ymove= 0 ; } break ; } } if (ymove== 0 ){ break ;} //if there is no vertical movement, then don't check for vertical collisions //Up for ( int y= 0 ; y>=deltaY; y--){ TileType tile = World.getTileAt(( int ) getX()+x+ 1 , ( int ) getY()+y- 1 ); if (( int )getY()+y- 1 < 0 || tile!= null && tile.equals(solids[c])){ setLocation(getX(),getY()+y); collided += "up" ; if (reaction){ deltaY/= 3 ; ymove/= 3 ; } else { deltaY= 0 ; ymove= 0 ; } break ; } } } //Horizontal Collisions for ( int y= 0 ; y<hh* 2 - 1 ; y+=hh/ 2 - 2 ){ if (deltaX== 0 ){ break ;} //if there is no horizontal movement, then don't check for horizontal collisions //left for ( int x= 0 ; x>=deltaX; x--){ TileType tile = World.getTileAt(( int ) getX()+x- 1 , ( int ) getY()+y+ 1 ); if (( int )getX()+x- 1 < 0 || tile!= null && tile.equals(solids[c])){ setLocation(getX()+x,getY()); collided += "left" ; if (reaction){ deltaX/= 3 ; xmove/= 3 ; } else { deltaX= 0 ; xmove= 0 ; } break ; } } if (deltaX== 0 ){ break ;} //if there is no horizontal movement, then don't check for horizontal collisions //right for ( int x= 0 ; x<=deltaX; x++){ TileType tile = World.getTileAt(( int ) getX()+hw* 2 +x, ( int ) getY()+y+ 1 ); if (( int )getX()+hw* 2 +x > World.getWidth()- 1 || tile!= null && tile.equals(solids[c])){ setLocation(getX()+x,getY()); collided += "right" ; if (reaction){ deltaX/= 3 ; xmove/= 3 ; } else { deltaX= 0 ; xmove= 0 ; } break ; } } } } setLocation(getX()+deltaX,getY()+deltaY); System.out.println(collided); } static public void setGravityDirection(String s){ gravityDirection = s; } static public void setGravitationalAcceleration( int i){ g = i; } } |
1 2 3 4 | // this Object[] solids = ...; // instead of TileType[] solids = ...; |