Abirafdi Raditya Putra
Abirafdi Raditya Putra

Reputation: 997

Some of the member variables are not accessible

I'm trying to make a simple game, basically there is a map that you can put walls on, after that the enemy will spawn and walk to the finish tile.

Here's the call stack

Game constructor (setscene etc, create buildwall object that handles the wall placement) -> BuildWall (inputs are handled by this object, after user pressed Enter, call the game.generatePath) -> game.generatePath(here's where some of the member variables are not accessible)

Here is the code:

Game.h

#ifndef GAME
#define GAME

#include "Wall.h"
#include "Tile.h"
#include "Enemy.h"
#include "Path.h"
#include <QGraphicsView>
#include "Tower.h"

class BuildWall;

class Game: public QGraphicsView{
    Q_OBJECT
public:
    Game();

    QGraphicsScene * scene;
    Wall * build_wall;

    // map and tile details
    int map_width_in_tiles; // number of tiles in x axis
    int map_height_in_tiles;
    int map_tile_size; // in pixels

    // tiles are indexed in int for easy sorting in QMap. We must use indexOfPoint(x,y) to get an index of a tile
    QMap<int,Tile*> tiles;

    // spawning entities
    void spawnBlueSlime(Tile &spawn, Tile &dest, Path &path);

    int getTileSize();
    QMap<int,Tile*> getTiles();

    void generatePath();

    // tile indexing
    int indexOfPoint(int x, int y);

    // convert tile coordinate to scene coordinate
    int x_scene(int x);
    int y_scene(int y);

    Tower *tower;
    void mouseMoveEvent(QMouseEvent *event);

private:

    // game initializations
    void createMapTiles(QString filename);
    void createScene();
    void setMapTile(int map_width_in_tiles, int map_height_in_tiles, int map_tile_size_);

    // debugging
    void drawTilesOverlay(QString filename);
    void drawTilesPoint();

    //
    void printAllTiles();

    // mouse input


    // spawn dest
    Tile *spawn1;
    Tile *dest1;
    Tile *spawn2;
    Tile *dest2;

    BuildWall *buildwall;

};

#endif // GAME

Game.cpp

    Game::Game()
    {
        setMouseTracking(true);
        grabMouse();
        setMapTile(20,10,64); // but start from 0
        createScene();
        createMapTiles(":/floor/assets/floor/dirt.png");
        //drawTilesOverlay(":/util/assets/util/sTrackBorder_0.png");
        //drawTilesPoint();
        //printAllTiles();

        int index = indexOfPoint(19,4);
        Tower *tower = new Tower(*this,this->tiles.value(index)->x(),this->tiles.value(index)->y());

        BuildWall *buildwall = new BuildWall(*this);
    }

    void Game::generatePath() {
        delete buildwall;
        Tile *spawn1 = tiles.value(indexOfPoint(1,5));
        Tile *dest1 = tiles.value(indexOfPoint(19,5));
        Path *path = new Path(*this,*spawn1,*dest1);
        spawnBlueSlime(*spawn1,*dest1, *path);
    }
 //
 // others methods

BuildWall.h

    class Game;

    class BuildWall: public QGraphicsPixmapItem{
    public:
        BuildWall(Game &game_);
        Game &game;

    private:
        void keyPressEvent(QKeyEvent *ev);

    };

BuildWall.cpp

BuildWall::BuildWall(Game &game_) : game(game_)
{
    setPixmap(QPixmap(":/wall/assets/wall/brick_red.png"));
    setScale(0.5);
    game.scene->addItem(this);
    this->setFlag(QGraphicsItem::ItemIsFocusable, true);
    this->setFocus();
}

void BuildWall::keyPressEvent(QKeyEvent *ev)
{
    int grid = game.map_tile_size;
    switch (ev->key())
    {
        //other keys

        case Qt::Key_Return:
            this->setFlag(QGraphicsItem::ItemIsFocusable, false);
            this->setFocus();
            game.generatePath();
            break;
    }
}

Here you can see I can access some of the member variables such as tiles which are generated in Game constructor

enter image description here

Upvotes: 0

Views: 1115

Answers (1)

MisterC
MisterC

Reputation: 249

You are shadowing member variables.

In the constructor it should only say:

buildwall = new BuildWall(*this);

This is the case for all member variables (spawn1, dest1, spawn2, dest2, buildwall) and in all functions where you want to call these variables.

Upvotes: 2

Related Questions