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

2014/5/21

Why isn't this working?

mandyg233 mandyg233

2014/5/21

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class Sled here. * * @author (your name) * @version (a version number or a date) */ public class Sled extends Actor { private int Walk = (0); private static final String IMAGES = {"BobSled1000.png", "BobSledRight2000.png"}; private static final int RATE = 3; private static GreenfootImage backgroundarray = new GreenfootImage; private static GreenfootSound soundSound = new GreenfootSound("crack.mp3"); private boolean soundPlayed; // public int getVariableValue() // { // return variable; // } public Sled() { if(backgroundarray == null) { backgroundarray = new GreenfootImage("Blinklala1.png"); backgroundarray = new GreenfootImage("Blinklala2.png"); // Boat boat = new Boat(); // addObject(boat, 1800,300); // getWorld().addObject(new Boat(), 1800, 300); } } /** * Act - do whatever the Sled wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { doStuff(); } public void doStuff() { move(15); Walk = (Walk+1)%(RATE*IMAGES.length); if (Walk%RATE==0) { setImage(IMAGES); } if (getX() == 115 && getY() == 330) { turn(1); } if (getX() == 520 && getY() == 330) { turn(-7); } if (getX() == 820 && getY() == 290) { turn(6); } if (getX() == 1210 && getY() == 300) { turn(8); } if (getX() == 1030 && getY() == 290) { turn(2); } if (getX() == 1225 && getY() == 303) { turn(1); } if (getX() == 1420 && getY() == 316) { turn(-4); } // Actor boaty = getOneObjectAtOffset(-615,-30, Boat.class); // if ((getX() >1800) && boaty!= null) if (getX() == 1800) // && !soundPlayed) { // soundSound.play(); // soundPlayed = true; getWorld().removeObjects(getWorld().getObjects(Boat.class)); getWorld().setBackground(new GreenfootImage("Blinklala1.png")); Greenfoot.delay(20); getWorld().setBackground(new GreenfootImage("Blinklala2.png")); Greenfoot.delay(20); getWorld().setBackground(new GreenfootImage("Blinklala1.png")); Greenfoot.delay(30); getWorld().setBackground(new GreenfootImage("BackgroundImage.png")); // getWorld().addObject(boat, 1185, 300); getWorld().addObject(new Boat(), 1184,300); Greenfoot.delay(10); FWorld.label1.setText("What was that?"); Greenfoot.delay(30); FWorld.label1.setText("..."); getWorld().addObject (new Victor(), 1800, 565); } } }
mandyg233 mandyg233

2014/5/21

#
if (getX() == 1800) never seems to happen
mandyg233 mandyg233

2014/5/21

#
^^ I got it working, but I'm still not sure why it previously did not work. Any ideas?
davmac davmac

2014/5/21

#
When you post here there is a link below the text area where you type your post that says "Posting code? read this!" - please click that and learn how to use 'code' tags when you post code. It's just about impossible to copy and paste code from your posts otherwise, and that makes it very difficult to try the code in Greenfoot.
danpost danpost

2014/5/21

#
mandyg233 wrote...
if (getX() == 1800) never seems to happen
With a move speed of '15', chances are quite good, in fact, very strong, that a move will take the actor from under the 1800 mark to over the 1800 mark without actually landing at that mark.
You need to login to post a reply.