This would be my second post about the same project, my experience so far has been rather nice.
So anyway, I ran into the issue with the command dbPlaySprite () as playing the sprite would cause problems since the command will not stop playing the sprite until its gone through its rotation. For example dbPlaySprite ( 1, 1, 6, 60 ); if I bind the left arrow key, so that it will move left while playing those frames, and have
dbPlaySprite ( 1, 7, 12, 60 ); for the right key, so that the sprite will move right while playing those frames.
The issue is, if you where to switch the keys during the middle of playing the sprite, it will not switch to the other until it is finished with the first. Meaning that while moving to the left and quickly to the right, the dbPlaySprite () intended for the left button press would finish first before starting the other for te right.
So I decided to use something different, based off the tutorial, i set in int value for the current sprite dbSetSpriteFrame ( 1 , iPlayerFrame ); I ended up doing a series of if statments, but ran into a similar problem. The cycle for the sprite will not end until it gets to the a certain point. However, I do not know how to fix it so that it while the button is not being press, it will for to a default sprite for that direction, as well as make it quickly switch when I press another button.
The code should make is clearer. This is based off the dark Invaders code. As I'm trying to learn this before trying anything on my own.
Player.cpp
#include "DarkSDK.h"
#include "Player.h"
#include "Input.h"
//#include "Enemies.h"
//#include "Invader Text.h"
// player variables
// Player X position
int iPlayerX = 448;
// player Y position
int iPlayerY = 400;
// speed the player should move
int iPlayerSpeed = 2;
// current frame the player is on
int iPlayerFrame = 13;
// we increment this to form a delay for out animation
int iFrameAnimateDelay = 0;
void playerSetup ( void )
{
// the player animated sprite
dbCreateAnimatedSprite ( 1 , "Cronoo.bmp", 6, 3, 1 );
dbSprite ( 1 , iPlayerX , iPlayerY , 1 );
dbSetSpriteFrame ( 1 , iPlayerFrame );
dbSetSpritePriority ( 1 , 2 );
}
void playerUpdate ( void )
{
// see if the player ship needs to try and go left
if ( checkLeft() )
{
iPlayerX -= iPlayerSpeed;
if ( iPlayerX < 0 )
iPlayerX = 0;
iPlayerDirection = LEFT;
}
// see if the player ship needs to try and go right
else if ( checkRight() )
{
iPlayerX += iPlayerSpeed ;
if ( iPlayerX > 1024 - 128 )
iPlayerX = 1024 - 128;
iPlayerDirection = RIGHT;
}
// not left or right, so lets face straight
else
iPlayerDirection = STRAIGHT;
// animate the player
playerAnimate();
// check for player firing and update firing if a bullet is already in action
player.h #pragma once
void playerSetup ( void );
void playerUpdate ( void );
void playerAnimate ( void );
extern int iPlayerX;
extern int iPlayerY;
extern int iPlayerSpeed;
extern int iPlayerFrame;
extern int iFrameAnimateDelay;
Input.cpp#include "DarkSDK.h"
#include "Input.h"
// check the keys we have assigned to be the fire key
// check the keys we have assigned to be the left key
bool checkLeft ( void )
{
if ( dbKeyState ( 203 ) || dbKeyState ( 30 ) )
return true;
return false;
}
// check the keys we have assigned to be the right key
bool checkRight ( void )
{
if ( dbKeyState ( 205 ) || dbKeyState ( 32 ) )
return true;
return false;
}
Input.h
#pragma once
bool checkFire ( void );
bool checkLeft ( void );
bool checkRight ( void );
Main.cpp
#include "DarkGDK.h"
#include "Input.h"
#include "Player.h"
#include "windows.h"
void DarkGDK ( void )
dbSyncOn ( );
dbSyncRate ( 60 );
dbDisableEscapeKey ( );
dbRandomize ( dbTimer ( ) );
while ( LoopGDK ( ) )
{
backdropUpdate();
playerUpdate ();
playerAnimate ();
playerUpdateFire ();
if ( dbEscapeKey ( ) )
break;
dbSync ( );
}
return;
}