Helicopter Game Classes

Started by
5 comments, last by karwosts 13 years, 6 months ago
Hello, everybody :] I have a more substantial problem this time. What I'm doing is constructing a helicopter game where you just move up, down, left, and right from a profile view as you fly through rocks that come at you from the right. There are four classes I've made: Helicopter, Rock, Background, and Missile. What I'm currently having a problem with is compiling the Missile and Rock classes because of a couple of errors I've never seen before. Here are the sources for the headers and implementation files of the Missile and Rock classes:

// This header is used to define the member variables and functions of the Rock class.#include <SFML/Graphics.hpp>#include "Missile.h"using namespace sf;#ifndef ROCK_H#define ROCK_Henum {TINY, SMALL, MEDIUM, BIG}class Rock{   private:      Sprite rockSprite;      int x, y;	  bool isFloor;	  int rockType;	  int collisions[2][4];   public:      Rock(int, bool, Image&, Image&, Image&, Image&);	  ~Rock();	  void increaseX(int);	  void initializeCoords();	  bool testCollision(const Helicopter&);	  void returnCollisions(int[][4]);	  bool onFloor();	  int whatType();};#endif


// This header file defines all of the member variables and functions for the Missile class.#include <SFML/Graphics.hpp>using namespace sf;#ifndef MISSILE_H#define MISSILE_Hclass Missile{   private:	  Sprite missileSprite;	  int x, y;	  int collisions[2][4];   public:      Missile(int, int, Image&);	  ~Missile();	  void increaseX(int);	  void increaseY(int);	  bool testCollision(const Missile&);};#endif


// This implementation holds all of the details for the Rock class.#include <SFML/Graphics.hpp>#include "Rock.h"#include "Helicopter.h"using namespace sf;// ConstructorRock::Rock(int type, bool floor, Image &rock1, Image &rock2, Image &rock3, Image &rock4){   rockType = type;   isFloor = floor;      if (isFloor)   {      switch (rockType)	  {	     case TINY:		    rockSprite.SetImage(rock1);			x = 1200;			y = 690;			break;		 case SMALL:		    rockSprite.SetImage(rock2);			x = 1200;			y = 640;			break;		 case MEDIUM:		    rockSprite.SetImage(rock3);			x = 1200;			y = 590;		 case BIG:		    rockSprite.SetImage(rock4);			x = 1200;			y = 575;			break;	  }	}	else	{	   switch (rockType)	   {		 case TINY:		    rockSprite.SetImage(rock1);			rockSprite.FlipX(true);			x = 1200;			y = 50;			break;		 case SMALL:		    rockSprite.SetImage(rock2);			rockSprite.FlipX(true);			x = 1200;			y = 100;			break;		 case MEDIUM:		    rockSprite.SetImage(rock3);			rockSprite.FlipX(true);			x = 1200;			y = 150;		 case BIG:		    rockSprite.SetImage(rock4);			rockSprite.FlipX(true);			x = 1200;			y = 175;			break;	    }	}	this->initializeCoords();	rockSprite.SetX(x);	rockSprite.SetY(y);}// DestructorRock::~Rock(){}// Add to or decrease the X coordinate and bounding box coordsvoid Rock::increaseX(int increaser){    x += increaser;	rockSprite.SetX(x);    collisions[0][0] = x - 21;	collisions[0][1] = x + 21;	collisions[0][2] = x - 21;	collisions[0][3] = x + 21;}// Initialize the bounding box coordinates of the Rock objectvoid Rock::initializeCoords(){      switch (rockType)	  {	     case TINY:		    collisions[0][0] = x - 21;			collisions[0][1] = x + 21;			collisions[0][2] = x - 21;			collisions[0][3] = x + 21;			collisions[1][0] = y - 50;			collisions[1][1] = y - 50;			collisions[1][2] = y + 50;			collisions[1][3] = y + 50;			break;		 case SMALL:		    collisions[0][0] = x - 43;			collisions[0][1] = x + 43;			collisions[0][2] = x - 43;			collisions[0][3] = x + 43;			collisions[1][0] = y - 100;			collisions[1][1] = y - 100;			collisions[1][2] = y + 100;			collisions[1][3] = y + 100;			break;		 case MEDIUM:		    collisions[0][0] = x - 64;			collisions[0][1] = x + 64;			collisions[0][2] = x - 64;			collisions[0][3] = x + 64;			collisions[1][0] = y - 150;			collisions[1][1] = y - 150;			collisions[1][2] = y + 150;			collisions[1][3] = y + 150;			break;		 case BIG:		    collisions[0][0] = x - 75;			collisions[0][1] = x + 75;			collisions[0][2] = x - 75;			collisions[0][3] = x + 75;			collisions[1][0] = y - 175;			collisions[1][1] = y - 175;			collisions[1][2] = y + 175;			collisions[1][3] = y + 175;			break;		}}// Function to test for a helicopter collisionbool Rock::testCollision(const Helicopter &player){   int heliCollisions[2][4];   player.returnCollisions(heliCollisions);      return !(collisions[0][1] < heliCollisions[0][0] ||		// Fully to the left		    collisions[0][0] > heliCollisions[0][1] ||		// Fully to the right			collisions[1][2] > heliCollisions[1][0] ||		// Fully above			collisions[1][0] < heliCollisions[1][2]);		// Fully below}// Function to copy collision box into a passed-by-reference arrayvoid Rock::returnCollisions(int passedCollisions[0][4]){   passedCollisions[0][0] = collisions[0][0];   passedCollisions[0][1] = collisions[0][1];   passedCollisions[0][2] = collisions[0][2];   passedCollisions[0][3] = collisions[0][3];      passedCollisions[1][0] = collisions[1][0];   passedCollisions[1][1] = collisions[1][1];   passedCollisions[1][2] = collisions[1][2];   passedCollisions[1][3] = collisions[1][3];}// Returns whether it's on the floor or ceilingbool Rock::onFloor(){   if (isFloor)   {      return true;   }   else   {      return false;   }}// Returns the type of rockint Rock::whatType(){   return rockType;}


