9 Replies - 566 Views - Last Post: 24 May 2011 - 06:04 AM Rate Topic: -----

#1 speed-e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 11

Class aware of another class

Posted 19 May 2011 - 12:12 PM

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();
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Class aware of another class

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Class aware of another class

Posted 19 May 2011 - 01:27 PM

Quote

I have played around with creating the background before or after I create my sprite, but still background always stays at the top.


Have you changed the order you draw them in?
Was This Post Helpful? 1
  • +
  • -

#3 speed-e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 11

Re: Class aware of another class

Posted 19 May 2011 - 01:51 PM

Yes, I tried that several times. My character appears at the top of the screen, the background image from full size shrinks down. Then my character starts falling down (ground position set as a default is 380) and just as it reaches background height it hides behind the image. Here are some screens if you don't understand what I mean.

Thats how it looks like if I create Cyclops first, then background
Posted Image

Thats when I reverse the order and create background first
Posted Image

Falling down a second later
Posted Image

Background hides when I use if(dbSpaceKey()) newBg.hide();
Posted Image

This post has been edited by speed-e: 19 May 2011 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Class aware of another class

Posted 19 May 2011 - 02:33 PM

That is strange, I will take another look when I get time :)

Just as a question
int getX(int cyclopsX)
{
	cyclopsX = dbSpriteX(CYCLOPS_SPRITE);
	return cyclopsX;
}

int getY(int cyclopsY)
{
	cyclopsY = dbSpriteY(CYCLOPS_SPRITE);
	return cyclopsY;
}



Why do you have these functions taking parameters? It is quite unnecessary. With your method you have to create a variable and then assign it using this function. A much easier and more efficient way is to just return those values directly.

int getX()
{
   return dbSpriteX(CYCLOPS_SPRITE);
}

int getY()
{
   return dbSpriteY(CYCLOPS_SPRITE);
}



These functions can be put into your header file with the possibility of being inlined as well. Also I noticed that you didn't use the scope resolution :: operator on those getX/Y functions in the cpp file anyway, you are defining global functions and not defining the functions you declared in the header.
Was This Post Helpful? 0
  • +
  • -

#5 speed-e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 11

Re: Class aware of another class

Posted 19 May 2011 - 03:34 PM

Thanks for pointing out the mistake, I have already corrected it :)

How can I make use of these getX functions? I would like the program to constantly compare player.getX with enemy.getX and tell enemy to chase the player. I have tried this in the main loop, but I believe it only checks the position once at the start of the game and then it doesn't constantly update itself.

int a;
a = newPlayer.getX();

int b;
b = newEnemy.getX();

if(b<a) newEnemy.moveRight();
if(b>a) newEnemy.moveLeft();
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Class aware of another class

Posted 20 May 2011 - 07:21 AM

Well you could put the comparison into a function
void DoAI()
{
  if(b<a) newEnemy.moveRight();
  if(b>a) newEnemy.moveLeft(); 
}



Then call the function each frame.

Also those values should be member variables not created as globals, you don't have to create the a and b variables when you can just make them member variables and use them like so
if(newEnemy.getX < newPlayer.getX) 
{
  newEnemy.moveRight();
}
else if(newEnemy.getX > newPlayer.getX)
{ 
  newEnemy.moveLeft();
} 



Do you see what I mean?
Was This Post Helpful? 0
  • +
  • -

#7 speed-e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 11

Re: Class aware of another class

Posted 24 May 2011 - 02:53 AM

Sorry, I was away for a couple of days and I didn't have access to the Internet. I have tried your idea, although I get a bunch of errors both if I do it in a seperate function or without a function.

Without a function:
		if(meleeEnemy.getEnemyX < newPlayer.getX)
		{
			meleeEnemy.moveRight();
		}

		else if(meleeEnemy.getEnemyX > newPlayer.getX)
		{	 
			meleeEnemy.moveLeft();
		} 



ERRORS:
Error	9	error C2296: '<' : illegal, left operand has type 'int (__thiscall Enemy::* )(void)'	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	34
Error	4	error C2296: '>' : illegal, left operand has type 'int (__thiscall Enemy::* )(void)'	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	29
Error	10	error C2297: '<' : illegal, right operand has type 'int (__thiscall Wolverine::* )(void)'	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	34
Error	5	error C2297: '>' : illegal, right operand has type 'int (__thiscall Wolverine::* )(void)'	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	29
Error	8	error C2446: '<' : no conversion from 'int (__thiscall Wolverine::* )(void)' to 'int (__thiscall Enemy::* )(void)'	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	34
Error	3	error C2446: '>' : no conversion from 'int (__thiscall Wolverine::* )(void)' to 'int (__thiscall Enemy::* )(void)'	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	29
Error	1	error C3867: 'Enemy::getEnemyX': function call missing argument list; use '&Enemy::getEnemyX' to create a pointer to member	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	29
Error	6	error C3867: 'Enemy::getEnemyX': function call missing argument list; use '&Enemy::getEnemyX' to create a pointer to member	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	34
Error	2	error C3867: 'Wolverine::getX': function call missing argument list; use '&Wolverine::getX' to create a pointer to member	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	29
Error	7	error C3867: 'Wolverine::getX': function call missing argument list; use '&Wolverine::getX' to create a pointer to member	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	34



