However, now neither gravity nor hit detection work and only the standing animation plays, movment animations do not play.


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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * This class is just an example. You can delete it or change the code. * It's not necessary for the scrolling system. */ public class Azure extends ScrollingActor { private int vSpeed = 0 ; private int acceleration = 1 ; private boolean jumping; private int jumpStrength = 20 ; private static GreenfootImage[] standAnim = new GreenfootImage[ 9 ]; private static GreenfootImage[] rightAnim = new GreenfootImage[ 11 ]; private static GreenfootImage[] leftAnim = new GreenfootImage[ 11 ]; static { for ( int i= 0 ; i<standAnim.length; i++) standAnim[i] = new GreenfootImage( "Stand_0" +(i+ 1 )+ ".png" ); for ( int i= 0 ; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage( "Walk-A_" +(i < 10 ? "0" : "" )+(i+ 1 )+ ".png" ); for ( int i= 0 ; i<rightAnim.length; i++) leftAnim[i] = new GreenfootImage( "WalkLeft-A_" +(i < 10 ? "0" : "" )+(i+ 1 )+ ".png" ); } private int frame = 1 ; // with the following fields private int animCount; // counter for animations private int animDelay = 6 ; // initially set for standing animation (frame rate field) // this method might help private void setAnim(GreenfootImage[] anim, int frameRate) { animSet = anim; animCount = - 1 ; // prepares to set first image when incremented animDelay = frameRate; animate(); // calls a method to set a frame of this newly set animation } private int Counter = 0 ; private GreenfootImage[] animSet = standAnim; /** * Here you can tell your actor what he has to do. */ public void act() { animate(); move(); Counter(); ceiling(); } public void Counter() { if (Greenfoot.isKeyDown( "right" )) Counter = 0 ; if (Greenfoot.isKeyDown( "left" )) Counter = 1 ; } public void move() { int y = getY(); int x = getX(); if (Greenfoot.isKeyDown( "right" ) ) x+= 6 ; if (Greenfoot.isKeyDown( "left" ) ) x-= 6 ; setLocation(x,y); } public void fall() { setLocation(getX(), getY() +vSpeed); { vSpeed = vSpeed + acceleration; } jumping = true ; } public void onGround() { boolean onGround = false ; // assume in air vSpeed += acceleration; // apply gravity setLocation(getX(), getY()+vSpeed); // fall Actor actor = getOneIntersectingObject(Actor. class ); if (actor != null ) { // colliding with another actor int vDir = ( int )Math.signum(vSpeed); // vertical direction of movement vSpeed = 0 ; // kill vertical speed if (vDir > 0 ) onGround = true ; // landing and not bopping head setLocation(getX(), actor.getY()-vDir*(actor.getImage().getHeight()+getImage().getHeight())/ 2 + 30 ); // assigning stop location } if (onGround && Greenfoot.isKeyDown( "space" )) vSpeed = - 20 ; // initiate a jump } public boolean ceiling() { int spriteHeight = getImage().getHeight(); int yDistance = ( int ) (spriteHeight/- 2 - 30 ); Actor ceiling = getOneObjectAtOffset( 0 , yDistance, Highway. class ); if (ceiling != null ) { vSpeed = 1 ; return true ; } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/ 2 + 30 ; setLocation(getX(), newY); jumping = false ; } public void jump() { vSpeed = vSpeed - jumpStrength; jumping = true ; fall(); } private void animate() { animCount = (animCount+ 1 )%(animSet.length*animDelay); if (animCount%animDelay == 0 ) setImage(animSet[animCount/animDelay]); } } |
1 | moveVertically(); |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * This class is just an example. You can delete it or change the code. * It's not necessary for the scrolling system. */ public class Azure extends ScrollingActor { private int vSpeed = 0 ; private int acceleration = 1 ; private int jumpStrength = 20 ; private static GreenfootImage[] standAnim = new GreenfootImage[ 9 ]; private static GreenfootImage[] rightAnim = new GreenfootImage[ 11 ]; private static GreenfootImage[] leftAnim = new GreenfootImage[ 11 ]; static { for ( int i= 0 ; i<standAnim.length; i++) standAnim[i] = new GreenfootImage( "Stand_0" +(i+ 1 )+ ".png" ); for ( int i= 0 ; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage( "Walk-A_" +(i < 10 ? "0" : "" )+(i+ 1 )+ ".png" ); for ( int i= 0 ; i<rightAnim.length; i++) leftAnim[i] = new GreenfootImage( "WalkLeft-A_" +(i < 10 ? "0" : "" )+(i+ 1 )+ ".png" ); } private int animCount; // counter for animations private int animDelay = 6 ; // initially set for standing animation (frame rate field) private void setAnim(GreenfootImage[] anim, int frameRate) { animSet = anim; animCount = - 1 ; // prepares to set first image when incremented animDelay = frameRate; animate(); // calls a method to set a frame of this newly set animation } private GreenfootImage[] animSet = standAnim; /** * Here you can tell your actor what he has to do. */ public void act() { animate(); move(); moveVertically(); } public void move() { int y = getY(); int x = getX(); if (Greenfoot.isKeyDown( "right" ) ) x+= 6 ; if (Greenfoot.isKeyDown( "left" ) ) x-= 6 ; setLocation(x,y); } public void moveVertically() { boolean onGround = false ; // assume in air vSpeed += acceleration; // apply gravity setLocation(getX(), getY()+vSpeed); // fall Actor actor = getOneIntersectingObject(Actor. class ); if (actor != null ) { // colliding with another actor int vDir = ( int )Math.signum(vSpeed); // vertical direction of movement vSpeed = 0 ; // kill vertical speed if (vDir > 0 ) onGround = true ; // landing and not bopping head setLocation(getX(), actor.getY()-vDir*(actor.getImage().getHeight()+getImage().getHeight())/ 2 + 30 ); // assigning stop location } if (onGround && Greenfoot.isKeyDown( "space" )) vSpeed = - 20 ; // initiate a jump } private void animate() { animCount = (animCount+ 1 )%(animSet.length*animDelay); if (animCount%animDelay == 0 ) setImage(animSet[animCount/animDelay]); } } |