2014-02-13 41 views
1

我出現錯誤。向量下標超出範圍 - 更新方法

「Vector下標超出範圍,第932行。」

我有兩種敵人類型,一組小行星,然後是它自己的一個單一的敵人。除了小行星的數量是多重的以外,它們是完全一樣的,博格是獨立的。我已經將錯誤追蹤到了Borg的更新方法,這與Borg的一些名稱更改的Asteroids方法完全相同。我已經嘗試將「for」改爲「if」,並試圖將矢量數組更改爲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++; 
      } 
     } 
}; 

ENDIF

感謝您的幫助,對不起,如果我錯過了什麼。

+2

哪條線是932線? – Ghost

回答

1

很難與所提供的信息說,但 你的問題可能是在這裏:

在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你會更少的元素得到這樣的錯誤。

+0

我已經更新了代碼,每個班級都有完整的代碼。抱歉,模糊不清。 – Muffin

+1

管理修復它,非常感謝。那正是錯誤。祝你有美好的一天。 :) – Muffin