提示コードですが。コメント部のコードでアクセスエラーになります。どうすればいいのでしょうか?stage->getMapObject();はマップのベクターなのですがその要素を指定した要素を削除するコードなのですが削除する例外が出てしまいます。どうすればいいのでしょか?
cpp
1 2void Scene::Update() 3{ 4 stage->Update(); 5 player->Update(); 6 7 8 // プレイヤーとブロックの当たり判定 9 for (std::vector<MapObject>::iterator itr = stage->getMapObject().begin(); itr != stage->getMapObject().end(); itr++) 10 { 11 //printf("%d\n",itr->mCol.getSize().x); 12 if (Intersect_Rect_2D(itr->mCol,player->mCol) == true) 13 { 14 if (itr->getObjectType() == ObjectType::Item) 15 { 16 printf("あああ\n"); 17 itr = stage->getMapObject().erase(itr);////////// 18 } 19 20 player->Fix(itr->mCol.getPosition(), itr->getObjectType()); 21 22 23 } 24 } 25 26}
cpp
1#ifndef ___STAGE_HPP_ 2#define ___STAGE_HPP_ 3 4#include "MapObject.hpp" 5#include "Object.hpp" 6#include "../Entry.hpp" 7 8 9#include <iostream> 10 11// 画面のセルの数 12#define SCREEN_WIDTH_CELL ((int)((int)SCREEN_WIDTH / CELL)) 13#define SCREEN_HEIGHT_CELL ((int)((int)SCREEN_HEIGHT / CELL)) 14 15 16class Stage : Actor_2D 17{ 18public: 19 Stage(Entry* e); 20 21 ~Stage(); 22 23 void Update() override; 24 void Draw()override; 25 26 std::vector<MapObject> getMapObject(); //マップオブジェクトを取得 27 28private: 29 30 31 32 std::vector<MapObject> Area; 33 34 35 36}; 37 38#endif; 39
cpp
1#include "../../Header/Game/Stage.hpp" 2 3 4//コンストラクタ 5Stage::Stage(Entry* e) : Actor_2D() 6{ 7 8 std::shared_ptr<TextureData> sprite = LoadTexture("Assets/Block.png"); 9 std::shared_ptr<TextureData> Item_sprite = LoadTexture("Assets/Item.png"); 10 11 12 13// printf("X: %f\n", SCREEN_WIDTH / (float)CELL); 14// printf("Y: %f\n",SCREEN_HEIGHT / (float)CELL); 15 16 17 // ステージ 18 int Map[SCREEN_HEIGHT_CELL][SCREEN_WIDTH_CELL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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 35 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 36 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 37 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 38 }; 39 40 41 42 for (int y = 0; y < SCREEN_HEIGHT_CELL; y++) 43 { 44 for (int x = 0; x < SCREEN_WIDTH_CELL; x++) 45 { 46 47 // ブロック 48 if (Map[y][x] == 1) 49 { 50 Box_Collision_2D col; 51 col.setEnable(true); 52 col.setPosition(glm::ivec2(CELL * x,( -CELL * y))); 53 col.setSize(glm::ivec2(CELL, CELL)); 54 TextureUV uv; 55 uv.start = glm::vec2(0, 0); 56 uv.end = glm::vec2(CELL, CELL); 57 58 Area.push_back(MapObject(e, ObjectType::Brick, sprite, uv, glm::ivec2(CELL * x, (-CELL * y)), col)); 59 60 //レンガ 61 }else if (Map[y][x] == 2) 62 { 63 Box_Collision_2D col; 64 col.setEnable(true); 65 col.setPosition(glm::ivec2(CELL * x, (-CELL * y))); 66 col.setSize(glm::ivec2(CELL, CELL)); 67 TextureUV uv; 68 uv.start = glm::vec2(CELL, CELL); 69 uv.end = glm::vec2(CELL * 2, CELL * 2); 70 71 Area.push_back(MapObject(e, ObjectType::Block, sprite, uv, glm::ivec2(CELL * x, (-CELL * y)), col)); 72 } 73 74 // アイテム 75 else if (Map[y][x] == 3) 76 { 77 78 Box_Collision_2D col; 79 col.setEnable(true); 80 col.setPosition(glm::ivec2(CELL * x, (-CELL * y))); 81 col.setSize(glm::ivec2(CELL, CELL)); 82 TextureUV uv; 83 uv.start = glm::vec2(0, 0); 84 uv.end = glm::vec2(CELL , CELL); 85 86 Area.push_back(MapObject(e, ObjectType::Item, Item_sprite, uv, glm::ivec2(CELL * x, (-CELL * y)), col)); 87 } 88 } 89 } 90} 91 92 93std::vector<MapObject> Stage::getMapObject() 94{ 95 return Area; 96} 97 98 99void Stage::Update() 100{ 101 for (std::vector<MapObject>::iterator itr = Area.begin(); itr != Area.end(); itr++) 102 { 103 itr->Update(); 104 } 105} 106 107void Stage::Draw() 108{ 109 for (std::vector<MapObject>::iterator itr = Area.begin(); itr != Area.end(); itr++) 110 { 111 itr->Draw(); 112 } 113} 114 115//デストラクタ 116Stage::~Stage() 117{ 118 119} 120
回答1件
あなたの回答
tips
プレビュー