Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid very basic game development
    primarykey
    data
    text
    <p>Can anyone help me with this simple code? Why doesn't the circle move smoothly? What's wrong with it?</p> <pre><code>package chaseme; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.Window; public class ChaseMe extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(new SampleView(this)); } private class SampleView extends SurfaceView implements SurfaceHolder.Callback, Runnable { private Point point; private Thread thread; private SurfaceHolder holder; private Paint _rect; private Paint _circle; private boolean running; private int WIDTH; private int HEIGHT; private float radius = 20; public SampleView(Context context) { super(context); point = new Point(20, 20); _rect = new Paint(); _rect.setColor(Color.BLACK); _circle = new Paint(); _circle.setColor(Color.BLUE); holder = this.getHolder(); holder.addCallback(this); thread = new Thread(this); } private void updateModel() { if(point.x &gt; WIDTH - radius) { point.x = 20; } if(point.y &gt; HEIGHT - radius) { point.y = 20; } point.x++; point.y++; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} @Override public void surfaceCreated(SurfaceHolder holder) { WIDTH = getWidth(); HEIGHT = getHeight(); this.setRunning(true); thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; this.setRunning(false); while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) {} } } private void setRunning(boolean b) { running = b; } @Override public void run() { while(running) { Canvas c = null; updateModel(); try { c = holder.lockCanvas(null); // synchronized (holder) { render(c); // } } catch(Exception e) { Log.e("main", e.getMessage()); } finally { if(c!=null) { holder.unlockCanvasAndPost(c); } } } } private void render(Canvas c) { c.drawRect(0, 0, WIDTH, HEIGHT, _rect); c.drawCircle(point.x, point.y, radius , _circle); //c.save(); //c.restore(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload