【发布时间】:2015-03-14 20:09:06
【问题描述】:
我尝试创建一个映射,其中键是枚举,值是 SDL_Rect 数组(其中包含)动画的所有坐标。
std::map<Animation, SDL_Rect*> animations;
Animation currAnim;
我不想像我找到的解决方案 here 那样定义固定数量的动画。我能怎么做 ?因为
SDL_Rect right[3];
right[0].x = 264;
right[0].y = 0;
right[0].w = 36;
right[0].h = 64;
...
animations.insert(std::pair<Animation, SDL_Rect*>(Animation::MoveRight, right));
没用。
我试过了,但我知道我的绘图中有另一个问题,我必须找到带有 int(帧)的向量元素:
void Player::Draw(SDL_Renderer * renderer, int frame)
{
std::vector<SDL_Rect>::iterator it = std::find(animator->GetAnimations()[animator->GetCurrAnim()].begin(),
animator->GetAnimations()[animator->GetCurrAnim()].end(), frame);
SDL_RenderCopy(renderer, lTexture->GetTexture(), &*it, &rect);
}
我试过了,但是有一个错误说
'==' There is no operator found which accept type "SDL_Rect"
【问题讨论】:
-
不起作用是什么意思?编译错误(包括错误)、运行时错误(是的,包括错误)等
-
当您只存储指向其中一个的指针时,为什么要说“SDL_Rect 数组”?即便如此,为什么要使用指针?
std::map<Animation, SDL_Rect>或者如果真的是一个数组std::map<Animation, std::vector<SDL_Rect>> -
@LewisBolender
there is an exception and the program crashes好吧,您在我认为不需要它们的地方使用指针。仅此一项就是该程序出现问题的原因之一。可能该数组超出范围,并且地图现在以垃圾数据结束。如果是这种情况,请使用std::vector<SDL_Rect>作为地图的键,而不是SDL_Rect * -
这应该说
std::vector<SDL_Rect>作为地图中的数据。通过使用SDL_Rect *作为数据,您将该映射绑定到该数组。如果该数组超出范围,则您的地图具有无效值。此外,请发布更多代码并解释异常发生在哪一行(而不是说“它不起作用”)。除非发布更多代码,否则没有人可以给你一个明确的答案。
标签: c++ arrays dictionary sdl sdl-2