If I put it into a function created in the main.cpp
void moveEnemy()
{
		if(meleeEnemy.getEnemyX < newPlayer.getX)
		{
			meleeEnemy.moveRight();
		}

		else if(meleeEnemy.getEnemyX > newPlayer.getX)
		{	 
			meleeEnemy.moveLeft();
		} 
}



ERRORS
Error	1	error C2065: 'meleeEnemy' : undeclared identifier	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	39
Error	5	error C2065: 'meleeEnemy' : undeclared identifier	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	41
Error	7	error C2065: 'meleeEnemy' : undeclared identifier	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	44
Error	11	error C2065: 'meleeEnemy' : undeclared identifier	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	46
Error	3	error C2065: 'newPlayer' : undeclared identifier	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	39
Error	9	error C2065: 'newPlayer' : undeclared identifier	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	44
Error	2	error C2228: left of '.getEnemyX' must have class/struct/union	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	39
Error	8	error C2228: left of '.getEnemyX' must have class/struct/union	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	44
Error	4	error C2228: left of '.getX' must have class/struct/union	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	39
Error	10	error C2228: left of '.getX' must have class/struct/union	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	44
Error	12	error C2228: left of '.moveLeft' must have class/struct/union	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	46
Error	6	error C2228: left of '.moveRight' must have class/struct/union	c:\users\adrian\desktop\college\graded unit\development\wolverine animation\wolverine animation\main.cpp	41



How would I let the moveEnemy function know that the object meleeEnemy and newPlayer has already been created?

This post has been edited by speed-e: 24 May 2011 - 02:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Class aware of another class

Posted 24 May 2011 - 03:59 AM

Please post more of your code. What I meant was that the move function would be a member function.
Was This Post Helpful? 0
  • +
  • -

#9 speed-e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 11

Re: Class aware of another class

Posted 24 May 2011 - 04:48 AM

I forgot to add () on the end of member functions! Anyway, I have changed the code a little bit and now it only moves enemy to the right. I am working on my algorithm, but this is the code so far:
Main.cpp
#include "DarkGDK.h"
#include "Wolverine.h"
#include "Enemies.h"

void DarkGDK ( void )
{
	dbSyncOn ( );
	dbSyncRate ( 60 );

	Wolverine newPlayer(100,380);
	newPlayer.create();

	Enemy meleeEnemy(500,386);
	meleeEnemy.create();

	
	while ( LoopGDK ( ) )
	{
		newPlayer.paste();	
		newPlayer.moveRight();
		newPlayer.moveLeft();
		newPlayer.slashAttack();
		newPlayer.punchAttack();
		newPlayer.dashAttack();
		newPlayer.jump();

		meleeEnemy.paste();

		if(dbSpriteFrame(1) > 0)
		{
			if(meleeEnemy.getEnemyX() - newPlayer.getX() > 10 )
			{	
				meleeEnemy.moveLeft();
			} 
			else if(meleeEnemy.getEnemyX() - newPlayer.getX() < 10)
			{
				meleeEnemy.moveRight();
			}
		}

		dbSync();
	}

}




Wolverine.cpp
#include "DarkGDK.h"
#include "Wolverine.h"

bool g_bFacingRight;
bool g_bSlashing;
bool g_bPunching;
bool g_bDashing;
	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 Wolverine::getX()
{
	return dbSpriteX(1);
}

int Wolverine::getY()
{
	return dbSpriteY(1);
}

void Wolverine::create()
{
	dbSetImageColorKey(255,0,255);
	dbCreateAnimatedSprite(1,"wolverine.bmp", 8, 4, 1);
}

void Wolverine::paste()
{
	dbPasteSprite(1, wolverineX, wolverineY);
}

void Wolverine::moveRight()
{
		if(dbRightKey() == 1)
		{	
			if(dbSpriteFrame(1) < 2 || dbSpriteFrame(1) > 8)
			{
				dbSetSpriteFrame(1,2);
			}

			if(dbSpriteMirrored(1))
			{
				dbMirrorSprite(1);
				g_bFacingRight = true;
			}
				dbPlaySprite(1,2,8,100);
				wolverineX += PLAYER_MOVE;
		}
}

void Wolverine::moveLeft()
{
		if(dbLeftKey())
		{		
			if(dbSpriteFrame(1) < 2 || dbSpriteFrame(1) > 8)
			{
				dbSetSpriteFrame(1,2);
			}
				if(!dbSpriteMirrored(1))
				{
					dbMirrorSprite(1);
					g_bFacingRight = false;
				}

				if(dbSpriteMirrored(1))
				{
					wolverineX -= PLAYER_MOVE;
					dbPlaySprite(1,2,8,100);
				}
		}
}

void Wolverine::slashAttack()
{
		if(dbKeyState(16) == 1)
		{
			g_bSlashing = true;

		}

		// Kick animation
		if (g_bSlashing)
		{
				if(dbSpriteFrame(1) < 17 || dbSpriteFrame(1) > 22)
				{
					dbSetSpriteFrame(1,17);
				}
				
				dbPlaySprite(1,17,22,5);
				dbWait(100);

				if(dbSpriteFrame(1) == 22)
				{
					g_bSlashing = false;
					dbSetSpriteFrame(1,1);
				}
		}
}

