某些成员变量无法访问

时间:2015-10-25 09:15:29

标签: c++ qt qt-creator

我试图制作一个简单的游戏,基本上有一张地图,你可以把墙放在上面,之后敌人会产卵并走到终点砖。

这是调用堆栈

游戏构造函数(setscene等,创建处理墙壁放置的构建墙对象) - > BuildWall(输入由此对象处理,用户按Enter后调用game.generatePath) - > game.generatePath(这里是某些成员变量无法访问的地方)

以下是代码:

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;
    }
}

在这里你可以看到我可以访问一些成员变量,例如在游戏构造函数

中生成的tile

enter image description here

1 个答案:

答案 0 :(得分:2)

您正在隐藏成员变量。

在构造函数中,它应该只说:

buildwall = new BuildWall(*this);

所有成员变量(spawn1,dest1,spawn2,dest2,buildwall)以及要调用这些变量的所有函数都是这种情况。

相关问题