Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8653559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:34:25+00:00 2026-06-12T14:34:25+00:00

I am creating a pacman game in Java and I have 1 problem I

  • 0

I am creating a pacman game in Java and I have 1 problem I can’t get rid of.. The problem is as follows:

I have 4 buttons in the game screen for each way: up, down, left, right. The problem is that I can not use the buttons in the same time in x position as in the y position because I always get a value of 0 ( you understand this if you see my code below )

Below you can find my class for the pacman that i think is related to the problem ( see setBesturing(int besturing) method )

package h04PacMan;

/*
 * PacMan laten bewegen
 */

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class PacMan extends JPanel implements ActionListener {

private int horizontalePlaats = 250; // x location
private int verticalePlaats = 150; // y location
private int richtingEnSnelheid = +10; // speed
private final int WACHTTIJD = 500; // DELAY
int diameter;
int waarde;

public PacMan() {

    // create timer and start timer
            javax.swing.Timer autoKlik = new javax.swing.Timer(WACHTTIJD, this);

            autoKlik.start();
}

public int getHorPlaats() {
    return horizontalePlaats;
}

// current y positie

public int getVerPlaats() {
    return verticalePlaats;
}

// speed and way;
public int getRichtingEnSnelheid() {
    return richtingEnSnelheid;
}

// new x position

public void setHorPlaats(int nieuweHorPlaats) {

    if(nieuweHorPlaats > getWidth()) {

        nieuweHorPlaats = 0;
    }
    else if(nieuweHorPlaats < 0) {

        nieuweHorPlaats = getWidth();
    }

    horizontalePlaats = nieuweHorPlaats;

}

// new y position 
public void setVerPlaats(int nieuweVerPlaats) {

    if(nieuweVerPlaats > getHeight()) {

        nieuweVerPlaats = 0;
    }
    else if(nieuweVerPlaats < 0) {

        nieuweVerPlaats = getHeight();
    }

    verticalePlaats = nieuweVerPlaats;

}

public void setRichtingEnSnelheid(int nieuweRichtingEnSnelheid) {

    richtingEnSnelheid = nieuweRichtingEnSnelheid;

}

//movement
public void setBesturing(int besturing) {

    besturing = waarde;

    if(waarde == 0) {
        setVerPlaats( getVerPlaats() + richtingEnSnelheid);
    }
    else if(waarde == 1){
        setHorPlaats( getHorPlaats() + richtingEnSnelheid);
    }
}

@Override
public void actionPerformed(ActionEvent e) {

    setBesturing(waarde);
    System.out.println(waarde);
    repaint();

}

DrawPacMan pacman = new DrawPacMan();
DrawPacMan ghost1 = new DrawPacMan();
DrawPacMan ghost2 = new DrawPacMan();

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    // pacman movement
    diameter = 75;  
    pacman.drawPacMan(g, getHorPlaats(), getVerPlaats(), diameter, Color.yellow);

    // ghosts movement
    int g1x;
    for(g1x = 0; g1x < 10; g1x++) {

        pacman.drawGhost(g, g1x, 40, diameter, Color.red);

    }
    pacman.drawGhost(g, 170, 70, diameter, Color.blue);


}

}

and this is the actionListener in my gamecontrol class

    @Override
public void actionPerformed(ActionEvent e) {

    if(e.getSource() == links) {

        pacman.setRichtingEnSnelheid( -10 );
        pacman.setBesturing(1);
        System.out.println("links");
    }
    else if(e.getSource() == rechts) {
        pacman.setRichtingEnSnelheid( +10 );
        pacman.setBesturing(1);
        System.out.println("rechts");
    }
    else if(e.getSource() == boven) {
        pacman.setRichtingEnSnelheid( -10);
        pacman.setBesturing(0);
        System.out.println("boven");
    }
    else {
        pacman.setRichtingEnSnelheid( +10);
        pacman.setBesturing(0);
        System.out.println("beneden");
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:34:27+00:00Added an answer on June 12, 2026 at 2:34 pm

    Here is your problem:

    //movement
    public void setBesturing(int besturing) {
    
        besturing = waarde; <-- THIS LINE
    
        if(waarde == 0) {
            setVerPlaats( getVerPlaats() + richtingEnSnelheid);
        }
        else if(waarde == 1){
            setHorPlaats( getHorPlaats() + richtingEnSnelheid);
        }
    }
    

    You are overwriting the value of besturing with the old value waarde. It should be the other way around.

    //movement
    public void setBesturing(int besturing) {
    
        waarde = besturing; <-- THIS LINE
    
        if(waarde == 0) {
            setVerPlaats( getVerPlaats() + richtingEnSnelheid);
        }
        else if(waarde == 1){
            setHorPlaats( getHorPlaats() + richtingEnSnelheid);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating a simple RPG game, first time using XNA. Trying to get my character
I'm creating my first game. It will be Pacman or Snake. I'm going to
I am creating a pacman game in javascript to learn the language, and the
I'm creating a game, pacman specifically as part of my coursework and I'm having
So I'm building the pacman game in Java to teach myself game programming. I
Creating column at runtime, with DataTemplateSelector. Have exception at XamlReader.Load: System.Windows.StaticResourceExtension. How can i
Creating a pacman-like game, where a player eats objects. When a MovieClip (box) eats
I am creating a pacman-style game. I am trying to remove an instance of
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
Creating a JApplet I have 2 Text Fields, a button and a Text Area.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.