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

2011/10/17

Moving

ardwennem2 ardwennem2

2011/10/17

#
How can I GLIDE for instance 10 boxes to the right with only pressing a button once, and not holding it down. (Important: it has to glide, it musn't jump)
ardwennem2 ardwennem2

2011/10/17

#
what I tried to was this: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Wombat here. * * @author (your name) * @version (a version number or a date) */ public class Wombat extends Actor { public void act() { int oldX = getX(); int oldY = getY(); glide(oldX, oldY); checkKeyPressed(); } public void checkKeyPressed() { if (Greenfoot.getKey() == "up") { setLocation(getX(), getY() - 1); setRotation(270); } else if (Greenfoot.getKey() == "down") { setLocation(getX(), getY() + 1); setRotation(90); } else if (Greenfoot.getKey() == "left") { setLocation(getX() - 1, getY()); setRotation(0); } else if (Greenfoot.getKey() == "right") { setLocation(getX() + 1, getY()); setRotation(0); } } public void glide(int oldX, int oldY) { if (getX() != getX()/10*10) { if (getX() < oldX) { setLocation(getX() - 1, getY()); } if (getX() > oldX) { setLocation(getX() + 1, getY()); } } if (getY() != getY()/10*10) { if (getY() < oldY) { setLocation(getX(), getY() - 10); } if (getX() > oldX) { setLocation(getX(), getY() + 10); } } } } but in this there are 2 problems: I can only go up, and my gliding doesn't work
danpost danpost

2011/10/17

#
The gliding does not work because 'getX() != getX()/10*10' and 'getY() != getY()/10*10' will always return false, and besides that, before calling glide you set 'oldX = getX()' and 'oldY = getY()'. Therefore ALL of your 'if's within glide are returning false, ALL the time. What is glide supposed to do, exactly (in detail). Also glide is coded to move ten (10) units at a time vertically and only one (1) unit at a time horizontally -- is that what you intended? The reason you can only go 'up' is you are calling Greenfoot.getKey() multiple times and will only return a non-null value the first time if there is a value. You need to set Greenfoot.getKey() to a String variable and then check the value of the variable for 'up', 'down', 'left', and 'right'. Also, do not use '==' when comparing strings. Instead, us the ' "string".equals(String key)' method. Example 1: String myKey = Greenfoot.getKey(); // saves keystroke to string variable 'myKey' Example 2: if ("up".equals(myKey)) { moveUp(); } // shows use of .equals(String) method ADDED: I see your initial post now and know what you intented. Will post again.
danpost danpost

2011/10/18

#
You will probably need to add two or three instance variables to the Wombat class. The first one to indicating whether it is gliding or not (private boolean isGliding = false;). The second one to count the glide steps (private int glideStep = 0;). You should probably have one more to show which direction it is gliding (private int glideDirection = 0;). This last one can use the range 0 through 3, for each of the ordinate directions (East, South, West, and North, not necessarily in that order). When a directional keystroke is found, set isGliding to true, glideDirection to the appropriate value, reset glideStep to zero, and setRotation to direction (nothing more is needed within checkKeyPressed()). In glide() , check for 'isGliding', and if so, add the offset to the coordinate (getX() or getY()) that will change, and check it for leaving the world. If so, reset isGliding to false. If OK to move, move and increment glideStep, and check glideStep to equal 10. If so, reset isGliding to false.
// public void act()
glide();
checkKeyPressed();
// public void checkKeyPressed()
String myKey = Greenfoot.getKey();
if (myKey != null)
{
    if ("up".equals(myKey))
    {
        isGliding = true;
        glideStep = 0;
        glideDirection = 3;
        setRotation(270);
    }
    if ("right".equals(myKey)
    {
        isGliding = true;
        glideStep = 0;
        glideDirection = 0;
        setRotation(0);
    }
    if ("down".equals(myKey))
    {
        isGliding = true;
        glideStep = 0;
        glideDirection = 1;
        setRotation(90);
    }
    if ("left".equals(myKey))
    {
        isGliding = true
        glideStep = 0;
        glideDirection = 2;
        setRotation(0);
    }
}
// public void glide()
if (isGliding)
{
    if (glideDirection == 0)
    {
        if (getX() + 1 < getWorld().getWidth()) { setLocation(getX() + 1, getY()); }
        else { isGliding = false; }
    }
    if (glideDirection == 1)
    {
        if (getY() + 1 < getWorld().getHeight()) { setLocation(getX(), getY() + 1); }
        else { isGliding = false; }
    }
    if (glideDirection == 2)
    {
        if (getX() > 0) { setLocation(getX() - 1, getY()); }
        else { isGliding = false; }
    }
    if (glideDirection == 3)
    {
        if (getY() > 0) { setLocation(getX(), getY() - 1); }
        else { isGliding = false; }
    }
    if (isGliding)
    {
        glideStep++;
        if (glideStep == 10) { isGliding = false; }
    }
}
If not run at too fast a speed, you should be able to over-ride one glide with another.
ardwennem2 ardwennem2

2011/10/18

#
Thank you very much for this, I have just one more question: how should I define isGliding, because I either get the error: - Cannot find symbol - variable isGliding or I get (if I define it as Boolean): - Variable isGliding might not have been initalized
davmac davmac

2011/10/18

#
You need to define isGliding as an instance variable - outside of any methods (but inside the class).
public class Wombat extends Actor
{
     private boolean isGliding = false;
     ... (etc)
ardwennem2 ardwennem2

2011/10/18

#
O... so that is how I can use private ... xD now I understand, thanx
You need to login to post a reply.