void Wolverine::punchAttack()
{
	// Set "W" to activate punch
	if(dbKeyState(17) == 1)
	{
		g_bPunching = true;
	}
			// Punch animation
			if (g_bPunching)
			{
				if(dbSpriteFrame(1) < 9 || dbSpriteFrame(1) > 16)
				{
					dbSetSpriteFrame(1,9);
				}

				dbPlaySprite(1,9,16,5);
				dbWait(100);

				if(dbSpriteFrame(1) == 16)
				{
					g_bPunching = false;
					dbSetSpriteFrame(1,1);
				}
			}
}

void Wolverine::dashAttack()
{
	// Set "W" to activate punch
	if(dbKeyState(18) == 1)
	{
		g_bDashing = true;
	}
			// Punch animation
			if (g_bDashing)
			{
				if(dbSpriteFrame(1) < 25 || dbSpriteFrame(1) > 29)
				{
					dbSetSpriteFrame(1,25);
				}

				dbPlaySprite(1,25,29,100);

								
				if(dbSpriteMirrored(1))
				{
					wolverineX -= DASH_MOVE;
				}
				else
				{
					wolverineX += DASH_MOVE;
				}

				if(dbSpriteFrame(1) == 29)
				{
					g_bDashing = false;
					dbSetSpriteFrame(1,1);
				}
			}
}

void Wolverine::jump()
{
		if(dbSpaceKey())
		{
			SpacePressed++;
		}//Pressed Jump Key

		else
		{
			SpacePressed=0;
		}//Did not press Jump Key

		if(wolverineY > 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(1) < 23 || dbSpriteFrame(1) > 23)
			{
				dbSetSpriteFrame(1,23);
			}
			dbPlaySprite(1,23,23,100);
		}//Player is not touching ground, apply gravity

		wolverineY=wolverineY-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(1) == 23)
				{
					dbSetSpriteFrame(1,1);
				}
		}

		wolverineX=wolverineX+Vx;
}



Wolverine.h
#include "Variables.h"

class Wolverine {

public:
	Wolverine(int x, int y) { wolverineX = x; wolverineY = y; };
	int getX();
	int getY();
	void create();
	void paste();
	void moveRight();
	void moveLeft();
	void slashAttack();
	void punchAttack();
	void dashAttack();
	void jump();

protected:
	int wolverineX;
	int wolverineY;
};



Enemies.cpp
#include "DarkGDK.h"
#include "Enemies.h"

int Enemy::getEnemyX()
{
	return dbSpriteX(ENEMY_SPRITE);
}

int Enemy::getEnemyY()
{
	return dbSpriteY(ENEMY_SPRITE);
}

void Enemy::create()
{
	dbSetImageColorKey(255,0,255);
	dbCreateAnimatedSprite(ENEMY_SPRITE,"enemy.bmp",5,2,ENEMY_IMAGE);
}

void Enemy::paste()
{
	dbPasteSprite(ENEMY_SPRITE, enemyX, enemyY);
}

void Enemy::moveLeft()
{
	dbPlaySprite(ENEMY_SPRITE,1,5,100);

	enemyX-=ENEMY_MOVE;
			//if (enemyX <= 0)
			//{
			//	dbMirrorSprite(ENEMY_SPRITE);
			//}

			//if (enemyX + dbSpriteWidth(ENEMY_SPRITE) >= dbScreenWidth())
			//{
			//	dbMirrorSprite(ENEMY_SPRITE);
			//}

			//if (enemyX > 580 || enemyX <= 0)
			//{
			//	enemyX += ENEMY_MOVE;
			//}

			//// If the enemy is currently mirrored, move right.
			//if ( dbSpriteMirrored(ENEMY_SPRITE) )
			//{
			//	enemyX += ENEMY_MOVE;
			//}
			//// If the enemy is not mirrored, move left.
			//else
			//{
			//	enemyX -= ENEMY_MOVE;
			//}

			//if(enemyX == dbSpriteX(1))
			//{
			//	dbSetSpriteFrame(ENEMY_SPRITE,6);
			//}
}

void Enemy::moveRight()
{
	enemyX+=ENEMY_MOVE;
}



Enemies.h

const int ENEMY_MOVE = 2;

const int ENEMY_SPRITE = 3;

const int ENEMY_IMAGE = 3;

class Enemy {

public:
	Enemy(int x, int y) { enemyX = x; enemyY = y; };
	int getEnemyX();
	int getEnemyY();
	void create();
	void paste();
	void moveLeft();
	void moveRight();

protected:
	int enemyX;
	int enemyY;
};


This post has been edited by speed-e: 24 May 2011 - 04:56 AM

Was This Post Helpful? 0
  • +
  • -

#10 speed-e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 11

Re: Class aware of another class

Posted 24 May 2011 - 06:04 AM

I have managed to get it to work! :) Thanks for your interest in my problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1