Hello everyone
The problem that I am facing at the moment is how to make classes communicate with each other.
I am making a game with different classes and so now I have got the player, background and enemy. I have made a background and I am trying to paste it behind the player so he can walk on its surface. Unfortunately when I create the background class, my character disappears from the screen. I know that the player is behind the background, because I have used the dbShowSprite and dbHideSprite function. I have also tried to use dbSpritePriority to make my character a 1st plan sprite - it never worked. I have played around with creating the background before or after I create my sprite, but still background always stays at the top. Do you know why?
P.S. I am sorry for so many topics requesting help, but I guess that everyone has to learn these things one day and I can see I am only getting better in finding solutions to my problems
Background.h
class Background {
public:
Background(int,char*, int, int, int);
void load();
void create();
void show();
void hide();
protected:
int spriteID;
char* filename;
int positionX;
int positionY;
int imageID;
};
Background.cpp
#include "DarkGDK.h"
#include "Background.h"
Background::Background(int sprID, char* name, int posX, int posY, int imgID)
{
spriteID = sprID;
filename = name;
positionX = posX;
positionY = posY;
imageID = imgID;
}
void Background::load()
{
dbLoadImage(filename,spriteID);
}
void Background::create()
{
dbSprite(spriteID,positionX,positionY,imageID);
}
void Background::show()
{
dbShowSprite(spriteID);
}
void Background::hide()
{
dbHideSprite(spriteID);
}
Cyclops.h
#include "Variables.h"
class Laser;
class Cyclops {
public:
Cyclops(int x, int y) { cyclopsX = x; cyclopsY = y; };
int getX(int cyclopsX);
int getY(int cyclopsY);
void create();
void paste();
void moveRight();
void moveLeft();
void kickAttack();
void laserAttack();
void punchAttack();
void jump();
protected:
int cyclopsX;
int cyclopsY;
int laserX;
int laserY;
};
Cyclops.cpp (player)
#include "DarkGDK.h"
#include "Cyclops.h"
#include "Laser.h"
const int CYCLOPS_SPRITE = 2;
const int CYCLOPS_IMAGE = 2;
bool g_bFacingRight;
bool g_bKicking;
bool g_bFiringLaser;
bool g_bPunching;
float Vx=0.0f;
float Vy=0.0f;
float Gravity = 0.2f;
float GroundFriction = 0.05f;
bool Ground=true;
int JumpLeft=1;
int SpacePressed=0;
int getX(int cyclopsX)
{
cyclopsX = dbSpriteX(CYCLOPS_SPRITE);
return cyclopsX;
}
int getY(int cyclopsY)
{
cyclopsY = dbSpriteY(CYCLOPS_SPRITE);
return cyclopsY;
}
void Cyclops::create()
{
dbSetImageColorKey(255,0,255);
dbCreateAnimatedSprite(CYCLOPS_SPRITE,"cyclops.bmp", 9, 7, 2);
}
void Cyclops::paste()
{
dbPasteSprite(CYCLOPS_SPRITE, cyclopsX, cyclopsY);
}
void Cyclops::moveRight()
{
if(dbRightKey() == 1)
{
if(dbSpriteFrame(CYCLOPS_SPRITE) < 10 || dbSpriteFrame(CYCLOPS_SPRITE) > 14)
{
dbSetSpriteFrame(CYCLOPS_SPRITE,10);
}
if(dbSpriteMirrored(CYCLOPS_SPRITE))
{
dbMirrorSprite(CYCLOPS_SPRITE);
g_bFacingRight = true;
}
dbPlaySprite(CYCLOPS_SPRITE,10,14,100);
cyclopsX += PLAYER_MOVE;
}
}
void Cyclops::moveLeft()
{
if(dbLeftKey())
{
if(dbSpriteFrame(CYCLOPS_SPRITE) < 10 || dbSpriteFrame(CYCLOPS_SPRITE) > 14)
{
dbSetSpriteFrame(CYCLOPS_SPRITE,10);
}
if(!dbSpriteMirrored(CYCLOPS_SPRITE))
{
dbMirrorSprite(CYCLOPS_SPRITE);
g_bFacingRight = false;
}
if(dbSpriteMirrored(CYCLOPS_SPRITE))
{
cyclopsX -= PLAYER_MOVE;
dbPlaySprite(CYCLOPS_SPRITE,10,14,100);
}
}
}
void Cyclops::kickAttack()
{
if(dbKeyState(16) == 1)
{
g_bKicking = true;
}
// Kick animation
if (g_bKicking)
{
if(dbSpriteFrame(CYCLOPS_SPRITE) < 54 || dbSpriteFrame(CYCLOPS_SPRITE) > 61)
{
dbSetSpriteFrame(CYCLOPS_SPRITE,54);
}
dbPlaySprite(CYCLOPS_SPRITE,54,61,5);
dbWait(100);
if(dbSpriteFrame(CYCLOPS_SPRITE) == 61)
{
g_bKicking = false;
dbSetSpriteFrame(CYCLOPS_SPRITE,1);
}
}
}
void Cyclops::laserAttack()
{
Laser newLaser;
newLaser.load();
newLaser.create(cyclopsX,cyclopsY);
newLaser.hide();
// Set "E" to activate laser
if(dbKeyState(18) == 1)
{
g_bFiringLaser = true;
}
// Laser animation
if(g_bFiringLaser)
{
if(dbSpriteFrame(CYCLOPS_SPRITE) < 28 || dbSpriteFrame(CYCLOPS_SPRITE) > 32)
{
dbSetSpriteFrame(CYCLOPS_SPRITE,28);
}
dbPlaySprite(CYCLOPS_SPRITE,28,32,100);
newLaser.show();
//int posX;
//posX = newLaser.getPositionX(dbSpriteX(3));
//while(posX < dbScreenWidth())
//{
// newLaser.moveRight();
//}
if(dbSpriteFrame(CYCLOPS_SPRITE)>=32)
{
dbSetSpriteFrame(CYCLOPS_SPRITE,1);
g_bFiringLaser = false;
}
}
}
void Cyclops::punchAttack()
{
// Set "W" to activate punch
if(dbKeyState(17) == 1)
{
g_bPunching = true;
}
// Punch animation
if (g_bPunching)
{
if(dbSpriteFrame(CYCLOPS_SPRITE) < 37 || dbSpriteFrame(CYCLOPS_SPRITE) > 42)
{
dbSetSpriteFrame(CYCLOPS_SPRITE,37);
}
dbPlaySprite(CYCLOPS_SPRITE,37,42,5);
dbWait(100);
if(dbSpriteFrame(CYCLOPS_SPRITE) == 42)
{
g_bPunching = false;
dbSetSpriteFrame(CYCLOPS_SPRITE,1);
}
}
}
void Cyclops::jump()
{
if(dbSpaceKey())
{
SpacePressed++;
}//Pressed Jump Key
else
{
SpacePressed=0;
}//Did not press Jump Key
if(cyclopsY > 380.0f)//Player is touching ground, reset jumpstate
{
Ground=true;
JumpLeft=1;
Vy=0.0f;
}
else{Ground=false;}//Player not touching ground
if(SpacePressed==1)//We only want to detect when the Jumpkey was pressed.
{
if(JumpLeft>0)
{
Vy=7.0;
JumpLeft--;
Ground=false;
}//add vertical force, and will no longer be on ground
}
if(Ground==false)
{
Vy=Vy-Gravity;
if(dbSpriteFrame(CYCLOPS_SPRITE) < 46 || dbSpriteFrame(CYCLOPS_SPRITE) > 46)
{
dbSetSpriteFrame(CYCLOPS_SPRITE,46);
}
dbPlaySprite(CYCLOPS_SPRITE,46,46,100);
}//Player is not touching ground, apply gravity
cyclopsY=cyclopsY-Vy;//Make changes to Y position
//End Jump code
if(dbLeftKey()){Vx=-2.0f;}
if(dbRightKey()){Vx=2.0f;}
//This code stops the player absolutely
if(Vx<0.5f && Vx>-0.5f){Vx=0.0f;}
//Ground Friction
if(Ground==true)
{
if(Vx>0.5f){Vx=Vx-GroundFriction;}
if(Vx<-0.5f){Vx=Vx+GroundFriction;}
if(dbSpriteFrame(CYCLOPS_SPRITE) == 46)
{
dbSetSpriteFrame(CYCLOPS_SPRITE,1);
}
}
cyclopsX=cyclopsX+Vx;
}
Main.cpp
#include "DarkGDK.h"
#include "Cyclops.h"
#include "Background.h"
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
Cyclops newPlayer(0,0);
newPlayer.create();
Background newBg(1,"sewer.bmp",0,0,1);
newBg.load();
newBg.create();
//newBg.hide();
while ( LoopGDK ( ) )
{
newPlayer.paste();
newPlayer.moveRight();
newPlayer.moveLeft();
newPlayer.kickAttack();
newPlayer.laserAttack();
newPlayer.punchAttack();
newPlayer.jump();
dbSync();
}
}