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

2018/8/30

Need Help

CosmicCaleb CosmicCaleb

2018/8/30

#
I was making a simple Greenfoot game, and I have a spider that moves around with the WASD keys. It works for a small amount of time, and then the keystrokes do not do anything. I press the keys, but the spider stops moving. So i'm pretty sure it isn't a code problem. Does anyone know what's up?
public void act() 
    {
         keyMove();  
         eat();
    }    
    
     public void keyMove()
   {
       if (Greenfoot.isKeyDown("w"))
    {   
        setLocation(getX(), getY()-5);
    }
    
     if (Greenfoot.isKeyDown("s"))
    {   
        setLocation(getX(), getY() +5);
    }
    
     if (Greenfoot.isKeyDown("a"))
    {   
        setLocation(getX() -5, getY());
    }
    
     if (Greenfoot.isKeyDown("d"))
    {   
        setLocation(getX() +5, getY());
    }
    
     if (Greenfoot.isKeyDown("right"))
    {   
        turn(10);
    }
    
     if (Greenfoot.isKeyDown("left"))
    {   
        turn(-10);
    }
}

public void eat()
{
    if (isTouching(Lobster.class))
    {
        removeTouching(Lobster.class);
    }
}
}
danpost danpost

2018/8/30

#
CosmicCaleb wrote...
i'm pretty sure it isn't a code problem.
It does not appear to be (at least with what was given). What other codes do you currently have?
CosmicCaleb CosmicCaleb

2018/8/30

#
Here is the other code, but the code above is for the actor that was having the problem.
public class Lobster extends Actor
{
    /**
     * Act - do whatever the Lobster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(10);  
        if (Greenfoot.getRandomNumber(50) < 5) 
        {
            turn(Greenfoot.getRandomNumber(50) +25);
        }
        if (isAtEdge())
        {
            turn(Greenfoot.getRandomNumber(50) -20);
        }
    }
    public Lobster()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 250, image.getHeight() - 299);
        setImage(image);
    }
}
public class Crab extends Actor
{
    public void act()
    {
        move(10);
        if (Greenfoot.getRandomNumber(100) < 80)
        {
            turn(Greenfoot.getRandomNumber(25) - 10);
        }
        if (isAtEdge())
        {
            turn(Greenfoot.getRandomNumber(100) - 10);
        } 
   }
   }
danpost danpost

2018/8/30

#
CosmicCaleb wrote...
Here is the other code, but the code above is for the actor that was having the problem. << Code Omitted >>
And your world class code?
CosmicCaleb CosmicCaleb

2018/9/4

#
}
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)

public class CrabWorld extends World
{
    /**
     * Create the crab world (the beach). Our world has a size 
     * of 560x560 cells, where every cell is just 1 pixel.
     */
    public CrabWorld() 
    {
        super(1000, 600, 1);
        prepare();
        GreenfootImage bg = new GreenfootImage("Water.png"); 
        bg.scale(getWidth(), getHeight());
        setBackground(bg);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        Crab crab = new Crab();
        addObject(crab,470,311);
        removeObject(crab);
        Crab crab2 = new Crab();
        addObject(crab2,791,246);
        Lobster lobster = new Lobster();
        addObject(lobster,535,301);
        Lobster lobster2 = new Lobster();
        addObject(lobster2,688,400);
        Lobster lobster3 = new Lobster();
        addObject(lobster3,414,375);
        Lobster lobster4 = new Lobster();
        addObject(lobster4,331,181);
        Baby baby = new Baby();
        addObject(baby,246,331);
        Baby baby2 = new Baby();
        addObject(baby2,298,442);
        Baby baby3 = new Baby();
        addObject(baby3,549,162);
        Baby baby4 = new Baby();
        addObject(baby4,781,156);
        Crab crab3 = new Crab();
        addObject(crab3,860,317);
        Crab crab4 = new Crab();
        addObject(crab4,148,457);
        Crab crab5 = new Crab();
        addObject(crab5,150,188);
        removeObject(crab4);
        removeObject(crab2);
        removeObject(crab3);
        Crab crab6 = new Crab();
        addObject(crab6,619,142);
        Crab crab7 = new Crab();
        addObject(crab7,889,298);
        Crab crab8 = new Crab();
        addObject(crab8,444,91);
        removeObject(baby2);
        removeObject(baby3);
        removeObject(baby4);

    }
}
danpost danpost

2018/9/4

#
I presume the spider code given is part of the class called Baby. I doubt very much that any other code in that class could have anything to do with your issue. What version of greenfoot are you using, and what type system do you have it on?
CosmicCaleb CosmicCaleb

2018/9/4

#
Greenfoot 3.0.4
danpost danpost

2018/9/4

#
Well, its not any of your given code (which was tested, by the way) and I did not see any related bug logs. You could try reinstalling greenfoot -- or try a different version (3.1.0 perhaps).
davmac davmac

2018/9/5

#
It works for a small amount of time, and then the keystrokes do not do anything. I press the keys, but the spider stops moving
Let me guess: you're using a Mac? It sounds like the problem described here. Try the solution there, or try using Greenfoot 3.5.0.
You need to login to post a reply.