Cześć! Szukam pomocy przy pisaniu gry. Mam problem z utworzeniem tablicy pocisków, dla jednego, wszystko działa jak powinno. Próbowałem dojść do tego na różne sposoby ale już nie mam pomysłu
. Dodaje fragmenty kodu klas pocisku oraz gracza:
Bullet.h
#ifndef _BULLET_H
#define _BULLET_H
class Bullet
{
private:
SDL_Texture* bulletTexture; // Tekstura pocisku
Game* game;
public:
Bullet(Game& game); // Konstruktor pocisku
bool loadFromFile(std::string); // Ladowanie tekstury z podanej sciezki
int GetBulletWidth() const; // Zwraca szerowkosc pocisku
int GetBulletHeight() const; // Zwraca wysokosc pocisku
bool GetIsAlive(); // Zwraca czy pocisk jest uzywany
void Update(); // Odswiezanie pozycji pocisku
void Fire(int StartPosX, int StartPosY, int XSpeed, int YSpeed); // Strzal pocisku
void Render(); // Wyswietlanie pocisku na ekranie
private:
bool IsAlive; // Czy pocisk jest uzywany
int mPosbX; // Pozycja X pocisku
int mPosbY; // Pozycja Y pocisku
int mVelbX; // Predkosc pocisku na osi X
int mVelbY; // Predkosc pocisku na osi Y
int bullet_w; // Szerokosc pocisku
int bullet_h; // Wysokosc pocisku
};
#endif
Bullet.cpp
Bullet::Bullet(Game& game)
{
this->game = &game;
std::string path = "img/bullet.png";
IsAlive = false;
mPosbX = 0;
mPosbY = 0;
mVelbX = 0;
mVelbY = 0;
loadFromFile(path.c_str());
}
...
void Bullet::Update()
{
if (IsAlive)
{
mPosbX = mPosbX + mVelbX;
mPosbY = mPosbY - mVelbY;
}
if (mPosbY < 0)
{
IsAlive = false;
}
}
void Bullet::Fire(int StartPosX, int StartPosY, int XSpeed, int YSpeed)
{
mPosbX = StartPosX;
mPosbY = StartPosY;
mVelbX = XSpeed;
mVelbY = YSpeed;
IsAlive = true;
}
void Bullet::Render()
{
if (IsAlive)
{
SDL_Rect renderQuad = { mPosbX, mPosbY, bullet_w, bullet_h };
SDL_RenderCopy(game->renderer, bulletTexture, NULL, &renderQuad);
}
}
...
Player.h
#ifndef _PLAYER_H
#define _PLAYER_H
class Player
{
private:
SDL_Texture* playerTexture; // Tekstura gracza
Game* game;
public:
int getPlayerWidth() const; // Zwraca szerokosc tekstury
int getPlayerHeight() const; // Zwraca wysokosc tekstury
int player_w; // Szerokosc tekstury
int player_h; // Wysokosc tekstury
int GetPosX(); // Zwraca pozycje X gracza
int GetPosY(); // Zwraca pozycje Y gracza
Player(Game&, std::string); // Konstruktor gracza
void HandleEvent(SDL_Event& e); // Odpowiada za uzycie klawiszy
void move(); // Zmiana wspolrzednych gracza
bool loadFromFile(std::string); // Laduje obrazek z podanej sciezki
void render(); // Wyswietla na ekranie
static const int MAX_BULLETS = 10;
Bullet* mBullets[MAX_BULLETS];
~Player(); // Destruktor
private:
int mPospX, mPospY; // Wspolrzedne gracza
int mVelpX, mVelpY; // Predkosc gracza
};
#endif
Player.cpp
Player::Player(Game& game, std::string path)
{
...
for (int i = 0; i < MAX_BULLETS;i++)
{
mBullets[i] = new Bullet(game);
}
loadFromFile(path.c_str());
}
void Player::HandleEvent(SDL_Event& e)
{
if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
switch (e.key.keysym.sym)
{
...
case SDLK_SPACE:
for (int i = 0; i < MAX_BULLETS;i++)
{
mBullets[i]->Fire(GetPosX() + getPlayerWidth() / 2 - mBullets[i]->GetBulletWidth / 2, GetPosY() - getPlayerHeight() / 2 + mBullets[i]->GetBulletHeight(), 0, 7);
}
break;
}
}
...
void Player::move()
{
...
for (int i = 0; i < MAX_BULLETS;i++)
{
mBullets[i]->Update();
}
}
...
void Player::render()
{
SDL_Rect renderQuad = { mPospX, mPospY, player_w, player_h };
SDL_RenderCopy(game->renderer, playerTexture, NULL, &renderQuad);
for (int i = 0; i < MAX_BULLETS;i++)
{
mBullets[i]->Render();
}
}
Problem, wyskakujący przy próbie kompilacji:
Error C2065 'mBullets': undeclared identifier- dla każdej próby użycia tablicy.
Pozdrawiam