20.11.2016 Views

Android App Development ฉบับสมบูรณ์

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

long curTime = System.currentTimeMillis();<br />

if (lastTimeMs < 0) {<br />

lastTimeMs = curTime;<br />

return;<br />

}<br />

long elapsedMs = curTime – lastTimeMs;<br />

lastTimeMs = curTime;<br />

lVx += ((elapsedMs * lAx) / 1000) * pixelsPerMeter;<br />

lVy += ((elapsedMs * lAy) / 1000) * pixelsPerMeter;<br />

lBallX += ((lVx * elapsedMs) / 1000) * pixelsPerMeter;<br />

lBallY += ((lVy * elapsedMs) / 1000) * pixelsPerMeter;<br />

boolean bouncedX = false;<br />

boolean bouncedY = false;<br />

if (lBallY – ballRadius < 0) {<br />

lBallY = ballRadius;<br />

lVy = -lVy * rebound;<br />

bouncedY = true;<br />

} else if (lBallY + ballRadius > lHeight) {<br />

lBallY = lHeight – ballRadius;<br />

lVy = -lVy * rebound;<br />

bouncedY = true;<br />

}<br />

if (bouncedY && Math.abs(lVy) < STOP_BOUNCING_VELOCITY) {<br />

lVy = 0;<br />

bouncedY = false;<br />

}<br />

if (lBallX – ballRadius < 0) {<br />

lBallX = ballRadius;<br />

lVx = -lVx * rebound;<br />

bouncedX = true;<br />

} else if (lBallX + ballRadius > lWidth) {<br />

lBallX = lWidth – ballRadius;<br />

lVx = -lVx * rebound;<br />

bouncedX = true;<br />

}<br />

if (bouncedX && Math.abs(lVx) < STOP_BOUNCING_VELOCITY) {<br />

lVx = 0;<br />

bouncedX = false;<br />

}<br />

synchronized (LOCK) {<br />

ballPixelX = lBallX;<br />

ballPixelY = lBallY;<br />

}<br />

velocityX = lVx;<br />

velocityY = lVy;<br />

276<br />

<strong>Android</strong> <strong>App</strong> <strong>Development</strong> <strong>ฉบับสมบูรณ์</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!