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

2012/9/19

Playing with movement and speed

Mikeson Mikeson

2012/9/19

#
Using the up and down buttons to increase & decrease the speed (movement) so far I have only been able to get it to move one place on key down. Bellow is what I have tried so far. private int speed; public void act() { // move(speed); move(); } public void speed() { if(Greenfoot.isKeyDown("up")) { // speed++; /**Compiles Does not move**/ // move(+1); /** Compiles Does not move**/ } if(Greenfoot.isKeyDown("down")) { // speed--; /** Compiles Does not move**/ // move(-1); /** Compiles Does not move**/ } } public void move() { if(Greenfoot.isKeyDown("up")) { move(+1); /** Compiles Moves no increase only moves one spot**/ } if(Greenfoot.isKeyDown("down")) { move(-1); /** Compiles Moves no decrease only moves one spot**/ } }
danpost danpost

2012/9/19

#
In the 'move()' method, the checks for 'up' and 'down' keys should change the value of speed, accordingly. Remove the calls to 'move(int)' out of the 'if' brackets and instead of '-1' or '+1', just use 'speed' in a call to 'move(int)' after the 'if' brackets.
Mikeson Mikeson

2012/9/19

#
@danpost tried what you said however I dont't fully understand what you mean could you perhaps write an example or. Do you know any senarios with source code that use what im trying to achive I can have a look at.
danpost danpost

2012/9/19

#
Put simply, you said "Using the up and down buttons to increase & decrease the speed".
// instance variables whose values can be adjusted
int speed = 0;
final int MAX_SPEED = 5;
final int DELAY = 20; // must be greater than zero
// within act or a method it calls
if (Greenfoot.isKeyDown("up") && speed < MAX_SPEED * DELAY) speed++;
if (Greenfoot.isKeyDown("down") && speed > -MAX_SPEED * DELAY) speed--;
and you want it to move with respect to the value of 'speed'
move(speed / DELAY);
The reason for the delay is so it does not jump to maximum speed so quickly you do not notice the increase/decrease in speed. This gives the user a chance to stop the change in speed at whatever speed they are comfortable with at the moment. You can think of the DELAY field as being the inverse of accelleration, or DRAG (the lower the value, the faster the change in speed).
lelle lelle

2012/9/19

#
I have a simular problem. I can not get my spider to go byitself, it moves only when I hold the arrow key down, but stops after I release the button, what am I missing?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Spider here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spider extends Actor
{
    private int Speed = 4;
    private int rotate = 45;
    /**
     * Act - do whatever the Spider wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        control();
     
    }
    public void control()
    {
        if(Greenfoot.isKeyDown("right"))  
        {
           setLocation(getX()+Speed, getY());  
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-Speed, getY());
        }
       if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-Speed);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+Speed); 
        }
        if(Greenfoot.isKeyDown("space"))
        {
            setRotation(getRotation()+rotate);
        }
    }
   
    }
   
   
danpost danpost

2012/9/19

#
@lelle, Normally, if the actor is to move all the time, the 'left' and 'right' arrow keys control the rotation of the object and move(int) will move it. There are always other ways to accomplish the same thing, and each way may or may not act exactly the same as another. Looking at your code, as you have it right now, in summary, says to relocate or rotate if a key is down; which is exactly the action you are getting right now.
You need to login to post a reply.