【问题标题】:Some of the member variables are not accessible某些成员变量不可访问
【发布时间】:2015-10-25 09:15:28
【问题描述】:

我正在尝试制作一个简单的游戏,基本上有一个可以放置墙壁的地图,然后敌人会生成并走到终点。

这是调用堆栈

游戏构造函数(setscene 等,创建处理墙放置的 buildwall 对象)-> 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;
    }
}

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

【问题讨论】:

  • 你的问题是什么?!
  • 它在标题中,我期待所有成员变量都可以访问,但正如 MisterC 指出的那样,我所做的是隐藏它们。如果不够清楚,请见谅

标签: c++ qt qt-creator


【解决方案1】:

您正在隐藏成员变量。

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

buildwall = new BuildWall(*this);

这适用于所有成员变量(spawn1、dest1、spawn2、dest2、buildwall)以及您要调用这些变量的所有函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多