मेरे पास यह स्प्राइट घूर्णन एल्गोरिदम है (इसका खराब नाम और परीक्षण के लिए उपयोग किया जाता है)। यह बहुत करीब है, इसके साथ खींचे गए sprites घूमते हैं। हर फ्रेम मैं इसमें +5 डिग्री जोड़ सकता हूं और अपने अच्छे छोटे स्प्राइट को घूमता देख सकता हूं। समस्या यह है कि कैनवास के लिए खींची गई अन्य चीजें अब झिलमिलाहट हैं। अगर मैं घूर्णन नहीं करता हूं तो नियमित रूप से खींचे गए sprites महान काम करते हैं। मुझे लगता है कि मैं करीब हूं लेकिन मुझे नहीं पता कि मैं किस टुकड़े को याद कर रहा हूं। नीचे मेरे दो "Draw_Sprite" विधियां हैं, एक बस पहले संसाधन लोड किए गए बिटमैप को कैनवास में पास कर लेता है। दूसरा, कुछ घूर्णन करता है जो मुझे पता है कि स्प्राइट को इतनी एक्स द्वारा कितनी बार घुमाने के लिए ... और फिर इसे खींचें । यदि मेरे पास एक अच्छा गेम लूप है जो कई ऑब्जेक्ट खींचता है, तो एक प्रकार घुमावदार प्रकार है। फिर गैर घुमावदार sprites झिलमिलाहट और फिर भी घुमावदार sprite कभी नहीं करता है। अगर मैं गैर घुमाया स्प्राइट पहले आकर्षित, सब कुछ ठीक है हालांकि, लेकिन फिर Z- क्रम में गड़बड़ किया जा सकता है ... विधि परिभाषाओं (UI तत्व आदि की चोटी पर स्प्राइट):Canvas.DrawBitmap का उपयोग कर एंड्रॉइड में स्प्राइट रोटेशन। मैं करीब हूँ, मैं क्या गलत कर रहा हूँ?
/*************************************************
* 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, 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, 0, width, height);
Rect dst = new Rect(x, y, x + width, y + height);
c.drawBitmap(images[curr_frame], src, dst, 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, 0, 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
}
तो यह यह क्या करता है, एक घूर्णी ड्रा के लिए एक कॉल के बाद चीजों को सही ढंग से तैयार कर रहे हैं। लेकिन समस्या यह झिलमिलाहट है। अब कोई कह सकता है कि मुझे बस अपनी सभी घूर्णन वाली चीजें करना चाहिए और आखिरी बचाओ, लेकिन ज़ॉर्डरिंग बंद हो जाएगी .... सुझावों के बारे में सुझाव कि ज़ोरर्डिंग या झिलमिलाहट के इस मुद्दे से कैसे निपटें?
कि पूरी तरह से यह किया ... वाह – Codejoy
बस इस कल में भाग: आप कोड के केवल कुछ ही लाइनों के साथ ऐसा कर सकते हैं। कैनवास में हेरफेर को कैसे संभाला जाता है, इस बारे में पूरी तरह से गलत धारणा थी। खुशी है मैंने सही तरीका सीखा! :) – methodin