The problem is, when press the Left key, the left animation starts after you are already moving. Its like this for both left and right. Its obviously cause of my inefficient code. Also if you spot any other things that could be improved, let me know.
#include "DarkGDK.h"
int playerx = 20;
int playery = 600;
void Initialize()
{
dbSetWindowOff ( );
dbSetDisplayMode ( 1024, 768, 32 );
dbHideMouse ( );
dbPositionMouse ( 50, 50 );
}
void LoadImages()
{
dbLoadImage( "data/sky.png", 2 ); // Sky
dbLoadImage( "data/building1.png", 3 ); // Building 1
dbLoadImage( "data/building2.png", 4 ); // Building 2
dbLoadImage( "data/building3.png", 5 ); // Building 3
dbLoadImage( "data/cloud0.png", 6 ); // Cloud 0
dbLoadImage( "data/cloud1.png", 7 ); // Cloud 1
dbLoadImage( "data/cloud2.png", 8 ); // Cloud 2
dbLoadImage( "data/cloud3.png", 9 ); // Cloud 3
dbLoadImage( "data/hills.png", 10 ); // Hills
dbLoadImage( "data/ground.png", 11 ); // Ground
// Priority, 10 = top; 1 = Bottom
dbSetSpritePriority( 100, 11 ); // Idle
dbSetSpritePriority( 2, 1 ); // Sky
dbSetSpritePriority( 3, 7 ); // Building 1
dbSetSpritePriority( 4, 8 ); // Building 2
dbSetSpritePriority( 5, 10 ); // Building 3
dbSetSpritePriority( 6, 2 ); // Cloud 0
dbSetSpritePriority( 7, 3 ); // Cloud 1
dbSetSpritePriority( 8, 4 ); // Cloud 2
dbSetSpritePriority( 9, 5 ); // Cloud 3
dbSetSpritePriority( 10, 6 ); // Hills
dbSetSpritePriority( 11, 9 ); // Ground
}
void SpriteHandling()
{
dbSprite( 2, 0, 0, 2 );
dbSprite( 3, 679, 250, 3 );
//dbSprite( 4, x, y, 4 );
//dbSprite( 5, x, y, 5 );
//dbSprite( 6, x, y, 6 );
//dbSprite( 7, x, y, 7 );
//dbSprite( 8, x, y, 8 );
//dbSprite( 9, x, y, 9 );
dbSprite( 10, 0, 474, 10 );
dbSprite( 11, 0, 699, 11 );
dbSprite( 100, playerx, playery, 100 );
dbCreateAnimatedSprite ( 100, "data/Character/Character.png", 16, 1, 100 );
}
void SpriteScaling()
{
dbScaleSprite ( 2, 170 );
}
void SpriteCollision()
{
dbSpriteCollision ( 100, 11 );
if ( dbSpriteHit ( 100, 11 ) == 1 )
{
playery;
}
else
{
playery++;
}
}
/*1-4 = Walk Left
5-8 = Walk Right
9-12 = Idle Left
13-16 = Idle Right*/
void PlayerMovementLeft()
{
if ( dbLeftKey() )
{
dbPlaySprite( 100, 1, 4, 210 );
playerx--;
}
}
void PlayerMovementRight()
{
if ( dbRightKey() )
{
dbPlaySprite( 100, 5, 8, 210 );
playerx++;
}
}
void DarkGDK ( void )
{
dbSyncOn();
dbSyncRate(60);
Initialize();
LoadImages();
while ( LoopGDK ( ) )
{
SpriteHandling();
SpriteScaling();
SpriteCollision();
PlayerMovementLeft();
PlayerMovementRight();
dbSync ( );
}
return;
}
www.Helios-Online.net