// This class implementation file holds the details of the Missile class.#include <SFML/Graphics.hpp>#include "Missile.h"#include "Rock.h"using namespace sf;// Constructor for the Missile class; also initializes collision boxMissile::Missile(int xCoord, int yCoord, Image &missileImage){   x = xCoord;   y = yCoord;   missileSprite.SetX(x);   missileSprite.SetY(y);   missileSprite.SetImage(missileImage);       collisions[0][0] = x - 23;	collisions[0][1] = x + 23;	collisions[0][2] = x - 23;	collisions[0][3] = x + 23;	collisions[1][0] = y - 9;	collisions[1][1] = y - 9;	collisions[1][2] = y + 9;	collisions[1][3] = y + 9;}// DestructorMissile::~Missile(){}// Function to test the intersection of collision boxesbool Missile::testCollision(const Rock::Rock &testRock){   int rockCollisions[2][4];   testRock.returnCollisions(rockCollisions);      return !(collisions[0][1] < rockCollisions[0][0] ||		// Fully to the left		    collisions[0][0] > rockCollisions[0][1] ||		// Fully to the right			collisions[1][2] > rockCollisions[1][0] ||		// Fully above			collisions[1][0] < rockCollisions[1][2]);		// Fully below}// Function to increase the x coordinate by value passedvoid Missile::increaseX(int increaser){    x += increaser;	missileSprite.SetX(x);    collisions[0][0] += increaser;	collisions[0][1] += increaser;	collisions[0][2] += increaser;	collisions[0][3] += increaser;}// Function to increase the y coordinate by value passedvoid Missile::increaseY(int increaser){    y += increaser;	missileSprite.SetY(y);    collisions[1][0] += increaser;	collisions[1][1] += increaser;	collisions[1][2] += increaser;	collisions[1][3] += increaser;}


The errors I'm getting come from both of the implementation files. Here's the example from the Rock class:

prototype for 'bool Rock::testCollision(const Helicopter&)' does not match any in class 'Rock'

But I did put that prototype in the header file! :p That's why I'm confused. Then it says this for a function within that function definition:

passing 'const Helicopter' as 'this' argument of 'void Helicopter::returnCollisions(int (*)[4])' discards qualifiers

In the Missile class, it does the exact same thing as that whole function except it's on the testCollision function.

Any help, guidance, or even code advice in general given the above source would be very appreciated :] Thank you all very much in advance.

Colton
Advertisement
Your Rock header appears to have no knowledge of any class called "Helicopter", despite requesting a reference to one for testCollision.

You should at least have to forward-declare "class Helicopter" inside of the Rock header if you want to take a reference to a helicopter.

Its hard to tell what the second one is about without seeing the Helicopter class.

What IDE is this? Those warnings seem like they could be better, though I may just be used to the visual studio ones.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
I added an #include "Helicopter.h" into the Rock class and that cleared up the problem you referred to first. Thank you very much :] I still have various other errors though, including the second of the two, but I'll try and get to those after this one is finished up. Here's the Helicopter header and implementation:

