SDL可以设置多个定时器吗?可以的话是只是通过TimeID来区分吗?
if (Screen_src.x == 0)
{
Trap_Rain Rain;//陷阱
try
{
myRTime = SDL_AddTimer(RINTERVALS, Raining, NULL);
if (myRTime == NULL)
throw SDL_GetError();
}
catch (const char *s)
{
cerr << "myRTime inited failed" << endl << s << endl;
}
}
Uint32 Raining(Uint32 interval, void* A1)
{
Trap_Rain* A = (Trap_Rain*)A1;
A->rRain.y += 5;
if (A->rRain.y > 330){
A->rRain.y = A->RainY;
}
if (A->rRain.x >= HeroX&&A->rRain.x <= HeroX + Hero_Width&&A->rRain.y == HeroY){
if (A->invinsible == 0){
--Health;
cout << Health << endl;
if (Health > 0)
A->invinsible = SDL_GetTicks() + 5000; //若健康值不为0则无敌
else{
cout << "Dead" << endl;
SDL_RemoveTimer(myRTime);
SDL_Quit();
}
}
else if (A->invinsible == SDL_GetTicks())
A->invinsible = 0;
}
else{
SDL_BlitSurface(A->pBack2, &A->rBack2, pBack, &A->rBack2);//局部刷新pBack2
SDL_BlitSurface(A->pRain, NULL, pBack, &A->rCloud);//把雨滴图片放在背景上
SDL_BlitSurface(pBack, NULL, pScreen, &Screen_src);
SDL_Flip(pScreen);
}
return RINTERVALS;
}
上面是我在函数里声明的第二个定时器,但程序一到这一步就停止运行了。
我的目的是想写一个小游戏,有一个陷阱是下雨的。想通过定时器让雨滴下落。同时在这个定时器里判断主角是否触碰雨滴。
雨滴陷阱类是主角类的友元。