java - Android studio 2d game fps issue -
i trying develop may first android game in 2d. intend build scratch without use of engine. have manage create following thread:
import android.graphics.canvas; import android.view.surfaceholder; public class mainthread extends thread { private int fps = 30; private double averagefps; private gamepanel gamepanel; private surfaceholder surfaceholder; private boolean running; public static canvas canvas; public mainthread(surfaceholder surfaceholder, gamepanel gamepanel) { super(); this.surfaceholder = surfaceholder; this.gamepanel = gamepanel; } @override public void run() { long starttime; long timemillis; long waittime; long totaltime = 0; int framecount = 0; // how many milliseconds take run through loop long targettime = 1000 / fps; while (running) { starttime = system.nanotime(); canvas = null; // try lock canvas pixel editing try { canvas = this.surfaceholder.lockcanvas(); synchronized (surfaceholder) { // main game loop this.gamepanel.update(); this.gamepanel.draw(canvas); } } catch (exception e) { } { if (canvas != null) { try { surfaceholder.unlockcanvasandpost(canvas); } catch (exception e) { e.printstacktrace(); } } } timemillis = (system.nanotime() - starttime) / 1000000; waittime = targettime - timemillis; try { system.out.println(waittime); this.sleep(waittime); } catch (exception e) { } totaltime += system.nanotime() - starttime; framecount++; if (framecount == fps) { averagefps = 1000 / ((totaltime / framecount) / 1000000); framecount = 0; totaltime = 0; system.out.println(averagefps); } } }
the problem have averagefps keeps dropping when adding stuff game. background works @ 30 on nexus 5 upon adding character , obstacles drops 22-21. there anyway can optimize ?
thank you
update: gamepanel looks this:
import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rect; import android.graphics.typeface; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; import java.util.arraylist; import java.util.random; public class gamepanel extends surfaceview implements surfaceholder.callback { public static final int width = 1920; public static final int height = 1080; public static float movespeed = -12; private random rand = new random(); private mainthread thread; private background bg; private treadmill tm; private player player; private arraylist<box> boxes; private int mindistance = 600; private int score; public gamepanel(context context) { super(context); // add callback service holder intercept events getholder().addcallback(this); // make gamepanel focusable can handle events setfocusable(true); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacedestroyed(surfaceholder holder) { boolean retry = true; while (retry) { try { thread.setrunning(false); thread.join(); retry = false; thread = null; } catch (interruptedexception e) { e.printstacktrace(); } } } @override public void surfacecreated(surfaceholder holder) { // instantiate objects thread = new mainthread(getholder(), this); bg = new background(bitmapfactory.decoderesource(getresources(), r.drawable.background)); tm = new treadmill(bitmapfactory.decoderesource(getresources(), r.drawable.ground)); player = new player(bitmapfactory.decoderesource(getresources(), r.drawable.character)); boxes = new arraylist<box>(); //start game loop thread.setrunning(true); thread.start(); } @override public boolean ontouchevent(motionevent event) { if (event.getaction() == motionevent.action_down) { if (player.jump == false) { player.setvelocity(-14f); player.setjump(true); } return true; } return super.ontouchevent(event); } public void update() { bg.update(); tm.update(); player.update(); if (boxes.size() == 0) { boxes.add(new box(bitmapfactory.decoderesource(getresources(), r.drawable.box), width)); } else if (boxes.get(boxes.size() - 1).getx() < width) { if (width - boxes.get(boxes.size() - 1).getx() < mindistance) { boxes.add(new box(bitmapfactory.decoderesource(getresources(), r.drawable.box), rand.nextint(400 - 50) + width + mindistance)); } } (int = 0; < boxes.size(); i++) { if (boxes.get(i).getx() <= player.getx() && boxes.get(i).getx() >= player.getx() - 12) { score++; movespeed -= 0.2; system.out.println(movespeed); } if (collision(boxes.get(i), player)) { boxes.remove(i); break; } if (boxes.get(i).getx() < -100) { boxes.remove(i); break; } } (int = 0; < boxes.size(); i++) { boxes.get(i).update(); } } @override public void draw(canvas canvas) { final float scalefactorx = getwidth() / (width * 1.f); final float scalefactory = getheight() / (height * 1.f); if (canvas != null) { final int savestate = canvas.save(); canvas.scale(scalefactorx, scalefactory); // draw background bg.draw(canvas); tm.draw(canvas); player.draw(canvas); (box bx : boxes) { bx.draw(canvas); } drawtext(canvas); canvas.restoretocount(savestate); } } public boolean collision(gameobject a, gameobject b) { if (rect.intersects(a.getrectangle(), b.getrectangle())) { return true; } return false; } public void drawtext(canvas canvas) { paint paint = new paint(); paint.setcolor(color.black); paint.settextsize(50); paint.settypeface(typeface.create(typeface.default, typeface.bold)); canvas.drawtext("score: " + (score), 15, height - 20, paint); }
thank far
the problem doesn't seem in main thread activity, followed tutorial fine (except line - system.out.println(waittime); - don't think thats problem cause.) problem in gamepanel. ;)
Comments
Post a Comment