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

2012/9/12

The world does not create, Unable to press "act", "play" or "reset"

Stormtrooper299 Stormtrooper299

2012/9/12

#
The world doesn't even create, please help. I've attached all the code down below, MMyWorld port greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; public class MyWorld extends World { public MyWorld() { super(600,400, 1); GreenfootImage background = getBackground(); background.setColor(Color.WHITE); background.fill(); addObject(new Survivor(), 300, 200); zombieSpawner(); } public void zombieSpawner() { addObject(new Zombie(), Greenfoot.getRandomNumber(100),Greenfoot.getRandomNumber(100)); addObject(new Zombie(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(100)); } } Explosion import greenfoot.*; // Explosion or, GreenfootImage, and Greenfoot) import java.util.*; public class Explosion extends Actor { private final static int IMAGE_COUNT= 8; private static GreenfootImage images; private int imageNo = 0; private int increment=1; public Explosion() { initialiseImages(); setImage(images); Greenfoot.playSound("Explosion.wav"); } public synchronized static void initialiseImages() { if(images == null) { GreenfootImage baseImage = new GreenfootImage("explosion-big.png"); int maxSize = baseImage.getWidth(); int delta = maxSize / IMAGE_COUNT; int size = 0; images = new GreenfootImage; for(int i=0; i < IMAGE_COUNT; i++) { size = size + delta; images = new GreenfootImage(baseImage); images.scale(size, size); } } } public void act() { setImage(images); imageNo += increment; if(imageNo >= IMAGE_COUNT) { increment = -increment; imageNo += increment; } if(imageNo < 0) { getWorld().removeObject(this); } } } MoverMover import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public abstract class Mover extends Actor { private Vector movement = new Vector(); private double x = 0; private double y = 0; public Mover() { } /** * Create new thing initialised with given speed. */ public Mover(Vector speed) { movement = speed; } /** * Move forward one step. */ public void move() { x = x + movement.getX(); y = y + movement.getY(); if(x >= getWorld().getWidth()) { x = 0; } if(x < 0) { x = getWorld().getWidth() - 1; } if(y >= getWorld().getHeight()) { y = 0; } if(y < 0) { y = getWorld().getHeight() - 1; } setLocation(x, y); } public void setLocation(double x, double y) { this.x = x; this.y = y; super.setLocation((int) x, (int) y); } public void setLocation(int x, int y) { setLocation((double) x, (double) y); } /** * Increase the speed with the given vector. */ public void increaseSpeed(Vector s) { movement.add(s); } /** * Return the current movement. */ public Vector getMovement() { return movement; } } BulletBullet import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Bullet extends Mover { /** The damage this bullet will deal */ private static final int damage = 16; /** A bullet looses one life each act, and will disappear when life = 0 */ private int life = 30; private boolean atWorldEdge = false; public Bullet() { } public Bullet(Vector speed, int rotation) { super(speed); setRotation(rotation); increaseSpeed(new Vector(rotation, 15)); Greenfoot.playSound("EnergyGun.wav"); } /** * The bullet will damage asteroids if it hits them. */ public void act() { if(life <= 0) { getWorld().removeObject(this); } else { life--; move(); checkZombieHit(); } } /** * Check whether we have hit an Zombie. */ private void checkZombieHit() { Zombie zombie = (Zombie) getOneIntersectingObject(Zombie.class); if (zombie != null){ getWorld().removeObject(this); zombie.hit(damage); } } } SurvivorSurvivor import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A survivor of the zombie apocolypse, control using a,w,s,d. * The gun is fired by hitting the 'space' key. * * @author Dallin Wrathall */ public class Survivor extends Mover { private int gunReloadTime; private int reloadDelayCount; private Vector acceleration; public int shotsFired; public static Survivor main; public static boolean survivorAlive; private GreenfootImage rocket = new GreenfootImage("rocket.png"); private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png"); /** * */ public Survivor() { main = this; gunReloadTime = 30; reloadDelayCount = 0; shotsFired = 0; survivorAlive = false; } /** * Do what a Survivor's gotta do. (Which is:shooting when the rig keys are pressed.) */ public void act() { survivorAlive = true; MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseMoved(null)) setRotation(getRotationToPoint(mouse.getX(), mouse.getY())); move(); checkCollision(); checkKeys(); reloadDelayCount++; } public int getShotsFired() { return shotsFired; } public void setGunReloadTime(int reloadTime) { gunReloadTime = reloadTime; } private void checkCollision() { Zombie a = (Zombie) getOneIntersectingObject(Zombie.class); if(a != null) { getWorld().addObject(new Explosion(), getX(), getY()); survivorAlive = false; getWorld().removeObject(this); } } /** * Check whether there are any key pressed and react to them. */ private void checkKeys() { if(Greenfoot.isKeyDown("w")) { setLocation(getX(),(getY()-2)); } if(Greenfoot.isKeyDown("a")) { setLocation(getX() - 2, getY()); } if(Greenfoot.isKeyDown("d")) { setLocation(getX() + 2, getY()); } if (Greenfoot.isKeyDown("s")) { setLocation(getX(),(getY()+2)); } if(Greenfoot.isKeyDown("space")) { fire(); } } private void ignite(boolean boosterOn) { if(boosterOn) { setImage(rocketWithThrust); acceleration.setDirection(getRotation()); increaseSpeed(acceleration); } else { setImage(rocket); } } /** * Fire a bullet if the gun is ready. */ private void fire() { if(reloadDelayCount >= gunReloadTime) { Bullet b = new Bullet(getMovement().copy(), getRotation()); getWorld().addObject(b, getX(), getY()); b.move(); shotsFired++; reloadDelayCount = 0; } } public int getRotationToPoint(int x, int y) { int angle = 1; if (getX() > x && getY() > y) { int opp = getY() - y; int adj = getX() - x; angle = (int)(180 + Math.toDegrees(Math.atan(opp/adj))); } else if (getX() > x && getY() < y) { int opp = y - getY(); int adj = getX() - x; angle = (int)(180 - Math.toDegrees(Math.atan(opp/adj))); } else if (getX() < x && getY() > y) { int opp = getY() - y; int adj = x - getX(); angle = (int)(0 - Math.toDegrees(Math.atan(opp/adj))); } else if (getX() < x && getY() < y) { int opp = y - getY(); int adj = x - getX(); angle = (int)(360 + Math.toDegrees(Math.atan(opp/adj))); } return angle; } } ZombieZombie import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Zombie extends Mover { /** Size of this Zombie */ private int size; /** When the Zombie's life reaches 0 the Zombie will die */ private int stability; public Zombie() { this(64); } /** * Create an Zombie with a given size and default speed. */ public Zombie(int size) { size = Greenfoot.getRandomNumber(200); } /** * Create an Zombie with a given size size and speed. */ private Zombie(int size, Vector speed) { super(speed); setSize(size); } /** * Let the Zombie act. That is: go to Survivor. */ public void act() { move(2); turnTowards(Survivor.main.getX(), Survivor.main.getY()); } /** * Set the size of this Zombie. */ public void setSize(int size) { stability = size; this.size = size; GreenfootImage image = getImage(); image.scale(size, size); } /** * Return the current stability of this Zombie. (If it goes down to * zero, it breaks up.) */ public int getStability() { return stability; } /** * Hit this Zombie dealing the given amount of damage. */ public void hit(int damage) { stability = stability - damage; if(stability <= 0) getWorld().removeObject(this); } } And yes, If you noticed, I did start off using the astroid scenario
Gevater_Tod4711 Gevater_Tod4711

2012/9/12

#
I think your code is ok. The part in which the world is created is the constructor of your world. everything there is ok. So I don't think that your code is the problem. Possible answers could be: - If you haven't compiled the classes there is no new World. - If you've got to many classes then Greenfoot can't open (it have to be more than 150 I think) - Your Greenfoot Version could be buggy. Whitch version do you use?
nccb nccb

2012/9/12

#
The only reason why I could see that it might not create the world successfully is if the rocket or rocketWithThrust images are missing. If this is the case, you should at least see an error in the console -- see if an extra window has been created, called "Terminal" (or similar), which may well have an error in it.
Gevater_Tod4711 Gevater_Tod4711

2012/9/12

#
Yes that could also be the case. Doe's the compiler give any errors?
danpost danpost

2012/9/12

#
It looks like you are missing two characters at the beginning of the class. import should be the first word. Also, in the Explosion class, remove the second line 'or GreenfootImage, and Greenfoot)'.
Stormtrooper299 Stormtrooper299

2012/9/13

#
@Grevater_tod4711 -I have compiled the classes, its wierd it came out of no where. -I only have 5 classes, so I don't think that is the problem... -Version: 2.2.2 -I have not been getting any errors when compiling @danpost -Sorry that was a copy and paste mishap, its commented out. @nccb -I just tried your suggestion, and we have a winner! haha. I took out the set images and the varibles having to do with the images and it worked. Annoying that it doesn't give me an error.
davmac davmac

2012/9/13

#
Can we get a copy of your Greenfoot? We've only released 2.2.1 so far, if you have 2.2.2 already then it will save us a lot of development effort! :D
You need to login to post a reply.