Hello everyone, I am a beginner in Object Oriented Programming and I have been experiencing a rather strange problem.
What I have is a soldier with an animated image (it uses a loop in an array to animate and I have made sure the hitbox of the individual images are constantly the same, max height and width wise).
All it does now is simply walking and colliding with objects. However, it sometimes collides with an object and crashes my entire Greenfoot program (it won't compile anymore, and won't throw an error).
Here is my code :
The Obstacle Class does not have any instance variables nor methods other than Act(){}, and it is a superclass of two seperate objects that are equally empty (these are the objects Police.class is supposed to collide against).
Any help would be appreciated ;-)
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Police extends Actor { private char dir = 'R' ; public static int XP; public static int YP; private static int imageCounter; GreenfootImage[] imagesL = new GreenfootImage[ 7 ]; GreenfootImage[] imagesR = new GreenfootImage[ 7 ]; GreenfootImage[] imagesU = new GreenfootImage[ 7 ]; GreenfootImage[] imagesD = new GreenfootImage[ 7 ]; public void act() { walk(); animate(); if (Greenfoot.isKeyDown( "left" ) || Greenfoot.isKeyDown( "right" ) || Greenfoot.isKeyDown( "up" ) || Greenfoot.isKeyDown( "down" )){ animation(); } getXPosition(); getYPosition(); } public void walk(){ int x = getX(); int y = getY(); if (Greenfoot.isKeyDown( "up" )){ if (getOneObjectAtOffset( 0 , -(getImage().getHeight()/ 2 )- 4 , Obstacle. class ) == null ){ setLocation(x, y- 4 ); } else { while (getOneObjectAtOffset( 0 , -(getImage().getHeight()/ 2 )- 1 , Obstacle. class ) == null ){ setLocation (x, y- 1 ); } } dir = 'U' ; } if (Greenfoot.isKeyDown( "down" )){ if (getOneObjectAtOffset( 0 , (getImage().getHeight()/ 2 )+ 4 , Obstacle. class ) == null ){ setLocation(x, y+ 4 ); } else { while (getOneObjectAtOffset( 0 , (getImage().getHeight()/ 2 )+ 1 , Obstacle. class ) == null ){ setLocation(x, y+ 1 ); } } dir = 'D' ; } if (Greenfoot.isKeyDown( "left" )){ if (getOneObjectAtOffset(-(getImage().getWidth()/ 2 )- 4 , 0 , Obstacle. class ) == null ){ setLocation(x- 4 , y); } else { while (getOneObjectAtOffset(-(getImage().getWidth()/ 2 )- 1 , 0 , Obstacle. class ) == null ){ setLocation(x- 1 , y); } } dir = 'L' ; } if (Greenfoot.isKeyDown( "right" )){ if (getOneObjectAtOffset((getImage().getWidth()/ 2 )+ 4 , 0 , Obstacle. class ) == null ){ setLocation(x+ 4 , y); } else { while (getOneObjectAtOffset((getImage().getWidth()/ 2 )+ 1 , 0 , Obstacle. class ) == null ){ setLocation(x+ 1 , y); } } dir = 'R' ; } } public void animate(){ for ( int i= 0 ; i<imagesD.length; i++ ) { switch (dir){ case 'D' : imagesD[i] = new GreenfootImage( "P_D_" + i + ".png" ); setImage( imagesD[imageCounter] ); break ; case 'U' : imagesU[i] = new GreenfootImage( "P_U_" + i + ".png" ); setImage( imagesU[imageCounter] ); break ; case 'L' : imagesL[i] = new GreenfootImage( "P_L_" + i + ".png" ); setImage( imagesL[imageCounter] ); break ; case 'R' : imagesR[i] = new GreenfootImage( "P_R_" + i + ".png" ); setImage( imagesR[imageCounter] ); break ; } } } public void animation() { switch (dir){ case 'D' : imageCounter = ( imageCounter + 1 ) % imagesD.length; setImage( imagesD[imageCounter] ); break ; case 'U' : imageCounter = ( imageCounter + 1 ) % imagesU.length; setImage( imagesU[imageCounter] ); break ; case 'L' : imageCounter = ( imageCounter + 1 ) % imagesL.length; setImage( imagesL[imageCounter] ); break ; case 'R' : imageCounter = ( imageCounter + 1 ) % imagesR.length; setImage( imagesR[imageCounter] ); break ; } } public int getXPosition(){ XP = getX(); return XP; } public int getYPosition(){ YP = getY(); return YP; } } |