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

2014/5/14

Why is my character flying across the screen???

GP2 GP2

2014/5/14

#
Hi, I'm new to greenfoot and I'm trying to make a game where my character moves across the screen using the arrow keys to maneuver it. I used the setLocation method instead of move because I did not want to have to deal with direction and when I pressed the arrow keys, my character just wen to the corner instead of moving one coordinate. Here's the code for the character class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Character extends Actor
{
    private String name;
    public static int walkingSpeed = 1;
    public static int numberOfCombosPossible;
    public static int range;
    public static int lives = 3;
    public static String specialAbility;
   
    public Character()
    {
        super();
    }
    
    public void act() 
    {
        if(Greenfoot.isKeyDown("left"))
        {
          moveLeft();   
        }
        if(Greenfoot.isKeyDown("up"))
        {
            moveUp();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        if(Greenfoot.isKeyDown("down"))
        {
            moveDown();
        }
        if(Greenfoot.isKeyDown("q"))
        {
            comboAttack();
        }
        if(Greenfoot.isKeyDown("w"))
        {
            //bigAttack();
        }
        if(Greenfoot.isKeyDown("space"))
        {
            //pickUp();
        }
    }
    
    public String getName()
    {
        return name;
    }
    
    public String getSpecialAbility()
    {
        return specialAbility;
    }
    
    public void moveLeft()
    {
        setLocation(getX() - walkingSpeed, getY()); 
    }
    
    public void moveUp()
    {
        setLocation(getX(), getY()- walkingSpeed);
    }
    
    public void moveRight()
    {
        setLocation(getX() + walkingSpeed, getY()); 
    }
    
    public void moveDown()
    {
        setLocation(getX(), getY()+ walkingSpeed); 
    }
    
    public void comboAttack()
    {

    }
    
}
I'm still in the beginning stages of the game but the methods I'm having issue with are the moveUp, moveDown, MoveLeft, and moveRight methods. My guess is that the the third number in the world constructor which according to the comments makes the world a 1X1 pixel is the problem but if I set it to a number higher than 1 it creates an error. Someone please help!
danpost danpost

2014/5/14

#
My guess is that the problem is with the speed at which the scenario is set at. Maybe the speed slider is not around the middle of the slidebar or you are using 'Greenfoot.setSpeed' with a number closer to 100 than 50. You might want to rename the class to something other than 'Character' (since Character is already defined in the Java API). I do not know for sure if this could cause complications; but, changing the name would avoid that possibility.
GP2 GP2

2014/5/14

#
Thank you so much! Your advice worked and I changed my class name to player to avoid complications . Again thank you
You need to login to post a reply.