所以我有臭名昭着的白方问题(再次),这次我尽可能地回复参考,但无济于事。我有一个Level
,其中包含GamePieces
的向量,每个GamePiece
对象包含sf::sprite
。这是尝试从GamePiece
绘制精灵的代码:
Engine.h:
#pragma once
#include "stdafx.h"
#include "SFML/Graphics.hpp"
#include "ImageManager.h"
#include "Level.h"
#include "GamePiece.h"
class Engine{
public:
Engine();
~Engine();
void Go();
private:
void ProcessInput();
void UpdateCPU();
void Render();
sf::RenderWindow window;
ImageManager imgr;
Level level;
};
引擎执行绘图的功能:
void Engine::Render(){
window.clear();
//draw wall tiles
for(int i = 0; i < 160; i++){
if(i < 60){
level.GetWallTile().setPosition(0, i * 10);
window.draw(level.GetWallTile());
}
if(i >= 60 && i < 100){
level.GetWallTile().setPosition((i - 60) * 10, 590);
window.draw(level.GetWallTile());
}
if(i >= 100){
level.GetWallTile().setPosition(390, (i - 100) * 10);
window.draw(level.GetWallTile());
}
}
//draw BG tiles
for(int i = 1; i < 39; i++){
for(int j = 0; j < 59; j++){
level.GetBGTile().setPosition(i * 10, j * 10);
window.draw(level.GetBGTile());
}
}
for(int i = 0; i < level.GetGamePieces().size(); i++){
window.draw(level.GetGamePieces()[i].GetPieceSprite());
}
window.display();
}
请注意,wallTile
和bgTile
正在绘制得很好,它会持续给我一个循环白色方块。
以下是相关的功能和类:
//level.h
#pragma once
#include "stdafx.h"
#include "SFML/Graphics.hpp"
#include "ImageManager.h"
#include "GamePiece.h"
class Level{
public:
Level(ImageManager &im);
~Level();
sf::Sprite &GetWallTile();
sf::Sprite &GetBGTile();
void AddPiece();
std::vector<GamePiece> &GetGamePieces();
private:
ImageManager &imgr;
//not sure if vector needed, or one sprite
//std::vector<sf::Sprite> wallTiles_;
sf::Sprite wallTile;
//std::vector<sf::Sprite> bgTiles_;
sf::Sprite bgTile;
std::vector<GamePiece> gamePieces_;
};
//Level.cpp
#include "stdafx.h"
#include "Level.h"
Level::Level(ImageManager &im) : imgr(im){
im.AddResourceDirectory("images/");
wallTile.setTexture(im.GetImage("walltile1.png"));
bgTile.setTexture(im.GetImage("bgtile1.png"));
AddPiece();
}
Level::~Level(){
}
void Level::AddPiece(){
GamePiece piece(imgr);
gamePieces_.push_back(piece);
}
std::vector<GamePiece> &Level::GetGamePieces(){
return gamePieces_;
}
//GamPiece.h
class GamePiece{
public:
GamePiece(ImageManager &im);
~GamePiece();
void SetShape();
sf::Sprite &GetPieceSprite();
private:
ImageManager &imgr;
sf::Sprite pieceShape;
};
//and GamePiece.cpp
#include "stdafx.h"
#include "GamePiece.h"
GamePiece::GamePiece(ImageManager &im) : imgr(im){
SetShape();
}
GamePiece::~GamePiece(){
}
void GamePiece::SetShape(){
//int temp = rand() % 7;
int temp = 0;
switch(temp){
case 0:
pieceShape.setTexture(imgr.GetImage("line.png"));
case 1:
pieceShape.setTexture(imgr.GetImage("llblock.png"));
case 2:
pieceShape.setTexture(imgr.GetImage("rlblock.png"));
case 3:
pieceShape.setTexture(imgr.GetImage("square.png"));
case 4:
pieceShape.setTexture(imgr.GetImage("lsquiggly.png"));
case 5:
pieceShape.setTexture(imgr.GetImage("rsquiggly.png"));
case 6:
pieceShape.setTexture(imgr.GetImage("tblock.png"));
}
}
sf::Sprite &GamePiece::GetPieceSprite(){
return pieceShape;
}
我知道很多代码,但我真的不知道纹理超出范围的地方,假设这是错误的。看一下类似的帖子,这可能是个问题,但我再次需要帮助找到它。
答案 0 :(得分:2)
void Level::AddPiece() {
GamePiece piece(imgr);
gamePieces_.push_back(piece);
piece.SetShape();
}
piece
正在复制到向量中。驻留在向量中的元素与从中复制的对象(绑定到AddPiece()
范围的对象)无关。因此,对piece
采取的任何操作都不会影响插入的元素。
如果您需要访问之前push_back()
编辑的元素,请使用back()
返回对它的引用:
gamePieces_.back().SetShape();
在将其插入向量之前,您也可以调用SetShape()
。