I am having trouble animating more than 1 character. I have a character selection screen, and from this, the player chooses 1 character, and then the character is put into the game world. I don't know how to animate the chosen sprite in the game world. Here is my CharSelect World code
Here is my Game code
Here is my hero code
And here is my global variable code
Thanks!
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CharSelect here. * * @author (your name) * @version (a version number or a date) */ public class CharSelect extends World { /** * Constructor for objects of class CharSelect. * */ public CharSelect() { super ( 600 , 400 , 1 ); addObject(Globals.h, 300 , 255 ); prepare(); } public void act() { if (Greenfoot.isKeyDown( "1" )) { Globals.h.changeImage( 1 ); } else if (Greenfoot.isKeyDown( "2" )) { Globals.h.changeImage( 2 ); } else if (Greenfoot.isKeyDown( "3" )) { Globals.h.changeImage( 3 ); } else if (Greenfoot.isKeyDown( "4" )) { Globals.h.changeImage( 4 ); } else if (Greenfoot.isKeyDown( "space" )) { Greenfoot.setWorld( new Game()); } } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Game here. * * @author (your name) * @version (a version number or a date) */ public class Game extends World { private int speed = 7 ; private int vSpeed = 0 ; private int acceleration = 1 ; private int jumpStrenght = - 6 ; private Background img0, img1, img2; /** * Constructor for objects of class Game. * */ public Game() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 1000 , 700 , 1 , false ); // Add New Hero// img0 = new Background(); addObject( img0,getWidth()/ 2 , getHeight()/ 2 ); img1 = new Background(); addObject( img1, getWidth() + getWidth()/ 2 , getHeight()/ 2 ); img2 = new Background(); addObject( img2, getWidth() - getWidth()/ 2 , getHeight()/ 2 ); Hero hero = new Hero(); addObject(Globals.h, 50 , 400 ); addObject( new Long(), 189 , 650 ); addObject( new Hump(), 581 , 572 ); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { } public void act() { if (Greenfoot.isKeyDown( "right" )) { img0.scroll(); img1.scroll(); img2.scroll(); } if (Greenfoot.isKeyDown( "left" )) { img0.scroll(); img1.scroll(); img2.scroll(); } } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Hero here. * * @author (your name) * @version (a version number or a date) */ public class Hero extends Actor { GreenfootImage i1 = new GreenfootImage( "wizard_1.png" ); GreenfootImage i2 = new GreenfootImage( "princess.png" ); GreenfootImage i3 = new GreenfootImage( "goblin_2.png" ); GreenfootImage i4 = new GreenfootImage( "barbarian_1.png" ); int vSpeed = 0 ; int acceleration = 1 ; int speed = 7 ; int jumpStrenght = - 20 ; boolean jumping; /** * Act - do whatever the Hero wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act () { if (getWorld() instanceof CharSelect) return ; checkKeys(); checkFall(); landOnTop(); fireProjectile(); } public void fireProjectile() { if (Greenfoot.isKeyDown( "down" )) { getWorld(). addObject( new Projectile(), getX(), getY()); Projectile projectile = new Projectile(); } } private void checkKeys() { if (Greenfoot.isKeyDown( "up" ) && jumping == false ) { jump(); } } public void checkFall() { if (onGround()) { vSpeed = 0 ; } else { fall(); } } public void jump() { vSpeed= jumpStrenght; fall(); jumping = true ; } public boolean onGround() { int spriteHeight = getImage().getHeight(); int yDistance =( int ) (spriteHeight/ 2 + 5 ); Actor ground = getOneObjectAtOffset( 0 ,getImage().getHeight()/ 4 ,Ground. class ); if (ground == null ) { jumping = true ; return false ; } else { moveToGround(ground); return true ; } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() -(groundHeight + getImage().getHeight()/ 100000000 ); jumping = false ; } public void landOnTop() { if (isTouching(Ground. class )) { setLocation(getX(), getY() - 1 ); } } public void moveRight() { setLocation (getX() + speed,getY() ); } public void moveLeft() { setLocation (getX() - speed,getY()); } public void fall() { setLocation (getX(),getY()+vSpeed); vSpeed = vSpeed + acceleration; jumping = true ; } public Hero() { setImage(i1); } public void changeImage ( int x) { if (x == 1 ) { setImage(i1); } if (x == 2 ) { setImage(i2); } if (x == 3 ) { setImage(i3); } if (x == 4 ) { setImage(i4); } } } |
1 2 3 4 5 6 7 8 9 10 | import lang.stride.*; /** * Write a description of class Globals here. * @author (your name) @version (a version number or a date) */ public class Globals { protected static Hero h = new Hero(); } |