2012-07-09 48 views
0

我正在單聲道android遊戲應用程序。我想要背景圖像從上到下垂直滾動的示例代碼。我有一個代碼,但它不能正常工作。因此,有人幫助我。單聲道爲Android應用程序

mBGFarMoveY = mBGFarMoveY + 3; 
    int newFarY = mBackgroundImageFar.Height + (+ mBGFarMoveY); 
    if (newFarY <= 0) 
    { 
    mBGFarMoveY = 0; 
    canvas.DrawBitmap (mBackgroundImageFar,0,mBGFarMoveY,null); 
    } 
    else 
    { 
    canvas.DrawBitmap (mBackgroundImageFar,0,mBGFarMoveY,null); 
    canvas.DrawBitmap (mBackgroundImageFar,0, newFarY, null); 
    } 

感謝&方面的, Chakradhar。

回答

0

你看到了什麼,你期望什麼?就我所見,代碼有幾個問題。

  1. 該位置不是基於時間計算,所以滾動將 跳動。
  2. 重疊代碼看起來不太好,並且出現了很多超出範圍的情況。我不確定 什麼是'canvas',但是如果它是android.graphics中的那個,那麼您的 可以指定源矩形和目標矩形,而不僅僅是'y'位置而是 。

所以像(未經測試,我不寫代碼之前這個平臺,但你應該明白我的意思):

y = (time_seconds * pixels_per_second); 
y = y % image.Height; // wrap 
src_rect.left = 0; 
src_rect.right = image.Width - 1; 
src_rect.top = y; 
src_rect.bottom = image.Height - 1; 

dst_rect.left = 0; 
dst_rect.right = image.Width - 1; 
dst_rect.top = 0; 
dst_rect.bottom = image.Height - 1; 

if (y == 0) { 
    canvas.DrawBitmap(image, src_rect, dst_rect, null); 
} 
else { 
    dst_rect.bottom = src_rect.height() - 1; 
    canvas.DrawBitmap(image, src_rect, dst_rect, null); 

    src_rect.top = 0; 
    src_rect.bottom = y - 1; 
    dst_rect.top = dst_rect.bottom + 1; 
    dst_rect.bottom = image.Height - 1; 

    canvas.DrawBitmap(image, src_rect, dst_rect, null); 
} 
相關問題