// This header file defines the class functions and variables for the Helicopter class. Defined in the notes doc.#include <SFML/Graphics.hpp>using namespace sf;#ifndef HELICOPTER_H#define HELICOPTER_Henum {FLAT, UP, DOWN};class Helicopter{   private:      // Image objects to store the different frames of helicopter animation      Image heli1;	  Image heli2;	  Image heli3;	  Image heli4;	  Image missileImage;	  	  Sprite heliSprite;	  Sprite missileAid;	  Clock animationTimer;	  Clock movementTimer;	  	  // X and Y coordinates	  int x, y;	  	  // Current frame of animation	  int currentFrame;	  	  // Collision box	  int collisions[2][4];	  	  int rotation;	  int missiles;	  	  // Pointer to dynamically make a missile	  Missile *missilePtr;	     public:      Helicopter(int, int);		// Constructor	  ~Helicopter();			// Destructor	  void increaseX(int);	  void increaseY(int);	  void setX(int);	  void setY(int);	  void adjustRotation(int);	  void returnCollisions(int[][4]);	  Missile fireMissile(int, int);	  void animationManager();};#endif


// This implementation file holds all of the implementation for the Helicopter class.#include <SFML/Graphics.hpp>#include "Helicopter.h"#include "Missile.h"using namespace sf;// Constructor to initialize x and y values and collision boxHelicopter::Helicopter(int xCoord, int yCoord){   // Load the images of the helicopter object   heli1.LoadFromFile("heli1.bmp");   heli2.LoadFromFile("heli2.bmp");   heli3.LoadFromFile("heli3.bmp");   heli4.LoadFromFile("heli4.bmp");   missileImage.LoadFromFile("missile.png");      heliSprite.SetImage(heli1);   missileAid.SetImage(missileImage);      x = xCoord;   y = yCoord;   heliSprite.SetX(x);   heliSprite.SetY(y);      currentFrame = 1;   missiles = 1;       collisions[0][0] = x - 87;    collisions[0][1] = x + 87;	collisions[0][2] = x - 87;	collisions[0][3] = x + 87;	collisions[1][0] = y - 30;	collisions[1][1] = y - 30;	collisions[1][2] = y + 30;	collisions[1][3] = y + 30;}// DestructorHelicopter::~Helicopter(){}// Change x value to a value passedvoid Helicopter::setX(int xCoord){   x = xCoord;   heliSprite.SetX(x);}// Change y value to a value passedvoid Helicopter::setY(int yCoord){   y = yCoord;   heliSprite.SetY(y);}// Increase the x value by an amount passedvoid Helicopter::increaseX(int xIncrease){   if (movementTimer.GetElapsedTime() < .005)   {      return;   }      x += xIncrease;   heliSprite.SetX(x);   collisions[0][0] += xIncrease;   collisions[0][1] += xIncrease;   collisions[0][2] += xIncrease;   collisions[0][3] += xIncrease;      movementTimer.Reset();}// Increase the y value by an amount passedvoid Helicopter::increaseY(int yIncrease){   y += yIncrease;   heliSprite.SetY(y);   collisions[1][0] += yIncrease;   collisions[1][1] += yIncrease;   collisions[1][2] += yIncrease;   collisions[1][3] += yIncrease;}// Adjust rotation of the sprite based upon direction of movementvoid Helicopter::adjustRotation(int rotate){   if (rotate == UP)   {      heliSprite.SetRotation(-35);	  missileAid.SetRotation(-35);   }   else if (rotate == DOWN)   {      heliSprite.SetRotation(35);	  missileAid.SetRotation(35);   }   else   {      heliSprite.SetRotation(0);	  missileAid.SetRotation(0);   }}// Fires a missile from the helicopterMissile Helicopter::fireMissile(int copterX, int copterY){   missiles--;   Missile *missilePtr = new Missile(copterX, copterY, &missileImage);      return *missilePtr;}// Function to copy collision box into a passed-by-reference arrayvoid Helicopter::returnCollisions(int passedCollisions[][4]){   passedCollisions[0][0] = collisions[0][0];   passedCollisions[0][1] = collisions[0][1];   passedCollisions[0][2] = collisions[0][2];   passedCollisions[0][3] = collisions[0][3];      passedCollisions[1][0] = collisions[1][0];   passedCollisions[1][1] = collisions[1][1];   passedCollisions[1][2] = collisions[1][2];   passedCollisions[1][3] = collisions[1][3];}// Keeps track of the helicopter's animationvoid Helicopter::animationManager(){   if (animationTimer.GetElapsedTime() < .1)   {      return;   }      if (currentFrame == 1)   {      heliSprite.SetImage(heli2);	  currentFrame++;   }   else if (currentFrame == 2)   {      heliSprite.SetImage(heli3);	  currentFrame++;   }   else if (currentFrame == 3)   {      heliSprite.SetImage(heli4);	  currentFrame++;   }   else if (currentFrame == 4)   {      heliSprite.SetImage(heli1);	  currentFrame = 1;   }      animationTimer.Reset();}


