Sign in to follow this  

[Java] Applet game freezing

This topic is 3670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am taking a Java class in school and have just started trying to make some games to reinforce what I am learning. Applets seemed like the easiest way to get started with Java games so I started using them. I managed to make a functional pong game but I have been running into trouble when I try to use different classes with the applet. Right now I am trying to make a shooter but I have run into trouble with creating bullets. I can get the applet running fine when it is just the player ship moving around, but whenever I try to instantiate or create a shot object the applet will just freeze and do nothing. I would try to post a smaller section of code but I really don't know where the problem is coming from. Any help would be appreciated.
public class Entity
{
    private int myX;
    private int myY;
    
    public Entity(int x, int y)
    {
        myX = x;
        myY = y;
    }
    
    public int getX()
    {
        return myX;
    }
    
    public int getY()
    {
        return myY;
    }
    
    public void move(int changeX, int changeY)
    {
        myX += changeX;
        myY += changeY;
    }
}
public class Shot extends Entity
{
    private static int speed = -10;
    
    public Shot( int x, int y)
    {
        super(x,y);
    }

    public void update()
    {
        move(0, speed);
    }
}
import java.applet.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.Image;
import java.awt.Color;
import java.util.ArrayList;

public class Game extends Applet implements Runnable, KeyListener
{
    Thread t;  
    Image offscreenImage;
    Graphics offscr;
    boolean moveLeft; //true when player is moving left
    boolean moveRight; //true when player is moving right
    Player player; //player object
    ArrayList <Shot> shotList; //list of all shots fired
    Image shotImage; //bullet sprite
    
    public void init()
    {
        int width = getSize().width;   
        int height = getSize().height;   
        offscreenImage = createImage(width, height);
        offscr = offscreenImage.getGraphics();
        t = new Thread(this);
        t.start();
        addKeyListener(this);
        moveLeft = moveRight = false;  //player starts out not moving
        setBackground(Color.white);
        setForeground(Color.black);
        player = new Player(width/2, height - 50, getImage(getCodeBase(), "ship.gif"), this);
        shotImage = getImage(getCodeBase(), "shot.gif"); //assings shot sprite
        shotList = new ArrayList <Shot> ();
    }
    
    public void run()
    {
        while(true)
        {
            repaint();
            updatePos();
            
            try{
                t.sleep(1000/30);
            } catch(InterruptedException e) { ; }
        }
    }
    
    public void paint(Graphics g)
    {
        offscr.drawImage(player.getImage(), player.getX(), player.getY(), this); //draw player
        drawShots(offscr);
    }
    
    public void update(Graphics g)
    {
        offscr.setColor(getBackground());
        offscr.fillRect (0, 0, this.getSize().width, this.getSize().height); 
        
        offscr.setColor(getForeground());
        paint(offscr);
        
        g.drawImage(offscreenImage, 0, 0, this);
    }
    
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 37 && moveLeft == false){
            moveLeft = true;
            moveRight = false;}
        if (e.getKeyCode() == 39 && moveRight == false){
            moveRight = true;
            moveLeft = false;}
        if (e.getKeyCode() == 38){
            int x = player.getX() + 11;
            int y = player.getY();
            Shot shot = new Shot(x, y);
            shotList.add(shot);
        }
    }

    public void keyReleased(KeyEvent e) { 
        if (e.getKeyCode() == 37 && moveLeft == true){
            moveLeft = false;}
        if (e.getKeyCode() == 39 && moveRight == true){
            moveRight = false;}
    }
    
    public void keyTyped(KeyEvent e) {
        }
    
    public void updatePos()
    {
        if (moveLeft && (player.getX() > 0)){  //make sure that the player doesn't 
            player.moveLeft();}               //go off the screen to the left
        if (moveRight && (player.getX() + player.getWidth() < getSize().width)){ 
            player.moveRight();}      //same for the right
        updateShots();
    }
    
    public void updateShots()  //update the bullet positions
    {
        for (Shot i: shotList)
        {
            i.update();
        }
    }
    
    public void drawShots(Graphics g)  //draw the shots
    {
        for (Shot i: shotList)
        {
            g.drawImage(shotImage, i.getX(), i.getY(), this);
        }
    }
}

Share this post


Link to post
Share on other sites
Okay, I'll make my question more general. What could cause a Java applet to freeze? Or how can I run the applet in a way that I can get error messages or figure out what is going wrong?

Share this post


Link to post
Share on other sites
Quote:
Original post by Frostyeel
Or how can I run the applet in a way that I can get error messages or figure out what is going wrong?


The jdk comes with a program called appletviewer that you can use as an alternative to a browser when running applets. According to this webpage, you can specify -debug when running it from the command line to use the java debugger. That should help you figure out what's happening.

Share this post


Link to post
Share on other sites

This topic is 3670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this