• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
someguy127

Event Handling problems

2 posts in this topic

Hi, I am working on one of my first games in java and I was having a big problem with the Event Handler where it would only work somewhere in the realm of 1/10 times. If anyone has any idea on how to solve this problem I'd be much obliged.

This is my game panel constructor, I have global variables called isRight, isDown etc... and in the main gameloop I check if they are true
in which case my character moves so many pixels
[CODE]
public GamePanel(){
setFocusable(true);
requestFocus();
setPreferredSize(new Dimension(GWIDTH,GHEIGHT));
makeGrid();
setBackground(Color.white);
addKeyListener(new KeyListener(){

public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
isRight = true;
System.out.println("Rightpressed");
break;
case KeyEvent.VK_LEFT:
isLeft = true;
break;
case KeyEvent.VK_UP:
isUp = true;
break;
case KeyEvent.VK_DOWN:
isDown = true;
default:
break;
}

}

public void keyReleased(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
isRight = false;
System.out.println("Rightreleased");
break;
case KeyEvent.VK_LEFT:
isLeft = false;
break;
case KeyEvent.VK_UP:
isUp = false;
break;
case KeyEvent.VK_DOWN:
isDown = false;
default:
break;
}

}

public void keyTyped(KeyEvent e) {


}

});
}
[/CODE]
This here is the update methods and run() game loop, I don't think that the problem is in here because I put in a print statement in each of these methods and they ran consistently.
[CODE]
public void gameUpdate()
{
//update game logic

if(isRight){
player.x += player.speed;
}
if(isLeft){
player.x -=player.speed;
}

}
public void run()
{
while(running)
{

gameUpdate();
gameRender();
paintScreen();
try{
game.sleep(20);
}
catch(Exception e){}
}
}
[/CODE]

And Just incase it is not a problem with my constructor, here is my complete code if anyone is willing to look through it

[CODE]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GamePanel extends JPanel implements Runnable{

/**
*
*/
private static final long serialVersionUID = 1L;
//double buffering
Image dbImage;
Graphics dbg;

Rectangle[] grid = new Rectangle[250];

//JPanel Vars
public static final int GWIDTH = 500, GHEIGHT = 500;
static final Dimension gameDim = new Dimension(GWIDTH,GHEIGHT);

public boolean isRight = false,isLeft = false, isUp = false,isDown=false;

private Thread game;
private volatile boolean running = false;

Player player = new Player();
public GamePanel(){
setFocusable(true);
requestFocus();
setPreferredSize(new Dimension(GWIDTH,GHEIGHT));
makeGrid();
setBackground(Color.white);
addKeyListener(new KeyListener(){

public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
isRight = true;
System.out.println("Rightpressed");
break;
case KeyEvent.VK_LEFT:
isLeft = true;
break;
case KeyEvent.VK_UP:
isUp = true;
break;
case KeyEvent.VK_DOWN:
isDown = true;
default:
break;
}

}

public void keyReleased(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
isRight = false;
System.out.println("Rightreleased");
break;
case KeyEvent.VK_LEFT:
isLeft = false;
break;
case KeyEvent.VK_UP:
isUp = false;
break;
case KeyEvent.VK_DOWN:
isDown = false;
default:
break;
}

}

public void keyTyped(KeyEvent e) {


}

});
addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

});

}
private void makeGrid(){
int x=0,y=0;
for(int i = 0; i <grid.length;i++)
{
if(i % 16 != 0 | i ==0){
grid[i] = new Rectangle(x,y,32,32);
x+=32;
}
else{
y+=32;
x = 0;
grid[i] = new Rectangle(x,y,32,32);
x+=32;
}
}
}
private void startGame()
{
if(game == null || !running){
running = true;
game = new Thread(this);
game.start();

}
}
public void stopGame()
{
if(game != null || running)
{
running = false;
}
}
public void addNotify()
{
super.addNotify();
startGame();
}
//Game Rendering
public void gameUpdate()
{
//update game logic

if(isRight){
player.x += player.speed;
}
if(isLeft){
player.x -=player.speed;
}

}
private void gameRender()
{
if(dbImage == null)
{
dbImage = createImage(GWIDTH,GHEIGHT);
if(dbImage == null)
{
System.err.println("Error in Game Render");
return;
}
else
{
dbg = dbImage.getGraphics();
}
}
dbg.setColor(Color.WHITE);
dbg.fillRect(0, 0, getWidth(),getHeight());
//drawGAME
draw(dbg);
}
public void draw(Graphics g)
{
//draw things here
g.setColor(Color.black);
//player.draw(g);
g.fillRect(player.x, player.y, player.width, player.height);

g.setColor(Color.RED);
for(int i = 0; i < grid.length; i++){
g.drawRect(grid[i].x, grid[i].y, grid[i].width, grid[i].height);
}
}

public void paintScreen()
{
Graphics g;
try{
g = this.getGraphics();
if(dbImage != null && g!= null){
g.drawImage(dbImage,0,0, null);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
catch(Exception e)
{
System.err.println("Error in paintScreen");
}

}
//logic
public void run()
{
while(running)
{

gameUpdate();
gameRender();
paintScreen();
try{
game.sleep(20);
}
catch(Exception e){}
}
}
}
[/CODE] Edited by someguy127
0

Share this post


Link to post
Share on other sites
Are you sure the panel has focus? you request focus in the constructor, but I doubt it will have any effect, since the panel is not added to any frame/applet before the entire constructor is executed any ways. Try to give it focus from the frame after you have added the panel
0

Share this post


Link to post
Share on other sites
What exactly doesn't work ? Already set some break points and debugged ?
In any case, your direction variables should be volatile or synchronized.
0

Share this post


Link to post
Share on other sites

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  
Followers 0