我有这个精灵旋转算法(它的命名很差,只是用于测试).它是如此接近,用它绘制的精灵旋转.每一帧我都可以加5度,看看我漂亮的小精灵旋转.问题是,画布上绘制的其他东西现在都在闪烁.如果我不进行旋转,则常规绘制的精灵效果很好.我想我很接近,但我只是不知道我错过了什么.下面是我的两个“Draw_Sprite”方法,一个只是将先前资源加载的位图绘制到传入的画布.另一个,做了一些旋转最好我知道如何旋转精灵这么多x度…然后绘制它.如果我有一个很好的游戏循环来绘制几个对象,一种类型是旋转的类型.然后非旋转的精灵闪烁,但旋转的精灵永远不会闪烁.虽然如果我先绘制非旋转的精灵,一切都很好,但是Z-Ordering可能会混乱(UI元素顶部的精灵等)……方法定义:
/************************************************* * rotated sprite,ignore the whatever,its for ease of use and testing to have this argument list * @param c canvas to draw on. * @param whatever ignore * @param rot degrees to rotate * @return */ public int Draw_Sprite(Canvas c,int whatever,int rot) { //rotating sprite Rect src = new Rect(0,width,height); Rect dst = new Rect(x,y,x + width,y + height); Matrix orig = c.getMatrix(); mMatrix = orig; orig.setTranslate(0,0); orig.postRotate(rot,x+width/2,y+height/2); c.setMatrix(orig); c.drawBitmap(images[curr_frame],src,dst,null); c.setMatrix(mMatrix); //set it back so all things afterwards are displayed correctly. isScaled=false; return 1; } /******************************************************** * draw a regular sprite to canvas c * @param c * @return */ public int Draw_Sprite(Canvas c) { Rect src = new Rect(0,y + height); c.drawBitmap(images[curr_frame],null); isScaled=false; return 1; }
现在用法:
void onDraw(Canvas c) { canvas.drawRect( bgRect,bgPaint); //draw the background //draw all game objects // draw the normal items for (GameEntity graphic : _graphics) { graphic.toScreenCoords((int)player_x,(int)player_y); if(graphic.getType().equals("planet")) //draw planets graphic.Draw_Sprite(canvas); //before the rotation call draws fine else { //rotate all space ships every frame so i see them spinning //test rotation mRot +=5; if(mRot>=360) mRot=0; graphic.Draw_Sprite(canvas,mRot); //yes function name will be better in future. this rotates spins draws fine } } thePlayer.Draw_Sprite(canvas); //FLICKERS drawUI(canvas);//all things here flickr }
所以它确实这样做了,在调用旋转绘图之后的事情被正确绘制.但问题是它是闪烁的.现在有人可以说我应该做我所有的非旋转的东西并保存最后,但是zordering会关闭……关于如何解决这个zordering或闪烁问题的建议?