Okay I have two classes:
SWORDBEAM: This one basically handles the projectiles that come out of the player, its pretty simple:
class SWORDBEAM
{
public:
int x, y;
int alive;
int dir;
int xspeed, yspeed;
//COnstructor
SWORDBEAM()
{
x = 0;
y = 0;
alive = 0;
dir = 0;
xspeed = 0;
yspeed = 0;
}
//Core Functions
void drawbeam()
{
int n;
int px, py;
px = x;
py = y;
//Is the beam active
if(!alive)
return;
//Draw sword beam sprite
if(dir == UP)
al_draw_bitmap(swordbeamUp, px - mapxoff, py - mapyoff, NULL);
if(dir == DOWN)
al_draw_bitmap(swordbeamDown, px - mapxoff, py - mapyoff, NULL);
if(dir == LEFT)
al_draw_bitmap(swordbeamLeft, px - mapxoff, py - mapyoff, NULL);
if(dir == RIGHT)
al_draw_bitmap(swordbeamRight, px - mapxoff, py - mapyoff, NULL);
}
void movebeam()
{
int px, py;
px = x;
py = y;
//Is the beam active?
if(!alive)
return;
//Move the beam
x += xspeed;
y += yspeed;
px = x;
py = y;
//Stay within the screen
if(px < link->x - (gameBufferWidth/3) || px > link->x + (gameBufferWidth/3) ||
py < link->y - (gameBufferHeight/2) || py > link->y + (gameBufferHeight/2))
{
//If it goes off screen, than kill it
explosionSprite->alive = 1;
alive = 0;
return;
}
}
void fireweapon()
{
//Ready to fire again
if(!alive)
{
alive = 1;
al_play_sample_instance(swordcomboInstance);
//Fire the beam in the direction that link is facing
switch(link->dir)
{
//Up
case 0:
x = link->x + link->width/2;
y = link->y + link->height/2;
xspeed = 0;
yspeed = -swordspeed;
dir = 0;
break;
//Down
case 1:
x = link->x + link->width/2;
y = link->y + link->height/2;
xspeed = 0;
yspeed = swordspeed;
dir = 1;
break;
//Left
case 2:
x = link->x + link->width/2;
y = link->y + link->height/2 - 3;
xspeed = -swordspeed;
yspeed = 0;
dir = 2;
break;
//Right
case 3:
x = link->x + link->width/2;
y = link->y + link->height/2 - 3;
xspeed = swordspeed;
yspeed = 0;
dir = 3;
break;
}
}
}
};
//Declare a swordbeam
SWORDBEAM swordbeam;
My other class is the enemy, It basically moves in a path I set for it:
class OCTOROCK
{
private:
int x, y;
int width, height;
int curframe, maxframe;
int framecount, framedelay;
int dir;
int alive;
int pathx1, pathx2;
int pathy1, pathy2;
int speed;
public:
//Constructor
OCTOROCK()
{
x, y = 0;
width, height = 0;
curframe, maxframe = 0;
framecount, framedelay = 0;
dir = 0;
int speed = 0;
alive = 0;
pathx1, pathx2 = 0;
pathy1, pathy2 = 0;
}
void setUp(int px, int py, int pwidth, int pheight,
int pcurframe, int pmaxframe, int pframecount,
int pframedelay, int pdir, int pspeed, int palive,
int ppathx1, int ppathx2, int ppathy1, int ppathy2)
{
x = px;
y = py;
width = pwidth;
height = pheight;
curframe = pcurframe;
maxframe = pmaxframe;
framecount = pframecount;
framedelay = pframedelay;
dir = pdir;
speed = pspeed;
alive = palive;
pathx1 = ppathx1;
pathx2 = ppathx2;
pathy1 = ppathy1;
pathy2 = ppathy2;
}
//Responsible for updating the octorock
void updateOctorock()
{
if(alive)
{
//Give octorock a path to follow, only dealing with x axis for now
if(x > pathx2)
{
dir = LEFT;
}
else if(x < pathx1)
{
dir = RIGHT;
}
//Move the octorock
if(dir == UP)
{
x += 0;
y -= speed;
}
if(dir == DOWN)
{
x += 0;
y += speed;
}
if(dir == LEFT)
{
x -= speed;
y += 0;
}
if(dir == RIGHT)
{
x += speed;
y += 0;
}
}
}
//Responsible for drawing the octorock;
void drawOctorock()
{
//Is it alive?
if(alive)
{
//South
if(dir == DOWN)
{
if(++framecount > framedelay)
{
framecount = 0;
if(++curframe > 1)
{
curframe = 0;
}
}
}
//North
/*else if(dir == UP)
{
if(++framecount > framedelay)
{
framecount = 0;
if(++curframe > 3)
{
curframe = 2;
}
}
}*/
//Left
else if(dir == LEFT)
{
if(++framecount > framedelay)
{
framecount = 0;
if(++curframe > 5)
{
curframe = 4;
}
}
}
//Right
else if(dir == RIGHT)
{
if(++framecount > framedelay)
{
framecount = 0;
if(++curframe > 5)
{
curframe = 4;
}
}
}
if(dir == RIGHT)
{
al_draw_bitmap_region(octorock_images[curframe], 0, 0,
al_get_bitmap_width(octorock_images[curframe]),
al_get_bitmap_height(octorock_images[curframe]),
x - mapxoff, y - mapyoff, NULL);
}
else
{
al_draw_bitmap_region(octorock_images[curframe], 0, 0,
al_get_bitmap_width(octorock_images[curframe]),
al_get_bitmap_height(octorock_images[curframe]),
x - mapxoff, y - mapyoff, ALLEGRO_FLIP_HORIZONTAL);
}
}
}
//Sees if the octorock got attacked by link
void detectCollision()
{
int playerx;
int playery;
int x1;
int x2;
int y1;
int y2;
if(alive)
{
playerx = link->x;
playery = link->y;
x1 = x;
y1 = y;
x2 = x1 + width;
y2 = y1 + height;
if(inside(playerx, playery, x1, y1, x2, y2))
{
if(link->curframe == 0 || link->curframe == 1 ||
link->curframe == 2 || link->curframe == 3 ||
link->curframe == 4 || link->curframe == 5 ||
link->curframe == 6 || link->curframe == 7)
{
hearts -= 1;
}
else if(link->curframe == 9 || link->curframe == 10 ||
link->curframe == 11 || link->curframe == 12)
{
alive = 0;
}
}
}
}
void octoPosition()
{
cout << "X Pos: " << x << endl;
cout << "Y Pos: " << y << endl;
}
};
OCTOROCK octorock[1];
The problem is that when I shoot my projectiles, my enemy gets shot out along with the projectile sprite. I have no clue why this is happening.
I have console window running keeping track of all postions and it seems like the swordbeam and octorock are sharing variables. If they are separate classes then how so?