2014-02-13 5 views
1

오류가 있습니다.벡터 첨자가 범위를 벗어남 - 업데이트 방법

"벡터 아래 첨자가 932 줄에 있습니다."

나는 두 개의 적 유형을 가지고 있는데, 소행성의 배열과 그 위에 하나의 적을 가지고있다. 소행성의 수가 여러 개인 경우를 제외하고는 정확히 동일하게 만들어졌으며 Borg는 자체적으로 소유하고 있습니다. Borg의 업데이트 방법으로 오류를 추적했습니다. Borg의 업데이트 방법은 Asteroid 메서드와 동일하며 Borg의 일부 이름이 변경되었습니다. "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

관련 문제