I'm trying to make a game with a cat that shoots lasers. The cat is supposed to shoot a short laser segment when the spacebar is pressed that is created again and again until it hits a wall, then stops. I haven't yet written the code to make the laser disappear after it's shot, but I think it should still work. The game functions fine until the spacebar is pressed, then it freezes. I assume that I'm causing some kind of infinite loop where the laser doesn't realize that it hit a wall and keeps on going forever, but I can't figure out what's causing it. Can someone please help?
LaserCat class:
Laser class:
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 | import greenfoot.*; /** * Write a description of class LaserCat here. * * @author (your name) * @version (a version number or a date) */ public class LaserCat extends Actor { private GreenfootImage Down1 = new GreenfootImage( "KnightDog7.png" ); private GreenfootImage Down2 = new GreenfootImage( "KnightDog8.png" ); private GreenfootImage Up1 = new GreenfootImage( "KnightDog1.png" ); private GreenfootImage Up2 = new GreenfootImage( "KnightDog2.png" ); private GreenfootImage Left1 = new GreenfootImage( "KnightDog3.png" ); private GreenfootImage Left2 = new GreenfootImage( "KnightDog4.png" ); private GreenfootImage Right1 = new GreenfootImage( "KnightDog5.png" ); private GreenfootImage Right2 = new GreenfootImage( "KnightDog6.png" ); private int animationSpeed = 10 ; private int frame = 1 ; private int direction; private int timer = 0 ; int catDirection = 2 ; public void act() { checkKey(); checkFire(); } private int speed = 1 ; public void checkKey() { if (Greenfoot.isKeyDown( "down" )) { catDirection = 2 ; goDown(); } if (Greenfoot.isKeyDown( "up" )) { catDirection = 1 ; goUp(); } if (Greenfoot.isKeyDown( "left" )) { catDirection = 3 ; goLeft(); } if (Greenfoot.isKeyDown( "right" )) { catDirection = 4 ; goRight(); } } public void goDown() { timer++; setLocation(getX(),getY() + speed); if (frame == 1 && timer % 5 == 0 ) { setImage(Down1); frame = 2 ; } else if (frame == 2 && timer % 5 == 0 ) { setImage(Down2); frame = 1 ; } } public void goUp() { timer++; setLocation(getX(),getY() - speed); if (frame == 1 && timer % 5 == 0 ) { setImage(Up1); frame = 2 ; } else if (frame == 2 && timer % 5 == 0 ) { setImage(Up2); frame = 1 ; } } public void goRight() { timer++; setLocation(getX() + speed,getY()); if (Greenfoot.isKeyDown( "down" ) == false && Greenfoot.isKeyDown( "left" ) == false && Greenfoot.isKeyDown( "up" ) == false ) { if (frame == 1 && timer % 5 == 0 ) { setImage(Right1); frame = 2 ; } else if (frame == 2 && timer % 5 == 0 ) { setImage(Right2); frame = 1 ; } } } public void goLeft() { timer++; setLocation(getX() - speed,getY()); if (Greenfoot.isKeyDown( "down" ) == false && Greenfoot.isKeyDown( "right" ) == false && Greenfoot.isKeyDown( "up" ) == false ) { if (frame == 1 && timer % 5 == 0 ) { setImage(Left1); frame = 2 ; } else if (frame == 2 && timer % 5 == 0 ) { setImage(Left2); frame = 1 ; } } } public void checkFire() { if (Greenfoot.getKey() == "space" ) { shootLaser(); } } public int getDirection() { return catDirection; } private int laserLength = 0 ; public void shootLaser() { while (Laser.noWallDetected == true ) { getWorld().addObject( new Laser( this ), getX(), getY() + laserLength); laserLength += 2 ; } laserLength = 0 ; } } |
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 | import greenfoot.*; /** * Write a description of class Laser here. * * @author (your name) * @version (a version number or a date) */ public class Laser extends Actor { private LaserCat myCat; private boolean checkedForWall = false ; public static boolean noWallDetected = true ; public Laser(LaserCat myCat) { this .myCat = myCat; } public void act() { if (checkedForWall == false ) { if (wallDetected()) noWallDetected = false ; else noWallDetected = true ; checkedForWall = true ; } } public boolean wallDetected() { Actor a = getOneIntersectingObject(Wall. class ); if ( this .isAtEdge() || a != null ) return true ; else return false ; } } |