canvas - Android game in surfaceview lagg spikes -


guys. i'm playing around making first android game, stumbled problem. framerate seems have random lag spikes. if comment background(s) out framerate gets smoother. i've looked around , can't find solve problems. have feeling has allocating specific amount of time every time draw, don't know how implement such feature. suggestions? btw, tryed hardware ac, anti etc.

this class starts surfaceview :

package com.example.glassrunner;  imports here  public class game extends activity  {    mysurfaceview mysurfaceview; public soundpool spool; private int soundid; int length=0;   /** called when activity first created. */  @override public void oncreate(bundle savedinstancestate)  {     super.oncreate(savedinstancestate);     requestwindowfeature(window.feature_no_title);      this.setvolumecontrolstream(audiomanager.stream_music);           mysurfaceview = new mysurfaceview(this);      setcontentview(mysurfaceview);   }    @override protected void onresume()  {     // todo auto-generated method stub     super.onresume();      mysurfaceview.onresumemysurfaceview();  }  @override protected void onpause()  {     // todo auto-generated method stub     super.onpause();     mysurfaceview.onpausemysurfaceview(); }  @override protected void ondestroy() {     super.ondestroy();     mysurfaceview = null; }    } 

this surfaceview class :

package com.example.glassrunner;  imports here   public class mysurfaceview extends surfaceview implements runnable  { public static boolean gameover = false;  surfaceholder surfaceholder;  thread thread = null;    public integer score=0;   public soundpool spool; private int soundid; int length=0; public static mediaplayer mp;  volatile boolean running = false; int yposition = 450; int xposition = 50;  paint textpaint; long mlasttime;     bitmap background; bitmap background2; bitmap lines; bitmap runsprite; bitmap box;  paint bitmappaint ; paint textpaint2; bitmap scaledbackground ; bitmap scaledbackground2 ; bitmap scaledlines ; bitmap scaledbox; canvas canvas; paint paint; int spx=0; int spy=0; bitmap[][] sprite;    /** variables counter */ int framesamplescollected = 0; int framesampletime = 0; int fps = 0; int speed = 5;  toast gameovertoast;   context context; mediaplayer mmediaplayer;  public mysurfaceview(context context)  {     super(context);     this.context = context;     // todo auto-generated constructor stub     surfaceholder = getholder();     surfaceholder.setformat(pixelformat.rgb_565);       charsequence text = "game over!";     int duration = toast.length_short;     gameovertoast = toast.maketext(context, text, duration);     spool = new soundpool(10, audiomanager.stream_music, 0);     soundid = spool.load(context, r.raw.jump, 1);     mp = mediaplayer.create(context, r.raw.saturdaymorningfunk);          initialization(); }  public void initialization() {       mp.setlooping(true);     mp.start();        options options = new options();         options.insamplesize = 1/4;     options.inpreferredconfig = bitmap.config.rgb_565;        background=bitmapfactory.decoderesource(getresources(),r.drawable.background,options);     lines=bitmapfactory.decoderesource(getresources(),r.drawable.lines);// getting png drawable folder     background2=bitmapfactory.decoderesource(getresources(),r.drawable.background2,options);     runsprite=bitmapfactory.decoderesource(getresources(),r.drawable.runsprite);     box=bitmapfactory.decoderesource(getresources(),r.drawable.box);     bitmappaint = new paint(paint.anti_alias_flag); // tool painting on canvas     bitmappaint.setantialias(true);     bitmappaint.setfilterbitmap(true);        textpaint = new paint();     textpaint.setcolor(color.red);     textpaint.settextsize(32);     textpaint2 = new paint();     textpaint2.setcolor(color.blue);     textpaint2.settextsize(50);        scaledbackground = bitmap.createscaledbitmap(background, 2560, 500, true);     scaledbackground2 = bitmap.createscaledbitmap(background2, 2560, 400, true);     scaledlines = bitmap.createscaledbitmap(lines, 2560, 30, true);     runsprite = bitmap.createscaledbitmap(runsprite, 1400, 1000, true);     scaledbox = bitmap.createscaledbitmap(box, 100, 100, true);       sprite = new bitmap[4][7];           for(int row=0;row<=3;row++)     {          for(int col=0;col<=6;col++)         {             sprite[row][col] = bitmap.createbitmap(runsprite, spx, spy, 200, 250);             spx+=200;         }         spx=0;         spy+=250;          } }  public void onresumemysurfaceview() {     mp.seekto(length);     mp.start();     running = true;     thread = new thread(this);     thread.start();      }  public void onpausemysurfaceview() {     mp.pause();     length=mp.getcurrentposition();     boolean retry = true;     running = false;     while(retry){         try {             thread.join();              retry = false;         } catch (interruptedexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     }   }  public void ondestroymysurfaceview() {      mp.stop();     running = false;     thread = null;     thread.stop();    }   private void fps()   {     long = system.currenttimemillis();      if (mlasttime != 0)      {          //time difference between , last time here         int time = (int) (now - mlasttime);         framesampletime += time;         framesamplescollected++;          //after 10 frames         if (framesamplescollected == 10)          {              //update fps variable             fps = (int) (10000 / framesampletime);              //reset sampletime + frames collected             framesampletime = 0;             framesamplescollected = 0;         }        }      mlasttime = now; } public boolean pressdown = false; public long presstime; public boolean ontouchevent(motionevent event) {      if (event != null)     {          if (event.getaction() == motionevent.action_down)         {   if(yposition == orgpos)         {             spool.play(soundid, 15, 15, 1, 0, 1f);             pressdown = true;             presstime = system.currenttimemillis();         }           }else if (event.getaction() == motionevent.action_up)         {             pressdown = false;         }     }      return true; }  int x=0; int y=100; int x2=0; int y2=20; int row=0; int col=0; int limit = 100; int orgpos = 450; int xbox = 1280; int ybox = 580; random r = new random(); int rbox;  public static string fscore;  boolean ontop = false; long now;  long start; long stop; long time ;  int spriteposition = 0 ; int spritesize;  @override public void run()  {        while(running)     {         canvas = null;           if(surfaceholder.getsurface().isvalid())         {                canvas = surfaceholder.lockcanvas();               fps(); // fps              // update screen parameters             update();                draw();             surfaceholder.unlockcanvasandpost(canvas);          }     } }  public void update() {      if(score<500)     {         speed = 7;     }     else if(score%500 == 0)     {         speed = 7 + (score / 500);     }     if(col==6)     {         row++;         col=0;     }     if(row==4)     {         row=0;      }      score++;     fscore =  score.tostring();      if(x>-1280)     {         x-=speed;     }else if(x<=-1280)     {         x=0;     }      if(x2>-1280)     {         x2-=5;     }else if(x2<=-1280)     {         x2=-0;     }      rbox = r.nextint(999)+1280;      if(xbox > -100)     {         xbox-=speed;     }else if(xbox<=-100)     {         xbox=rbox;     }     if( (xposition + 200 == xbox +40 )&&(yposition + 250 > ybox+20)||( xposition+200<=xbox+70)&&( xposition+200>=xbox+20)&&(yposition + 250 > ybox+30) ) // collision     {         gameovertoast.show();          running = false;            spool.release();         mp.release();         looper.prepare();         intent database = new intent(context, mainhighscore.class);         database.putextra("score", fscore);         context.startactivity(database);           ondestroymysurfaceview();        }           = system.currenttimemillis();     if(( - presstime) <= 600)     {         if(yposition > limit)         {             yposition -= 10;         }     }     ontop = false;      if((now - presstime) >= 600 && (now - presstime) <= 1200)     {         if(!(yposition == orgpos))         {               if(yposition+250 >= ybox && xposition+200>=xbox+70 && xposition <= xbox+40)             {                 ontop=true;                 yposition = 340;             }else             {                 yposition += 10;             }         }       }     if((now - presstime) >= 1200)     {          if(yposition < 450) yposition +=10;         else yposition = 450;      }        } public void draw() {      canvas.drawcolor(color.white);     //canvas.drawbitmap(scaledbackground, x2,y2, bitmappaint);     canvas.drawbitmap(scaledbackground2, x,y, bitmappaint);     canvas.drawbitmap(scaledlines, x,650, bitmappaint);     canvas.drawtext(fscore, 1050, 50, textpaint2);     canvas.drawtext(fps + " fps", getwidth() / 2, getheight() / 2, textpaint);     canvas.drawbitmap(sprite[row][col],xposition,yposition,bitmappaint );     canvas.drawbitmap(scaledbox,xbox,ybox,bitmappaint);      col++; }    } 

i think problem might moving part. drawing stuff, , surfaceview not meant that.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -