This site requires JavaScript, please enable it in your browser!
Greenfoot back
kexsas321
kexsas321 wrote ...

2013/6/5

level select

kexsas321 kexsas321

2013/6/5

#
me Level1 code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Level1 here. * * @author (your name) * @version (a version number or a date) */ public class Level1 extends World { /** * Constructor for objects of class Level1. * */ public Level1() { super(600, 400, 1); prepare(); { Greenfoot.setWorld(new Level2()); } } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { ground ground = new ground(); addObject(ground, 65, 395); ground ground2 = new ground(); addObject(ground2, 227, 356); ground.setLocation(58, 341); ground2.setLocation(210, 364); ground ground3 = new ground(); addObject(ground3, 367, 348); ground ground4 = new ground(); addObject(ground4, 488, 302); ground ground5 = new ground(); addObject(ground5, 378, 241); ground ground6 = new ground(); addObject(ground6, 269, 198); ground ground7 = new ground(); addObject(ground7, 147, 151); ground ground8 = new ground(); addObject(ground8, 71, 100); ground ground9 = new ground(); addObject(ground9, 256, 81); ground ground10 = new ground(); addObject(ground10, 378, 88); ground ground11 = new ground(); addObject(ground11, 515, 97); ground10.setLocation(371, 80); ground11.setLocation(485, 79); ground ground12 = new ground(); addObject(ground12, 533, 147); ground12.setLocation(533, 147); ground12.setLocation(533, 147); ground12.setLocation(533, 147); ground12.setLocation(533, 147); ground12.setLocation(589, 81); ground12.setLocation(599, 79); Player2 player2 = new Player2(); addObject(player2, 50, 313); } } Level2 code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Level2 here. * * @author (your name) * @version (a version number or a date) */ public class Level2 extends World { /** * Constructor for objects of class Level2. * */ public Level2() { super(600, 400, 1); 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() { ground ground = new ground(); addObject(ground, 116, 373); ground ground2 = new ground(); addObject(ground2, 337, 270); ground ground3 = new ground(); addObject(ground3, 495, 199); Player2 player2 = new Player2(); addObject(player2, 102, 319); ground2.setLocation(288, 316); ground3.setLocation(444, 250); } } Actor code: import greenfoot.*; public class Player2 extends Actor { private int vSpeed = 3; private int acceleration = 1; private int jumpSpeed = 10; private boolean goingLeft = true; GreenfootImage gr = getImage(); private int count = 1; public void act() { checkKeys(); checkFall(); count++; } public void checkKeys() { if(Greenfoot.isKeyDown("left")) { if(goingLeft) { gr.mirrorHorizontally(); goingLeft=false; } this.setLocation(getX()-3,getY()); } if(Greenfoot.isKeyDown("right")) { if(!goingLeft) { gr.mirrorHorizontally(); goingLeft=true; } this.setLocation(getX()+3,getY()); } if(Greenfoot.isKeyDown("space")) { jump(); } } public void checkFall() { if(onGround()) { vSpeed = 1; } else { fall(); } } public boolean onGround() { int spriteHeight = getImage().getHeight(); int yDistance = (int) (spriteHeight / 2 + 5); Actor ground = getOneObjectAtOffset(0,getImage().getHeight()/2,ground.class); if(ground == null) { return false; } else { return true; } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); } public void fall() {if(count%2==0){ if(!onGround()) { vSpeed= vSpeed + acceleration; this.setLocation(getX(), getY() + vSpeed); }} } public void jump() { if(onGround()) { jumpSpeed = -90; this.setLocation(getX(), getY() + jumpSpeed); } } private boolean verSpeed() { if(vSpeed>=1) { return true; } else { return false; } } } ground code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class ground here. * * @author (your name) * @version (a version number or a date) */ public class ground extends Actor { /** * Act - do whatever the ground wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(0); } } and how to make myltiple levels seleckt or a just multiple levels? i not very good for engilish so u can just write code.
uanimal uanimal

2013/6/6

#
{ Greenfoot.setWorld(new Level2()); } set new world in player code
You need to login to post a reply.