java - Android very basic game development -


can me simple code? why doesn't circle move smoothly? what's wrong it?

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 activity 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 > width - radius) {             point.x = 20;         }         if(point.y > 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());             }             {                 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();     } } } 

you can't call point.x++ do. need calculate movement relative elapsed time , screen size.

step 1: @ every frame, calculate how time has passed since last frame doing

long = system.currenttimemillis(); elapsed = (now - mlasttime); totaltimeelapsed += elapsed; 

and @ end of main loop do

mlasttime = now; 

step 2. screen ratio:

screenwidth = myclass.this.getwidth(); screenheight = myclass.this.getheight(); float = screenwidth; float b = screenheight; screenratiox = a/width_of_your_phone; screenratioy = b/heigth_of_your_phone; 

step 3. can start doing animations, instance, if want move circle right left:

spritex1 = (int) ((spritex1 + (velocity*screenratiox*elapsed))+0.5); spritex2 = spritex1 + spritewidth; 

start velocity of 2.0 or , tweak there.

good luck!


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -