I'm trying to program a game where if you hold the mouse down you will continue to shoot. However, the code I'm using only works if you release the mouse before clicking again. Here's the code I have. I know it's a bit messy right now but I'll worry about that later.
Edit: Thank you in advance!
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { GreenfootImage run1= new GreenfootImage( "run1.png" ); GreenfootImage run2= new GreenfootImage( "run2.png" ); GreenfootImage stand= new GreenfootImage( "stand.png" ); GreenfootImage walk1= new GreenfootImage( "walk1.png" ); GreenfootImage walk2= new GreenfootImage( "walk2.png" ); private int imageChangePause= 0 ; public int firePause= 80 ; private int sprintCooldown= 0 ; /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (sprintCooldown> 0 ) { sprintCooldown=sprintCooldown- 1 ; } if (firePause< 80 ) { firePause=firePause+ 1 ; } if (firePause== 80 ) { if (Greenfoot.mousePressed( null ) == true ) { firePause= 0 ; Greenfoot.playSound( "bang.wav" ); getWorld().addObject( new Spark(), 290 , 189 ); } } if (Greenfoot.isKeyDown( "w" ) || Greenfoot.isKeyDown( "a" ) || Greenfoot.isKeyDown( "s" ) || Greenfoot.isKeyDown( "d" ) || Greenfoot.isKeyDown( "space" )) { if (Greenfoot.isKeyDown( "w" )) { if (!Greenfoot.isKeyDown( "shift" )) { if (imageChangePause== 20 ) { imageChangePause= 0 ; if (getImage()==walk2) { setImage(walk1); } else { setImage(walk2); } if (Greenfoot.getRandomNumber( 2 )+ 1 == 1 ) { Greenfoot.playSound( "Walking1.wav" ); } else { Greenfoot.playSound( "Walking2.wav" ); } } else { imageChangePause=imageChangePause+ 1 ; } } if (Greenfoot.isKeyDown( "shift" )) { if (Greenfoot.mousePressed( null ) == false ) { if (imageChangePause== 20 ) { imageChangePause= 0 ; if (getImage()==run2) { setImage(run1); } else { setImage(run2); } if (Greenfoot.getRandomNumber( 2 )+ 1 == 1 ) { Greenfoot.playSound( "Walking1.wav" ); } else { Greenfoot.playSound( "Walking2.wav" ); } } else { imageChangePause=imageChangePause+ 1 ; } } } } } else { setImage(stand); } } } |