Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 11 Mar 2013
Offline Last Active Jan 11 2016 08:17 AM

Posts I've Made

In Topic: Quickest way to iterate through a rectangles lines?

23 May 2013 - 09:18 AM

I'm sure that whatever language you are using (your code snippet can be several) there is a Rectangle implementation in the core API which has an intersects( anotherRectangle ) method?

In Topic: Understanding 2D Walking/Run Animation in Games

06 May 2013 - 02:05 AM

I would start by separating animation logic from the draw method into an update method. Secondly I would the make the animation logic framerate independant. As it is now it's written in a frame dependant way which causes the animation to look different depending on the speed of the machine.


I'll try to do some pseudo-code below to show how I do animation logic myself below.


// Animation frames ( can be used for longer intervalls of certain frames as shown using the frame 4 more than the others ).
private int[] animationSequence = { 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 3, 3, 2, 2 };

// Animation framerate.
private float animationFramerate = animationSequence.length / 1000F;
private int currentFrame = 0;
public void update( long ms ) {
    currentFrame += ms * animationFramerate; 
    if( currentFrame > animationSequence.length ) 
        currentFrame %= animationSequence.length;


Thats pretty much it for the lazy-mans version. There are some fixes which could be made for even better synchronization. Thats by adding logic which can handle "half" frames. You can then use a variable for time spillover, i.e. when you show frame N and you still have som time left on that frame before moving into the next one. 


In the above code, there are two ways to tweek your animation speed. One is to change the value of 1000F to either a higher value (slows down the animation) or vice versa for a faster one OR you can alter the animation sequence to have more frames of the same index in a row. When it comes to the rendering part you have two options as well. Either treat the index value in the sequence as an Image in your array or my preferred choice an offset in a sprite-sheet.

In Topic: Game Loop locking up my game

18 April 2013 - 06:10 AM

Seems you start and set the game running (basically an eternity loop until members isLooping && isRunning is set to false) from the onMouseClick. OnMouseClick is probably called by some thread in Swings complex thred/event handling mechanism and you freeze it with your game loop. 


I would suggest that in event callback methods called by Swing you just set flags which hits the Game class what to do and then handle those flags in the main game loop instead.