Hi guys, I''ve been going over this and although I''ve found a lot of info on pointers and classes, I just haven''t nailed my particular problem so thought posting is worth a shot.
I have the following class.
CEnemy.h
#ifndef __CENEMY_H
#define __CENEMY_H
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
class CEnemy
{
private:
public:
GLfloat xloc, yloc, zloc;
CEnemy(void);
~CEnemy(void);
void drawEnemy(void);
void moveEnemy(GLfloat y);
};
#endif
CEnemy.cpp
#include "CEnemy.h"
#include <cstdlib>
CEnemy::CEnemy()
{
xloc = (rand()%750);
yloc = 600.0f;
zloc = 0.0f;
};
CEnemy::~CEnemy()
{};
void CEnemy::moveEnemy(GLfloat y)
{
yloc = yloc - y;
};
void CEnemy::drawEnemy(void)
{
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(xloc+50.0, yloc, -10.0);
glVertex3f(xloc+50.0, yloc + 50.0, -10.0);
glVertex3f(xloc, yloc + 50.0, -10.0);
glVertex3f(xloc, yloc, -10.0);
glEnd();
};
So what this should create a red box at the top of the screen at a random position. (this is for a simple game of dropping boxes where you catch them with a character at the bottom of the screen). Ok, so I''ll be using this class for multiple enemies, so I first tried declaring the class as
CEnemy enemy1;
this worked fine, I was able to access the yloc with enemy1.yloc = *whatever*
so next step was to make a dynamic enemy like so:
CEnemy* enemy1;
in the initialization i create it with
enemy1 = new CEnemy;
...But anything after that like
enemy1->moveEnemy(100);
or even
enemy1->yloc = *whatever*;
both crash the program and I get a windows error (send error report, don''t send). I can leave in the CEnemy* enemy1 and the enemy1 = new CEnemy lines, and nothing will crash, but if I try to use them, boom, it dies.
Do I have to change anything about the class to handle dynamic creation? Any help is surely appreciated!
Thanks for reading.