gpt4 book ai didi

c++ - 部分成员变量不可访问

转载 作者:行者123 更新时间:2023-11-28 02:18:25 24 4
gpt4 key购买 nike

我正在尝试制作一个简单的游戏,基本上有一张 map ,您可以在上面放墙,之后敌人会生成并走到终点。

调用栈

游戏构造函数(设置场景等,创建处理墙放置的 buildwall 对象)-> BuildWall(输入由该对象处理,在用户按下 Enter 后,调用 game.generatePath)-> game.generatePath(这里是一些成员变量不可访问)

代码如下:

游戏.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

游戏.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;
}
}

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

enter image description here

最佳答案

您正在隐藏成员变量。

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

buildwall = new BuildWall(*this);

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

关于c++ - 部分成员变量不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com