3 Replies - 1859 Views - Last Post: 21 May 2011 - 05:41 AM Rate Topic: -----

#1 PKth22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-October 10

Unable to "smooth out" grid movement in game

Posted 19 May 2011 - 10:40 AM

So, in short, me and a partner are working on a game for our Computer Science final project, and we can't get the basic overworld engine to work like we need it to. The game we're making is a Pokemon clone, so the overworld screen is supposed to be a grid... plus it's a requirement to use a grid in our game >.< . Neither of us really know what to do here. The only solution I could think of would be to kinda "lock" the character into finishing a loop of an animation, but that didn't work out too well. Any tips at all would be great!

And the source code:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class jFrameTest extends JFrame implements KeyListener
{
	//Graphics g;
	int appWidth, appHeight,x,y,SQUARE_SIZE;
	int currentKey,tileSlideCount;
	boolean animationFlag;	//is the animation finished?
	
	public jFrameTest()
	{
		super("This is a test for JFrames");
		animationFlag = true;
		setSize(500,400);
		addKeyListener(this);
		setFocusable(true);
		setVisible(true);
		requestFocusInWindow();
		x=0;
		y=0;
		SQUARE_SIZE=20;
	}
	
	public void keyPressed(KeyEvent event)
	{
		currentKey = event.getKeyCode();
		System.out.println(currentKey);
		System.out.println(x +","+ y);
		
		if (currentKey == KeyEvent.VK_LEFT) 
		{
			if(animationFlag)
			{
				animationFlag = false;
				for(tileSlideCount=0;tileSlideCount<20;tileSlideCount++)
				{
					x -= 1;
					repaint();
					delay(5);
				}
				
				tileSlideCount=0;
				if (x < 3)
					x = 3;
				repaint();
				
				animationFlag = true;	
			}
		}
		else
		if (currentKey == KeyEvent.VK_RIGHT) 
		{
			for(tileSlideCount=0;tileSlideCount<20;tileSlideCount++)
				x += 1;
				
			tileSlideCount=0;
			if (x > getSize().width - 3 - SQUARE_SIZE)
				x = getSize().width - 3 - SQUARE_SIZE;
			repaint();
		}
		else 
		if (currentKey == KeyEvent.VK_UP) 
		{
			for(tileSlideCount=0;tileSlideCount<20;tileSlideCount++)
				y -= 1;
				
			tileSlideCount=0;
			if (y < 3)
				y = 3;
			repaint();
      	}
      	else
      	if (currentKey == KeyEvent.VK_DOWN) 
      	{
			for(tileSlideCount=0;tileSlideCount<20;tileSlideCount++)
				y += 1;
				
			tileSlideCount=0;
         	if (y > getSize().height - 3 - SQUARE_SIZE)
            	y = getSize().height - 3 - SQUARE_SIZE;
         	repaint();
      	}
      	System.out.println(currentKey);
	}
	
	public void keyReleased(KeyEvent event)
	{
		//currentKey=event.getKeyCode();
	}
	
	public void keyTyped(KeyEvent event)
	{
		//currentKey=event.getKeyCode();
	}
	
	public void paint(Graphics g)
	{
		g.setColor(Color.white);
		g.fillRect(0,0,510,400);
		g.setColor(Color.black);
		g.fillRect(x,y+20,SQUARE_SIZE,SQUARE_SIZE);
	}
	
	public void delay(int n)
	{
		long startDelay=System.currentTimeMillis();
		long endDelay=0;
		while(endDelay-startDelay<n)
			endDelay=System.currentTimeMillis();
	}	
	
	public void update(Graphics g)
	{
		System.out.println(currentKey);
		paint(g);
	}
	
	public static void main(String args[])
	{		
		jFrameTest application = new jFrameTest();
		
		application.repaint();
		
		application.setDefaultCloseOperation(
			JFrame.EXIT_ON_CLOSE );
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Unable to "smooth out" grid movement in game

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1576
  • View blog
  • Posts: 3,548
  • Joined: 05-April 11

Re: Unable to "smooth out" grid movement in game

Posted 19 May 2011 - 11:10 AM

You really should be making a Board class which controls the grid, and then you should have a Player class who has at least an X and Y position.

Also I think you should be painting in a JPanel by overriding its paintComponent method.If you want it to be smooth then you need to use Threads and have a consistent repaint of your JPanel

Actually I would suggest both of you read pbl's guide about canvas, it will help you a lot.

This post has been edited by CasiOo: 19 May 2011 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 PKth22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-October 10

Re: Unable to "smooth out" grid movement in game

Posted 20 May 2011 - 02:46 PM

View PostCasiOo, on 19 May 2011 - 11:10 AM, said:

You really should be making a Board class which controls the grid, and then you should have a Player class who has at least an X and Y position.

Also I think you should be painting in a JPanel by overriding its paintComponent method.If you want it to be smooth then you need to use Threads and have a consistent repaint of your JPanel

Actually I would suggest both of you read pbl's guide about canvas, it will help you a lot.


Um... To clarify, I don't mean to reduce flickering, I meant to smooth the transition from one cell space to another; I don't want the character/box to simply jump from cell to cell to cell.

As for threading and paintComponents, I've looked those up and they do look like they'd help out a lot, but even the "easy" tutorials are for intermediate Java users, rather than beginners (I've been using java for only 7 months now), so any tips to clarify just... things... in general, on those topics (Sorry for my complete lack of clarity)?

And for the Canvas tutorial... I couldn't find it -_-, I searched the Java help and tutorial subforums and found nothing... and I even went to PBL's profile and found nothing which surprised me- was the tutorial deleted or am I really just that oblivious/dumb? If I'm not, a link would be nice, or if it's posted on another site instead of this one, that'd be nice too.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Unable to "smooth out" grid movement in game

Posted 21 May 2011 - 05:41 AM

calling repaint() post a message to the drawing thread saying "please call paint()" the next time the drawing thread will run. Which basically means that the behaviour of

repaint();
Thread.sleep();

is a absolutly not predicable. This not the way should be done.

You need to use a java.swing.Timer(). In the actionPerformed() of this class, you update your calculations (drawing objects positionning) and the call repaint()
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1