Again, I appreciate the help very much :]

Colton
Quote:
I added an #include "Helicopter.h" into the Rock class


In general you shouldn't do this when a forward declaration would suffice. Your Rock header doesn't need to know anything about helicopter, only that helicopter is a class.

By including helicopter.h inside rock.h you're creating a more complex chain of inclusion than is otherwise necessary, and will just cause more mistakes and longer build times.

When you include helicopter.h inside rock.h, you're automatically requiring that everything that wants to know about rock to include helicopter as well, even if that thing wants nothing to do with helicopter.

It won't kill you this time, but you should in practice only include headers inside headers when you HAVE to, and use forward declarations whenever you can get away with it. It will make your code cleaner in the long run.

instead of '#include "Helicopter.h"' just 'class Helicopter;' will do.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
karwosts,

Thank you very much. That took out some of the errors.... I now have only 9 instead of 19 :p But I forgot to answer... Xcode is the IDE I'm using. And also, I'm using the SFML graphics library for all of the graphics in this game.

I didn't know about forward declarations until just now, so thank you for showing me that. I'm still in the process of getting familiar with all of this stuff... it's my first big project using object-oriented programming :]

Here are some of the errors I have left, and I would definitely appreciate some assistance with these ones as well, if possible O:]

1. In the Helicopter implementation file, in the fireMissile function, the errors says: "no matching function for call to 'Missile::Missile(int&, int&, sf::Image*)'"
EDIT: Solved :] But not the others yet :[

2. In the Missile implementation file, in the testCollision function, the error says, right below the header line: "prototype for 'bool Missile::testCollision(const Rock&)' does not match any in class 'Missile'"

3. In the Missile implementation file, in the testCollision function, the error says, right after the testRock.returnCollisions(rockCollisions): passing 'const Rock' as 'this' argument of 'void Rock::return Collisions(int (*)[4])' discards qualifiers'

4. In the Rock implementation file, in the testCollision function, the error says, right under the player.returnCollisions(heliCollisions): invalid use of undefined type 'const struct Helicopter'

Those are the only errors at this point :] They seem to be rather specific, and I honestly don't know what to do to fix them. I appreciate all help very much.

Colton

[Edited by - Noegddgeon on September 28, 2010 12:00:30 AM]
I got everything to work now :] Thank you very much for your help. If I have any ongoing problems I'll post them, but until then, I appreciate the help that was offered to me :]

Colton
Quote:
karwosts,

Thank you very much. That took out some of the errors.... I now have only 9 instead of 19 :p But I forgot to answer... Xcode is the IDE I'm using. And also, I'm using the SFML graphics library for all of the graphics in this game.

I didn't know about forward declarations until just now, so thank you for showing me that. I'm still in the process of getting familiar with all of this stuff... it's my first big project using object-oriented programming :]

No problem, got to learn this stuff sometime :)

Quote:
2. In the Missile implementation file, in the testCollision function, the error says, right below the header line: "prototype for 'bool Missile::testCollision(const Rock&)' does not match any in class 'Missile'"

You need to look a little harder, your function prototype doesn't match the header declaration.

//.hclass Missile{	  bool testCollision(const Missile&);};//.cppbool Missile::testCollision(const Rock::Rock &testRock)


Quote:
3. In the Missile implementation file, in the testCollision function, the error says, right after the testRock.returnCollisions(rockCollisions): passing 'const Rock' as 'this' argument of 'void Rock::return Collisions(int (*)[4])' discards qualifiers'

I'm not positive about this, but I think it has to do with calling a non-const function (returnCollisions) on a const rock. If you have a const object, then you need to call const functions on it.
Read this

Quote:
4. In the Rock implementation file, in the testCollision function, the error says, right under the player.returnCollisions(heliCollisions): invalid use of undefined type 'const struct Helicopter'

I think this is probably the same issue as 3.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

This topic is closed to new replies.

Advertisement