我正在尝试制作一个敌人指针的向量。每个指针都应该生成并绘制一个敌人。我想增加使用输入显示在屏幕上的敌人数量。我想从五个敌人开始,每按一次“S”键就增加一个敌人。我遇到了一个问题,我无法分配我想要的敌人数量。所有其他类按预期工作。这是代码:
GameSample.h
#ifndef __GameSample_H__
#define __GameSample_H__
#include <vector>
//#pragma warning(disable : 4200)
class ShaderProgram;
class Mesh;
class GameObject;
class Player;
const int StartEnemies = 5;
class GameSample : public GameCore
{
//int NumEnemies;
protected:
ShaderProgram* m_pShader;
Mesh* m_PlayerMesh;
Mesh* m_EnemyMesh;
Mesh* m_TargetMesh;
GameObject* m_Player;
//GameObject* m_Enemy[StartEnemies];
GameObject* m_Target;
std::vector<GameObject*> m_Enemy;
int NumEnemies;
public:
GameSample(Framework* pFramework);
virtual ~GameSample();
virtual void OnSurfaceChanged(int width, int height);
virtual void LoadContent();
virtual void Update(float deltatime);
virtual void Draw();
virtual void OnEvent(Events& event);
};
#endif //__GameSample_H__
GameSample.cpp
#include "GamePCH.h"
GameSample::GameSample(Framework* pFramework)
: GameCore( pFramework )
{
m_pShader = 0;
m_PlayerMesh = 0;
m_EnemyMesh = 0;
m_TargetMesh = 0;
m_Player = 0;
m_Target = 0;
NumEnemies = StartEnemies;
for (int i = 0; i < NumEnemies; i++)
{
m_Enemy.push_back(i); //Error "'void std::vector<GameObject*,std::allocator<_Ty>>::push_back(_Ty &&)':cannot convert argument 1 from 'int' to 'GameObject*const &'"
m_Enemy[i] = 0;
}
srand(time_t(NULL));
}
GameSample::~GameSample()
{
// Release the VBO.
//glDeleteBuffers( 1, &m_VBO );
delete m_pShader;
delete m_PlayerMesh;
delete m_EnemyMesh;
delete m_TargetMesh;
delete m_Player;
delete m_Target;
for (int i = 0; i < NumEnemies; i++)
{
delete m_Enemy[i];
}
}
void GameSample::OnSurfaceChanged(int width, int height)
{
// Set OpenGL to draw to the entire window.
glViewport( 0, 0, width, height );
}
void GameSample::LoadContent()
{
// Turn on depth buffer testing.
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
// Turn on alpha blending.
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
// Create our shaders.
m_pShader = new ShaderProgram( "Data/Shaders/color.vert", "Data/Shaders/color.frag" );
m_PlayerMesh = new Mesh();
m_PlayerMesh->SetShader(m_pShader);
m_PlayerMesh->GeneratePlayer();
m_EnemyMesh = new Mesh();
m_EnemyMesh->SetShader(m_pShader);
m_EnemyMesh->GenerateEnemy();
m_TargetMesh = new Mesh();
m_TargetMesh->SetShader(m_pShader);
m_TargetMesh->GenerateTarget();
m_Player = new Player(this, m_PlayerMesh);
for (int i = 0; i < NumEnemies; i++)
{
m_Enemy[i] = new Enemy(this, m_EnemyMesh);
}
m_Target = new Target(this, m_TargetMesh);
// Check for errors.
CheckForGLErrors();
}
void GameSample::Update(float deltatime)
{
if( m_pFramework->IsKeyDown( '1' ) )
{
m_pFramework->SetWindowSize( 600, 600 );
}
m_Player->Update(deltatime);
if (m_pFramework->IsKeyDown('S'))
{
NumEnemies++;
for (int i = 0; i < NumEnemies; i++)
{
m_Enemy[i]->Update(deltatime);
}
}
}
void GameSample::Draw()
{
// Setup the values we will clear to, then actually clear the color and depth buffers.
glClearColor( 0.0f, 0.0f, 0.4f, 0.0f ); // (Red, Green, Blue, Alpha) - Dark blue
glClearDepth( 1 ); // 1 is maximum depth
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
m_Player->Draw();
for (int i = 0; i < NumEnemies; i++)
{
m_Enemy[i]->Draw();
}
m_Target->Draw();
// Check for errors.
CheckForGLErrors();
}
答案 0 :(得分:0)
由于m_Enemy
是指针(std::vector<GameObject*>
)的向量,因此您应该在构造函数中执行m_Enemy.push_back(nullptr);
。在Update
函数中,增量NumEnemies
不会向m_Enemy
添加其他元素。您需要将另一个元素推送到m_Enemy,或者m_Enemy.push_back(new GameObject(...));
后面的行NumEnemies++;
。