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

2018/5/24

Game freezes when holding down keys

Coop799 Coop799

2018/5/24

#
Whenever i hold down the keys or spam for a little bit everything stops working and I have to restart the app. Heres the code for the player
public class player1 extends Actor
{
    GifImage gifRight = new GifImage("run1.gif");
    GifImage gifLeft = new GifImage("run2.gif");
    private int shootCounter=0, reloadDelay=50;
    private static int GRAVITY = 1;
    private int vSpeed;
    private int speed = 3;
    private int jumpCounter=0, jumpDelay=30;
    public void act() 
    {
       checkKeys();
       rightshoot();
       leftshoot();
       vSpeed += GRAVITY;
       setLocation(getX(), getY() + vSpeed);
       ground();
       jump();
    }    
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("d"))
       {
         setImage(gifRight.getCurrentImage());
         setLocation(getX() + speed, getY());
        }
        else{
         if (Greenfoot.isKeyDown("a"))
         {
          setImage(gifLeft.getCurrentImage());
          setLocation(getX() - speed, getY());
         }
         }
    }
    public void rightshoot()
    {
       shootCounter++;
        if(Greenfoot.isKeyDown("c"))
       {
            if(shootCounter>reloadDelay)
            {
                shootCounter=0;
                getWorld().addObject(new bullet(), getX()+20, getY());
                Greenfoot.playSound("shoot.wav");
            }
        }
       }
       public void leftshoot()
    {
       shootCounter++;
        if(Greenfoot.isKeyDown("x"))
       {
            if(shootCounter>reloadDelay)
            {
                shootCounter=0;
                getWorld().addObject(new bullet1(), getX(), getY());
                Greenfoot.playSound("shoot.wav");
            }
        }
       }
       public void ground()
    {
        if(isTouching(platform1.class))
        {
            GRAVITY = 0;
            vSpeed = 0;
        }
        else{
            GRAVITY = 1;
            vSpeed += GRAVITY;
     }
    }
       public void jump()
    {
        jumpCounter++;
        if (Greenfoot.isKeyDown("w"))
         {
            if(jumpCounter>jumpDelay)
            {
                jumpCounter=0;
                GRAVITY = -1;
                vSpeed = -15;
                Greenfoot.playSound("jump.wav");
            }
         }
     }
    } 
        
  

danpost danpost

2018/5/25

#
Coop799 wrote...
Whenever i hold down the keys or spam for a little bit everything stops working and I have to restart the app. Heres the code for the player << Code Omitted >>
I do not see anything given in this class that might cause freezing of the scenario. Maybe try another class (starting with your World subclass, maybe).
Coop799 Coop799

2018/5/25

#
public class MyWorld extends World
{
    public MyWorld()
    {    
        super(600, 337, 1);
        prepare();
    }
    private void prepare()
    {
        platform1 platform1 = new platform1();
        addObject(platform1,300,164);
        player1 player1 = new player1();
        addObject(player1,169,144);
    }
}
This is the code for the world
danpost danpost

2018/5/25

#
Coop799 wrote...
<< Code Omitted >> This is the code for the world
Obviously nothing there. Although, it is never a good idea to name an actor the same as its class. Class names, by convention, should begin with uppercase letters and variable names with lowercase ones.
Agent40 Agent40

2018/5/29

#
It could have something to do with your computer's RAM being overloaded by greenfoot due to having low specs. That's only if you have a bad computer.
Yehuda Yehuda

2018/5/29

#
Do you get a "java.lang.OutOfMemoryError: Java heap space" error (from using GIF images for your animation)?
Coop799 Coop799

2018/5/29

#
I dont get that error
Yehuda Yehuda

2018/5/31

#
Does the program work any differently if you comment out the parts that have to do with the GifImage?
You need to login to post a reply.