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

2014/9/16

My ship will not move backwards and forwards

coder04 coder04

2014/9/16

#
Can you correct my code
import greenfoot.*;

/**
 *  A user-controlled spaceship that can be teleported from one portal to another
 *  This class uses a somewhat make-shift move algorithm with drift slowing to stop
 *  (although, in space, this kind of slowing is not present)
 */
public class Ship extends Actor
{
    int speed = 5;
    /**
     * Method act: move spaceship and check teleporting status
     */
    public void act() 
    {
        move();
    }
    
    /**
     * Method move: checks for keystrokes and applies the changes, then moves the ship.
     * I applied a bit of slowing to the ship's speed (so it would be drifting to a stop)
     */
    private void move()
    {
        int dz = 0;
        if (Greenfoot.isKeyDown("right")) dz++;
        if (Greenfoot.isKeyDown("left")) dz--;
        // apply change in rotation
        setRotation(getRotation() + dz * 5);
        // get change in speed
        int ds = -1;
        if (Greenfoot.isKeyDown("up")) ds += 2;
        // adjust speed
        speed += ds;
        if (speed < 0) speed = 0;
        if (speed > 90) speed = 10;
        // and move
        if (speed >= 200) move(speed / 100);
        if (Greenfoot.isKeyDown("down")) ds += 5;
        // adjust speed
        speed += ds;
        if (speed < 5) speed = 5;
        if (speed > 200) speed = 200;
        // and move
        if (speed >= 20) move(speed / -10);
    }
}
Super_Hippo Super_Hippo

2014/9/16

#
First, you probably should use another method name, since 'move' already exists and this might confuse you once. line 38 is useless. The condition will never be true. Because if speed would be 200+, it would be set to 10 in line 36. Since you would only move if the speed is 200 or higher, it will not move. Look, the speed is 5 at the beginning. (Maybe add a 'private' in line 10.) If you press 'up', then 'ds' will be 1. If not, it is -1. So after line 34, speed will be 4 or 6. Lines 35 through 38 are not executed. Line 39 probably wants to decrease 'ds' instead of increasing it. What you may want to do, is something like this:
private int speed = 5; //speed
private int ds = 0; //velocity

public void act()
{
    checkKeypress();
}

public void checkKeypress()
{
    int dz = 0;
    if (Greenfoot.isKeyDown("right")) dz++;
    if (Greenfoot.isKeyDown("left")) dz--;
    setRotation(getRotation() + dz * 5);
    
    ds--; //losing velocity over time
    if (Greenfoot.isKeyDown("up")) ds += 2; //acceleration
    if (Greenfoot.isKeyDown("down")) ds -= 5; //braking
    speed += ds;
    move(speed);
}
davmac davmac

2014/9/16

#
Super_Hippo,
private int speed = 5; //speed  
private int ds = 0; //velocity  
This doesn't make sense - speed and velocity are the same thing (technically, velocity includes speed and direction). I think you're misunderstanding the purpose of the 'ds' variable. Lines 30 through 32 of the original code are:
        // get change in speed  
        int ds = -1;  
        if (Greenfoot.isKeyDown("up")) ds += 2;  
The purpose here is to set ds to -1 unless 'up' is pressed, in which case ds is set to 1 (being -1 + 2). ds represents acceleration, not velocity. If boost is applied, acceleration is positive; otherwise, it is negative (to slowly reduce speed).
Look, the speed is 5 at the beginning.
The declaration is not inside the method. speed is set to 5 when the object is created.
If you press 'up', then 'ds' will be 1. If not, it is -1. So after line 34, speed will be 4 or 6.
The first part is correct ('ds' will be 1 or -1). The second part is not. The current value of speed with either have 1 added or 1 subtracted.
Lines 35 through 38 are not executed.
Not true. I think what you meant is they will have no effect, but that's not correct either.
Super_Hippo Super_Hippo

2014/9/16

#
Ok true, the thing with the 'velocity' comment was dumb. I did mean the changing of speed, so yes, that is more like called acceleration. Sorry for that. With 'at the beginning', I didn't mean at the beginning of the method. I meant the first act cycle. And that is what the following (4 or 6) was referring to. True, they will have effect somehow. The condition in line 35 would be in fact never be true. (Speed will be set 5 if it is less than 5 in line 42 so it will never be less than 0 in line 35.) Only line 36 could be true, but only after the rocket already moved backwards for quite a while. When you press up (or down, both is accelerating the rocket), you will move the rocket backwards after a while, but it will never move forward.
danpost danpost

2014/9/16

#
After line 36 is executed, the value of 'speed' can be no more than '10'; so, line 38 will never be true. The range in the value of 'speed' at this point (when taking line 35 into account) is minimum '0' and maximum '10' After line 39 is executed, the possible values for 'ds' are { -1, 1, 4, 6 }. Now, the 'speed' range after line 41 is minimum '-1' and maximum '16'. Both conditions on lines 43 and 45 will never be true. The range of speed at the beginning of each act will therefore be minimum '5' (due to line 42) and maximum '16'. This means the range after line 34 is executed is minimum '4' and maximum '17'. Therefore, the conditions on lines 35 and 36 will never be true. Ok, lines 35, 36, 38, 43, and 45 all have conditions that will never be true and will not perform any actions. Since lines 38 and 45 have the 'move' commands in them, it is no wonder your ship does not move.
danpost danpost

2014/9/16

#
Now, it appears you tried to use code similar to the Ship in my Teleporting Demo scenario. Maybe try something like this for your 'move' method:
private void move()
{
    // apply change in rotation
    int dz = 0;
    if (Greenfoot.isKeyDown("right")) dz++;
    if (Greenfoot.isKeyDown("left")) dz--;
    setRotation(getRotation() + dz * 5);
    // get change in speed
    int ds = -1;
    if (Greenfoot.isKeyDown("up")) ds += 2;
    if (Greenfoot.isKeyDown("down")) ds -= 2;
    // adjust speed
    speed += ds;
    // limit speed
    if (speed < 0) speed = 0;
    if (speed > 90) speed = 90;
    // and move
    if (speed >= 10) move(speed / 10);
}
You need to login to post a reply.