- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个错误。
“vector 下标超出范围,第 932 行。”
我有两种敌人类型,一组小行星,然后是一个单独的敌人。它们的制作完全相同,只是小行星的数量是多倍的,而博格是独立的。我已将错误追踪到 Borg 的更新方法,该方法与 Asteroids 方法完全相同,但 Borg 的名称有所更改。我尝试将“for”更改为“if”,并且尝试将 vector 数组更改为 D3DXVECTOR3。我认为这是一个数组错误,但我的编程知识有限。以下是更新方法:
asteroidgamestate.h
#ifndef ASTEROIDSGAMESTATE
#define ASTEROIDSGAMESTATE
#include "Game Engine.h"
#include "Game Constants.h"
#include <vector>
class AsteroidsGameState:public GameState
{
private:
// STL vector to hold a collection of asteroid game sprites.
std::vector<GameSprite*> m_pAsteroids;
// STL vector to hold motion vectors for each asteroid.
std::vector<D3DXVECTOR3*> m_vAsteroidMotionVectors;
// STL vector to hold scaling factors for each asteroid.
std::vector<D3DXVECTOR2*> m_vAsteroidRotation;
public:
AsteroidsGameState() { }
~AsteroidsGameState()
{
this->Release();
}
//Initialises Asteroids & Borg cube
virtual bool Init()
{
D3DXVECTOR3 cSpritePosition;
GameSprite* asteroid;
D3DXVECTOR3* motionVector;
D3DXVECTOR2* rotation;
// Set up the asteroids.
for (int i = 0; i < MaximumNumberOfAsteroids / 2; i++)
{
asteroid = new GameSprite();
if (!asteroid->Init(420,425,true,L"asteroid.png"))
return false;
// Set the sprites current position.
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
asteroid->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector
asteroid->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
asteroid->SetAlive();
asteroid->SetVisible();
float scale = ((float)MathsUtilities::Get().GetRandomNumber(100, 1000)/3000.0f);
asteroid->SetScaleMatrix(scale, scale);
asteroid->SetRotationMatrix(0.0f);
this->m_pAsteroids.push_back(asteroid);
motionVector = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
motionVector->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->z = 0.0f;
this->m_vAsteroidMotionVectors.push_back(motionVector);
rotation = new D3DXVECTOR2(0.0f, 0.0f);
rotation->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
this->m_vAsteroidRotation.push_back(rotation);
}
for (int i = MaximumNumberOfAsteroids / 2; i < MaximumNumberOfAsteroids; i++)
{
asteroid = new GameSprite();
if (!asteroid->Init(420,425,true,L"asteroid2.png"))
return false;
// Set the sprites current position.
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
asteroid->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector
asteroid->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
asteroid->SetAlive();
asteroid->SetVisible();
float scale = ((float)MathsUtilities::Get().GetRandomNumber(100, 1000)/3000.0f);
asteroid->SetScaleMatrix(scale, scale);
asteroid->SetRotationMatrix(0.0f);
this->m_pAsteroids.push_back(asteroid);
motionVector = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
motionVector->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->z = 0.0f;
this->m_vAsteroidMotionVectors.push_back(motionVector);
rotation = new D3DXVECTOR2(0.0f, 0.0f);
rotation->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
this->m_vAsteroidRotation.push_back(rotation);
}
//Spawns one Borg
for (int i = 2 / 2; i < 2; i++)
{
asteroid = new GameSprite();
int BorgHealth = 4;
if (!asteroid->Init(420,425,true,L"borgcube.png"))
return false;
// Set the sprites current position.
/*if (BorgHealth < 4)
{
D3DXCOLOR(1.0f,1.0f,0.0f, 1.0f);
}*/
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
asteroid->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector
asteroid->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
asteroid->SetAlive();
asteroid->SetVisible();
float scale = ((float)MathsUtilities::Get().GetRandomNumber(999, 1000)/3000.0f);
asteroid->SetScaleMatrix(scale, scale);
asteroid->SetRotationMatrix(0.0f);
this->m_pAsteroids.push_back(asteroid);
motionVector = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
motionVector->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->z = 0.0f;
this->m_vAsteroidMotionVectors.push_back(motionVector);
rotation = new D3DXVECTOR2(0.0f, 0.0f);
rotation->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
this->m_vAsteroidRotation.push_back(rotation);
}
return true;
}
// Update pposition, rotation of asteroids.
virtual void Update()
{
GameSprite* asteroid;
int i = 0;
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pAsteroids.begin(); it != m_pAsteroids.end(); it++)
{
asteroid = *it;
if (m_vAsteroidRotation[i]->y <= 0)
m_vAsteroidRotation[i]->x -= m_vAsteroidRotation[i]->y;
else
m_vAsteroidRotation[i]->x += m_vAsteroidRotation[i]->y;
asteroid->SetRotationMatrix(m_vAsteroidRotation[i]->x);
i++;
if (i >= MaximumNumberOfAsteroids)
i = 0;
if (asteroid->GetAlive())
{
asteroid->SetTranslationMatrix(*m_vAsteroidMotionVectors[i]);
asteroid->Update();
}
asteroid->CheckBoundary();
}
}
// At this time no action is required on entering the state.
virtual void Enter() { }
// At this time no action is required when leaving the state.
virtual void Exit() { }
// Render asteroids.
virtual void Render()
{
GameSprite* asteroid;
// Render all the asteroids.
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pAsteroids.begin(); it != m_pAsteroids.end(); it++)
{
asteroid = *it;
asteroid->Render();
}
}
// Free allocated resources.
virtual void Release()
{
// Remove Vector classes containing game objects.
this->FreeSTL(m_pAsteroids);
this->FreeSTL(m_vAsteroidMotionVectors);
this->FreeSTL(m_vAsteroidRotation);
}
// Getter functions.
// Get the STL vector to hold a collection of asteroid game sprites.
std::vector<GameSprite*> GetAsteroids() { return this-> m_pAsteroids; }
// Get the STL vector to hold motion vectors for each asteroid.
std::vector<D3DXVECTOR3*> GetAsteroidMotionVectors() { return this->m_vAsteroidMotionVectors; }
// Get the STL vector to hold scaling factors for each asteroid.
std::vector<D3DXVECTOR2*> GetAsteroidRotation() { return this->m_vAsteroidRotation; }
// Private template function to free allocatted resources.
private:
// Template methods to help destroy game objects.
template<typename T>
void FreeSTL(std::vector<T*> &list)
{
std::vector<T*>::iterator it;
it = list.begin();
while(it != list.end())
{
if ((*it) != NULL)
{
delete (*it);
it = list.erase(it);
}
else
it++;
}
list.clear();
}
// New game level requires bringing the asteroids back to life.
void NextLevelOfAsteroids()
{
GameSprite* asteroid;
D3DXVECTOR3 cSpritePosition;
float scale;
int i = 0;
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pAsteroids.begin(); it != m_pAsteroids.end(); it++)
{
asteroid = *it;
// Set the sprites current position.
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
asteroid->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector.
asteroid->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
asteroid->SetAlive();
asteroid->SetVisible();
// Scale the asteroids.
scale = ((float)MathsUtilities::Get().GetRandomNumber(100, 1000)/3000.0f);
asteroid->SetScaleMatrix(scale, scale);
asteroid->SetRotationMatrix(0.0f);
// Set motion vectors for the asteroids.
m_vAsteroidMotionVectors[i]->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
m_vAsteroidMotionVectors[i]->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
// Set up rotation vector for the asteroids.
m_vAsteroidRotation[i]->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
i++;
}
}
};
#endif
borggamestate.h
#pragma once
#ifndef BORGGAMESTATE
#define BORGGAMESTATE
#include "Game Engine.h"
#include "Game Constants.h"
#include <vector>
class BorgGameState:public GameState
{
private:
// STL vector to hold a collection of borg game sprites.
std::vector<GameSprite*> m_pBorg;
// STL vector to hold motion vectors for the borg.
std::vector<D3DXVECTOR3*> m_vBorgMotionVectors;
// STL vector to hold scaling factors for the borg.
std::vector<D3DXVECTOR2*> m_vBorgRotation;
public:
BorgGameState() { }
~BorgGameState()
{
this->Release();
}
//Initialises Borg cube
virtual bool Init()
{
D3DXVECTOR3 cSpritePosition;
GameSprite* borg;
D3DXVECTOR3* motionVector;
D3DXVECTOR2* rotation;
// Set up the borg.
//Spawns one Borg
for (int i = 2 / 2; i < 2; i++)
{
borg = new GameSprite();
int BorgHealth = 4;
if (!borg->Init(420,425,true,L"borgcube.png"))
return false;
// Set the sprites current position.
/*if (BorgHealth < 4)
{
D3DXCOLOR(1.0f,1.0f,0.0f, 1.0f);
}*/
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
borg->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector
borg->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
borg->SetAlive();
borg->SetVisible();
float scale = ((float)MathsUtilities::Get().GetRandomNumber(999, 1000)/3000.0f);
borg->SetScaleMatrix(scale, scale);
borg->SetRotationMatrix(0.0f);
this->m_pBorg.push_back(borg);
motionVector = new D3DXVECTOR3(0.0f, 0.0f, 0.0f);
motionVector->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
motionVector->z = 0.0f;
this->m_vBorgMotionVectors.push_back(motionVector);
rotation = new D3DXVECTOR2(0.0f, 0.0f);
rotation->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
this->m_vBorgRotation.push_back(rotation);
}
return true;
}
// Update position, rotation of borg.
virtual void Update()
{
GameSprite* borg;
int i = 0;
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pBorg.begin(); it != m_pBorg.end(); it++)
{
borg = *it;
if (m_vBorgRotation[i]->y <= 0)
m_vBorgRotation[i]->x -= m_vBorgRotation[i]->y;
else
m_vBorgRotation[i]->x += m_vBorgRotation[i]->y;
borg->SetRotationMatrix(m_vBorgRotation[i]->x);
i++;
if (i >= MaximumNumberOfAsteroids)
i = 0;
if (borg->GetAlive())
{
borg->SetTranslationMatrix(*m_vBorgMotionVectors[i]);
borg->Update();
}
borg->CheckBoundary();
}
}
// At this time no action is required on entering the state.
virtual void Enter() { }
// At this time no action is required when leaving the state.
virtual void Exit() { }
// Render borg.
virtual void Render()
{
GameSprite* borg;
// Render borg.
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pBorg.begin(); it != m_pBorg.end(); it++)
{
borg = *it;
borg->Render();
}
}
// Free allocated resources.
virtual void Release()
{
// Remove Vector classes containing game objects.
this->FreeSTL(m_pBorg);
this->FreeSTL(m_vBorgMotionVectors);
this->FreeSTL(m_vBorgRotation);
}
// Getter functions.
// Get the STL vector to hold a collection of borg game sprites.
std::vector<GameSprite*> GetBorg() { return this-> m_pBorg; }
// Get the STL vector to hold motion vectors for each borg.
std::vector<D3DXVECTOR3*> GetBorgMotionVectors() { return this->m_vBorgMotionVectors; }
// Get the STL vector to hold scaling factors for the borg.
std::vector<D3DXVECTOR2*> GetBorgRotation() { return this->m_vBorgRotation; }
// Private template function to free allocatted resources.
private:
// Template methods to help destroy game objects.
template<typename T>
void FreeSTL(std::vector<T*> &list)
{
std::vector<T*>::iterator it;
it = list.begin();
while(it != list.end())
{
if ((*it) != NULL)
{
delete (*it);
it = list.erase(it);
}
else
it++;
}
list.clear();
}
// New game level requires bringing the borg back to life.
void NextLevelOfBorg()
{
GameSprite* borg;
D3DXVECTOR3 cSpritePosition;
float scale;
int i = 0;
std::vector<GameSprite*>::iterator it;
for (std::vector<GameSprite*>::iterator it = m_pBorg.begin(); it != m_pBorg.end(); it++)
{
borg = *it;
// Set the sprites current position.
cSpritePosition.x = (float)(100 + MathsUtilities::Get().GetRandomNumber(0, Graphics2D::Get().GetWindowWidth() - 100));
cSpritePosition.y = 1.0f;
cSpritePosition.z = 0.9f;
borg->SetSpritePosition(cSpritePosition);
// Set the sprites motion vector.
borg->SetTranslationMatrix(D3DXVECTOR3(1.0f, 1.0f, 0.0f));
borg->SetAlive();
borg->SetVisible();
// Scale the borg.
scale = ((float)MathsUtilities::Get().GetRandomNumber(100, 1000)/3000.0f);
borg->SetScaleMatrix(scale, scale);
borg->SetRotationMatrix(0.0f);
// Set motion vectors for the borg.
m_vBorgMotionVectors[i]->x = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
m_vBorgMotionVectors[i]->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/150.0f) + 0.25f;
// Set up rotation vector for the borg.
m_vBorgRotation[i]->y = (float)(MathsUtilities::Get().GetRandomNumber(-1000, 1000)/10000.0f) + 0.001f;
i++;
}
}
};
感谢您的帮助,如有遗漏,请见谅。
最佳答案
根据提供的信息很难说,但是您的问题可能出在这里:
在 BorgGameState.h 中更新()
borg = *it;
if (m_vBorgRotation[i]->y <= 0)
m_vBorgRotation[i]->x -= m_vBorgRotation[i]->y;
else
m_vBorgRotation[i]->x += m_vBorgRotation[i]->y;
borg->SetRotationMatrix(m_vBorgRotation[i]->x);
i++;
if (i >= MaximumNumberOfAsteroids) // <--- should this be MaximumNumberOfBorg instead?
i = 0;
如果 m_vBorgRotation 的元素少于 MaximumNumberOfAsteroids,您将得到这样的错误。
关于c++ - vector 下标超出范围 - 更新方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759572/
是否可以创建一个可以使用显式参数标签调用的下标? struct MyType { subscript (label: Bool) -> String? { return nil
我正在尝试制作一个包含带有上标和/或下标的文本的超链接。我找到了两种方法来做到这一点,它们都很糟糕。 解决方案#1:使用 Typography.Variants .这为某些字体提供了极好的上标……。
在这里! 我想在 geom_bracket 中包含一个带下标的标签在 ggplot2 .我尝试了不同的方式,但没有人成功(评论中的尝试): library(ggplot2) ggplot(data =
我正在尝试让 graphviz 启动并工作,我迫切需要节点标签中的下标。不幸的是,通过浏览无数关于类似问题的帖子,我似乎适合所有建议的解决方案,但仍然不起作用。这是我的代码: digraph G{
抱歉格式问题,我从来没有真正在这样的论坛上发帖,所以我必须学习一下操作方法。 我的问题是:我正在编写一个模板类,我想通过多种 [] 运算符访问我的容器。我读了一些关于这个主题的内容,所以我已经能够重载
我知道我们可以像在 matplotlib 中生成单个下标 $r_i$ 会给我一个下标为“i”的r。 但我想生成一个包含 3 或 4 个字母的下标,例如 r_ijk 应该给我一个带有“ijk”作为下标的
this[5] 有什么作用?我是否调用了某种未定义的行为?关于: std::vector foo{this, this + 5u}; 这个有用吗?我想知道指针算法对 this 的影响是什么。这是一个测
我从 visual studio 得到了一些奇怪的行为,关于以下代码片段,错误列表显示了 E0349 的几个实例:没有运算符“[]”匹配这些操作数。 Intellisense 似乎暗示类型不匹配,但正
我想为我的数组类提供 PHP 样式的 push_back 功能: arrayT arr; arr[] = 10; // == std::vector::push_back() 和 arrayT::op
下标 (subscripts)可以定义在类(class)、结构体(structure)和枚举(enumeration)中,是访问集合(collection),列表(list)或序列(sequence)
我正在使用traindata训练svm。 (R中的e1071软件包)。以下是有关我的数据的信息。 > str(train) 'data.frame': 891 obs. of 10 variab
#include int main(){ int arr[7] = {0,1,2,3,4,3,2}; arr[0]++[arr]++[arr]++[arr]++[arr]++[arr]
如果我想以特定用户的身份调用主脚本文件中的另一个 shell 脚本,我该怎么做呢?子脚本似乎失去了它正在运行的用户的上下文,我还没有找到任何有用的子脚本技术。 例如:war-install.sh if
这个问题在这里已经有了答案: Why isn't there an operator[] for a std::list? (4 个答案) 关闭 5 年前。 我有这些类型定义: typedef pa
我在 NSUserdefaults 中获取字典的字符串时遇到问题,这是我的代码。我不知道似乎是什么问题: static func getItemInUserDefaultsDictionary(key
我正在尝试执行以下代码并收到错误 Could not find member 'subscript on Xcode6 var b:[[Float]] = [[1,2]] var i2 = 0 //
我尝试运行的代码: std::string genBlankName(std::vector &posts) { std::string baseName = "New Post ";
1 1 A_{3} 2 2 C_{2} 3 3 ^{5}C_{1} 我有一个这样的输入文件要绘制。第三列是该点上的标签( latex 格式)。我如何在绘图上显示这些标签,就像它们在 latex 编译后
我在这里搜索了一段时间,之前的问题/答案部分回答了我的问题。我正在学习 R,来自 Matlab。正如标题所说,我有一个关于情节注释的问题。在 Matlab 中,绘制包含各种数据格式的注释非常简单,我正
我想将一些化学数据放入表格的列中。但在现有表格中,下标显示为普通字符。其中一些显示为问号。我应该怎么做才能解决它? 当我输入这段代码时 SELECT N'H' + NCHAR(0x2082) + N
我是一名优秀的程序员,十分优秀!