Hi Guys, in the process of doing a Mario game. I've come to a slight stumbling block. I can get Mario to move left and right, but the animation looks slightly off as the frames change to quickly. How do I slow it down? The scenario is located here: http://www.greenfoot.org/scenarios/8881
The code is as follows:
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Mario here. * * @author (your name) * @version (a version number or a date) */ public class Mario extends Actor { private final GreenfootImage RMidle= new GreenfootImage( "mario-idle.gif" ); private final GreenfootImage RMjump= new GreenfootImage( "mario-jump.gif" ); private final GreenfootImage RMwalk1= new GreenfootImage( "mario-walk1.gif" ); private final GreenfootImage RMwalk2= new GreenfootImage( "mario-walk2.gif" ); private final GreenfootImage RMwalk3= new GreenfootImage( "mario-walk3.gif" ); private final GreenfootImage LMidle = new GreenfootImage(RMidle); private final GreenfootImage LMjump = new GreenfootImage(RMjump); private final GreenfootImage LMwalk1 = new GreenfootImage(RMwalk1); private final GreenfootImage LMwalk2 = new GreenfootImage(RMwalk2); private final GreenfootImage LMwalk3 = new GreenfootImage(RMwalk3); private int speed = 3 ; private int frame; private boolean walking; private boolean facingRight; private boolean isKeyPressed; /** * An example of a method - replace this comment with your own */ public Mario() { setImage(RMidle); walking = false ; facingRight = true ; RMidle.scale( 38 , 50 ); RMjump.scale( 51 , 50 ); RMwalk1.scale( 48 , 50 ); RMwalk2.scale( 36 , 50 ); RMwalk3.scale( 44 , 50 ); LMidle.scale( 38 , 50 ); LMjump.scale( 51 , 50 ); LMwalk1.scale( 48 , 50 ); LMwalk2.scale( 36 , 50 ); LMwalk3.scale( 44 , 50 ); LMidle.mirrorHorizontally(); LMjump.mirrorHorizontally(); LMwalk1.mirrorHorizontally(); LMwalk2.mirrorHorizontally(); LMwalk3.mirrorHorizontally(); } /** * Act - do whatever the Mario wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeys(); } /** * An example of a method - replace this comment with your own */ public void checkKeys() { isKeyPressed = false ; if (Greenfoot.isKeyDown( "right" )) { walkRight(); setLocation (getX()+speed, getY()); isKeyPressed = true ; } if (Greenfoot.isKeyDown( "left" )) { walkLeft(); setLocation (getX()-speed, getY()); isKeyPressed = true ; } if (!(isKeyPressed)) { stopWalking(); } } public void walkRight() { walking = true ; facingRight = true ; frame ++; if (frame== 1 ) { setImage(RMidle); } else if (frame== 2 ) { setImage(RMwalk1); } else if (frame== 3 ) { setImage(RMwalk2); } else if (frame== 4 ) { setImage(RMwalk3); frame = 1 ; return ; } } public void walkLeft() { walking = true ; facingRight = false ; frame ++; if (frame== 1 ) { setImage(LMidle); } else if (frame== 2 ) { setImage(LMwalk1); } else if (frame== 3 ) { setImage(LMwalk2); } else if (frame== 4 ) { setImage(LMwalk3); frame = 1 ; return ; } } public void stopWalking() { walking = false ; if (facingRight) setImage (RMidle); else setImage (LMidle); } } |