Optimization
in
Android Processing
•
2 years ago
What tips can you give me on code optimization for android.
I have been checking the memory usage of many games that are built with eclipse and they usually use from 15 to 30 mb of ram.
My game witch doesn't seems as complex as most uses 45mb and can ramp up to 80 and you can make it crash by repetitively jumping from level to level.
Im gonna give you some examples of code, that I think are giving me some performance issues.
- void loadCoordinates() {
- println("loaded"+""+millis());
- try {
- String[]strokesdone = loadStrings("\\turtle\\ownStrokes.txt");
- for (n=0; n<nlvl;n++) {
- ownstrokes[n] = int(strokesdone[n]);
- if (ownstrokes[n]==0) {
- ownstrokes[n]=int(10);
- }
- }
- }
- catch (NullPointerException e) {
- String[] strokesdone = new String[nlvl];
- for (n=0; n<nlvl;n++) {
- strokesdone[n] = 10+"";
- }
- saveStrings("\\turtle\\ownStrokes.txt", strokesdone);
- }
- nMax=cX.length;
- for (n=0; n<nMax;n++) {
- hitX[n] = width/float(cX[n]);
- if (float(cX[n])==0) {
- hitX[n]=0;
- }
- hitY[n] = height/float(cY[n]);
- if (float(cY[n])==0) {
- hitY[n]=0;
- }
- }
- }
that function is played at the start of most levels to reset coordinates for hitbox's and my memory usage ramps up at every new level.
- void level26() {
- if (load==0) {
- nulify();
- loadCoordinates();
- die=0;
- drag=0.98;
- tinting=255;
- H=height/24;
- W=width/16;
- posX=width/1.4;
- posY=height/1.4;
- dX=0;
- dY=0;
- glue=0;
- bounce=0;
- movement=2;
- }
- load=1;
- image(level[lvl], 0, 0, width, height);
- whirlCount=(whirlCount+0.2)%4;
- image(whirl[int(whirlCount)], hitX[710], hitY[710], width/10, height/15);
- //game maker stuff
- if (nMax>0) {
- hitX[nMax]=abs(hitX[nMax-1]-mouseX);
- hitY[nMax]=abs(hitY[nMax-1]-mouseY);
- }
- character();
- image(levelForeground[lvl], 0, 0, width, height);
- ui();
- //stuff to delete later
- if (edit==1) {
- for (n=682;n<711;n+=2) {
- stroke(100);
- fill(255, 80);
- rect(hitX[n], hitY[n], hitX[n+1], hitY[n+1]);
- fill(0);
- text("square "+(n/2+1), hitX[n]+hitX[n+1]/2, hitY[n]+hitY[n+1]/2);
- text("n "+n, hitX[n]+hitX[n+1]/2, hitY[n]+hitY[n+1]/2+10);
- }
- }
- //stuff to delete later**
- if (posX<W) {
- posX=W;
- dX*=-stone;
- }
- if (posY<H) {
- posY=H;
- dY*=-stone;
- }
- for (n=682; n<712;n+=2) {
- if (colision2(posX, posY, H, W, hitX[n], hitY[n], hitX[n+1], hitY[n+1])==true) {
- fill(255);
- ////////////////////bumpers//////////////////////
- if (n==696||n==698||n==700||n==702||n==704||n==706||n==708) {
- if (bump==0) {
- dX=(width/32)*random(-1, 1);
- dY=(height/48)*random(-1, 1);
- bump=n;
- break;
- }
- }
- //speeed!!!!!!!!!!!!
- if (n==690||n==692||n==694) {
- dY++;
- break;
- }
- ////////////// PIT
- if (n==684) {
- dX=(hitX[n]+(hitX[n+1]/2)-posX)/50;
- dY=(hitY[n]+(hitY[n+1]/2)-posY)/50;
- die();
- glue=0;
- break;
- }
- // //glue tiles
- if (n==634||n==632) {
- glue=n;
- stone=1;
- }
- if (dY>0&&posY<hitY[n]) {
- posY-=dY;
- dY*=-stone;
- }
- if (dY<0&&posY>hitY[n]+hitY[n+1]) {
- posY-=dY;
- dY*=-stone;
- }
- if (dX>0&&posX<hitX[n]) {
- posX-=dX;
- dX*=-stone;
- }
- if (dX<0&&posX>hitX[n]+hitX[n+1] ) {
- posX-=dX;
- dX*=-stone;
- }
- // // bouncy tile
- // if(n==38){
- // dX*=bounce;
- // dY*=bounce;
- // }
- // win tile
- if (n==710) {
- //////////////
- if (nstroke<ownstrokes[lvl]&&ownstrokes[lvl]!=0) {
- ownstrokes[lvl]=nstroke;
- String[] strokesdone = new String[nlvl];
- for (n=0; n<nlvl;n++) {
- strokesdone[n] = ownstrokes[n]+"";
- }
- saveStrings("\\turtle\\ownStrokes.txt", strokesdone);
- }
- nstroke=0;
- /////////////
- lvlcomplete[lvl]=1;
- lvl++;
- if (lvlcomplete[lvl]!=1) {
- lvlcomplete[lvl]=2;
- }
- for (n=0; n<nlvl;n++) {
- leveldone[n] = lvlcomplete[n]+"";
- }
- saveStrings("\\turtle\\setup.txt", leveldone);
- load=0;
- }
- }
- stone=0.8;
- }
- //bumper sistem
- for (n=682; n<710;n+=2) {
- if (colision2(posX, posY, H, W, hitX[n], hitY[n], hitX[n+1], hitY[n+1])==false&&bump==n) {
- bump=0;
- }
- }
- //glue system
- for (n=614; n<640;n+=2) {
- if (colision2(posX, posY, H, W, hitX[n], hitY[n], hitX[n+1], hitY[n+1])==false&&glue==n) {
- dX=0;
- dY=0;
- }
- }
- }
Thats a example of how I do my levels and colision detection.
- void imagetool() {
- if (lvl==-2) {
- if (menuloaded==false) {
- menuloaded=true;
- for (n=0;n<9;n++) {
- menu[n]=loadImage("menu"+(n)+".png");
- }
- }
- }
- else {
- for (n=0;n<9;n++) {
- menuloaded=false;
- menu[n].delete();
- }
- }
- if (lastlvl!=lvl&&lvl!=-2&&lvl!=-1) {
- level[lvl]=loadImage("lvl"+(lvl+1)+".png");
- }
- if (lastlvl!=lvl&&lvl!=-2&&lvl!=-1&&lvl!=48) {
- levelForeground[lvl]=loadImage("lvl"+(lvl+1)+"Foreground.png");
- }
- if (lvl!=lastlvl) {
- for (n=0;n<level.length;n++) {
- if (n!=lvl&&level[n]!=null) {
- level[n].delete();
- }
- }
- for (n=0;n<levelForeground.length;n++) {
- if (n!=lvl&&levelForeground[n]!=null) {
- levelForeground[n].delete();
- }
- }
- }
- }
Thats how I handle images and image deletion to free up ram.
Please have a look at this samples and hopefully someone with a more experienced eye than I will find some obvious flaw with a piece of the code.
Thank you in advance.
PS , all my images are PNG, do you think thats the best file type ?
2