我应该如何在我的游戏中添加/加载图像?

时间:2013-07-09 05:24:01

标签: android image textures libgdx

所以,和其他人一样,我一直在使用libgdx开发一款针对android os的游戏。我真的很喜欢使用我的个人艺术(photoshop画笔)而不是像素艺术。好吧,实际上我正在尝试融合2因为我们都知道android没有足够的内存来显示我想要使用的一些艺术。

例如:我有很多背景,512x512彼此相邻,构成整个“第一阶段”,看似多达12个以上的图像,每个图像大约是100k。这对Android很重要,并且显着降低帧速率,低于30 fps,这使我无法在场景中添加更多图像,我将需要来做。

问题:添加“背景”图片和背景Doodads的 方式是什么?

Kb(大小)有多大不应该过去?

我将它们作为Sprite加载而不是纹理,是好还是坏?

背景图片保存为.png 512x512 8bit,是好还是坏什么会更好?

texturePacker是否可以为性能做任何事情,我甚至应该纹理包装这些512x512和更大的文件?

顺便说一句,我没有打包背景图片...

我是否必须使用像素艺术和较小的图像尺寸才能在Android上创建完整的侧边滚轮?

我知道有很多问题,我试图解释我的困境,所以你会理解它作为一个整体...... 将所有这些问题都视为一个问题:

我已经在这个游戏上工作了7个月了,知道我有时候会越过这座桥,我已经做好了,我会说引擎已经接近完成,意思是,地图/游戏编辑器,角色机制/能力,游戏机制(水平障碍/要做的事情等),goomba和boss技能/能力/ AI,故事,前景动画(64x64,64x128图像没问题,即小文件大小),项目,菜单,IT都准备好了使用除了我有多个大背景大小的问题....

请欣赏任何链接,文章,教程......我已经走得足够远,知道如果我做错了,我可能要重做很多STUFF ....

我想要一个20多个小时的侧卷轴,在这个巨大的游戏中,我需要做些什么呢?

4 个答案:

答案 0 :(得分:1)

你的drawblocks函数每次调用时都会调用new textureregion,你每次都在创建一个新对象。您应该只调用new一次来创建对象,并在调用drawblocks时引用该对象。

创建在创建屏幕时调用的某种init函数。

TextureRegion block1 = new TextureRegion(blockTextureRegion);

然后调用drawblocks函数。

case 0: spriteBatch.draw(block1, block.getPosition...

这样你只需要调用一次,而不是每次调用drawblocks。

答案 1 :(得分:0)

您应该阅读此https://code.google.com/p/libgdx/wiki/TexturePacker

使用

batch.begin();
  

你的精灵渲染代码在这里

batch.end();

这将帮助您获得更高的fps

和纹理打包器确实提高了使用

的性能

答案 2 :(得分:0)

下面是未编辑的代码,所以你可以得到一个想法,我使用WVC方法,继承人主要的渲染方法,plaz让我知道你是否想看到这里被调用的方法.. 我知道代码需要进行优化,所以如果有任何重大问题你会看到PLZ告诉我应该怎么做.. ty

public void render() {
 player01_class player01 = world.getPlayer01();

 if ((player01.getPosition().x) > (CAMERA_WIDTH / 2))  {
     cam.position.set((player01.getPosition().x), (cam.position.y), 0);
     //cam.zoom = 3f;
     cam.update();  


 } else if ((player01.getPosition().x) < (CAMERA_WIDTH / 2))  {
     cam.position.set((player01.getPosition().x), (cam.position.y), 0);
     //cam.zoom = 3f;
     cam.update();           
 }

 if ((player01.getPosition().y) > (CAMERA_HEIGHT / 2)) {
     cam.position.set((cam.position.x), (player01.getPosition().y) , 0);
     //cam.zoom = 3f;
     cam.update();  
 } else if ((player01.getPosition().y) < (CAMERA_HEIGHT) / 2) {
     cam.position.set((cam.position.x), (cam.position.y) , 0);
     //cam.zoom = 3f;
     cam.update();  

 }


 clipBounds = new Rectangle(cam.position.x - 10, cam.position.y - 10, cam.viewportWidth, cam.viewportHeight);

 ScissorStack.calculateScissors(cam, spriteBatch.getTransformMatrix(), clipBounds, scissors);
 ScissorStack.pushScissors(scissors);

 spriteBatch.setProjectionMatrix(cam.combined);  
 spriteBatch.begin();


 //bg_pilot.setPosition(player01.getPosition().x / 1.05f - 10, 0);
 //bg_pilot.draw(spriteBatch, 1f);


 pangodavillageBG_x = player01.getPosition().x;

 spriteBatch.draw(new TextureRegion(pangodavillageBG01), pangodavillageBG_x / 1.80f, 2 , 25,  25);   
 spriteBatch.draw(new TextureRegion(pangodavillageBG02), pangodavillageBG_x / 1.80f + 25, 2 , 25,  25);  
 spriteBatch.draw(new TextureRegion(pangodavillageBG03), pangodavillageBG_x / 1.80f + 50, 2 , 25,  25);  
 spriteBatch.draw(new TextureRegion(pangodavillageBG04), pangodavillageBG_x / 1.80f + 75, 2 , 25,  25);  

 spriteBatch.draw(new TextureRegion(pangodavillageFG01), pangodavillageBG_x / 2.00f, 0 , 25,  25);   
 spriteBatch.draw(new TextureRegion(pangodavillageFG02), pangodavillageBG_x / 2.00f + 25, 0 , 25,  25);  
 spriteBatch.draw(new TextureRegion(pangodavillageFG03), pangodavillageBG_x / 2.00f + 50, 0 , 25,  25);  
 spriteBatch.draw(new TextureRegion(pangodavillageFG04), pangodavillageBG_x / 2.00f + 75, 0 , 25,  25);  



// drawClouds();   

 UpdateTime();

 drawBlocks(); 

 //if (controller.edit_toggle)  {
 drawEditBlocks();
 //}

 drawItems(); 

 drawPowerUps();

 drawPlayer01();

 drawBadGuy();

 drawUIItems();



 //stage.act(Gdx.graphics.getDeltaTime());
// stage.draw();


 bmpfont.draw(spriteBatch, "FPS: " +  Gdx.graphics.getFramesPerSecond(),  cam.position.x - 10,  cam.position.y + 7);
 bmpfont.drawMultiLine(spriteBatch, "playerX: " + (int)player01.getPosition().x + " Y: " + (int)player01.getPosition().y, cam.position.x - 10,  cam.position.y + 6, 1f, HAlignment.LEFT); 
// bmpfont.drawMultiLine(spriteBatch, "rx1: " + rectx1 + " ry1: " + recty1,  cam.position.x - 10,  cam.position.y + 5, 1, HAlignment.LEFT);
// bmpfont.drawMultiLine(spriteBatch, "block: " + currentBlock_counter, cam.position.x - 10,  cam.position.y + 5, 1, HAlignment.LEFT);

 //bmpfont.drawMultiLine(spriteBatch, "badx: " + badGuy.getPosition().x + " badguyy: " +  badGuy.getPosition().y,  cam.position.x - 10,  cam.position.y + 4, 1, HAlignment.LEFT);    
 //bmpfont.drawMultiLine(spriteBatch, "cvelocity: " + badGuy.getAcceleration() + " VBGy: " + badGuy.blockCollidedwithBGY , cam.position.x - 10,  cam.position.y + 3, 1f, HAlignment.LEFT); 
 bmpfont.drawMultiLine(spriteBatch, "mouseX: " + mouseX + " Y: " + mouseY  , cam.position.x - 10, cam.position.y + 5, 1f, HAlignment.LEFT); 
 //bmpfont.drawMultiLine(spriteBatch, "mousexX: " + mouseRawX + " Y: " + mouseRawY  , cam.position.x - 10, cam.position.y + 3, 1f, HAlignment.LEFT); 

 bmpfont.drawMultiLine(spriteBatch, "camposX: " + (int)cam.position.x + " camposY: " + (int)cam.position.y, cam.position.x - 10, cam.position.y + 4, 1f, HAlignment.LEFT);

 bmpfont.drawMultiLine(spriteBatch, "H: " + WorldTime_hour + " M: " + WorldTime_min + " S: " + (int)WorldTime_sec, cam.position.x + 6, cam.position.y + 7, 1f, HAlignment.LEFT);

 //bmpfont.drawMultiLine(spriteBatch, "TileNumber: " + mapTile_ID + "  block_collide_bool: " +  map_block_collide_bool, cam.position.x - 10, cam.position.y + 3, 1f, HAlignment.LEFT); 

 //bmpfont.drawMultiLine(spriteBatch, " aY: " + addButtonRect, cam.position.x, cam.position.y + 2, 1f, HAlignment.LEFT);        
// bmpfont.drawMultiLine(spriteBatch, "m: " + rect_mouse + "  a: " + addButtonRect, cam.position.x - 10, cam.position.y + 2, 1f, HAlignment.LEFT);      
 //bmpfont.drawMultiLine(spriteBatch, "mapTileID: " + mapTile_ID, cam.position.x - 10, cam.position.y + 2, 1f, HAlignment.LEFT);        

// bmpfont.drawMultiLine(spriteBatch, "Game Mode: " + edit_toggle , cam.position.x - 10, cam.position.y + 1, 1f, HAlignment.LEFT);      


 spriteBatch.end();

 if (debug) {
     drawDebug();
     //drawCollisionBlocks(); 
    // drawCollisionToggleBlocks();

     drawEditMenu();


}

 spriteBatch.begin();

   //updateParticles();

 spriteBatch.end();

// spriteBatch.setProjectionMatrix(cam.combined);  
 spriteBatch.begin();

 bmpfont.setScale(.03f); 
 bmpfont.setColor(new Color(1,1,1,1));

 bmpfont.draw(spriteBatch, "ADD", cam.position.x - 9.5f, cam.position.y + 9.65f);
 bmpfont.draw(spriteBatch, "DEL", cam.position.x - 7.9f, cam.position.y + 9.65f);

 bmpfont.draw(spriteBatch, "MODE", cam.position.x - 5.4f, cam.position.y + 9.65f);

 bmpfont.draw(spriteBatch, "BLK", cam.position.x - 2.9f, cam.position.y + 9.65f);
 bmpfont.draw(spriteBatch, "ITM", cam.position.x - 1.35f, cam.position.y + 9.65f);
 bmpfont.draw(spriteBatch, "BAD", cam.position.x - -0.1f, cam.position.y + 9.65f);

 spriteBatch.end();

 spriteBatch.flush();
 ScissorStack.popScissors();


 bmpfont.setColor(new Color(1,1,1,1));
 bmpfont.setScale(.04f); 

}

继承了drawBlocks方法:

  private void drawBlocks()  {

   player01_class player01 = world.getPlayer01();


   displayBlock_x[3] = player01.getPosition().x;
   displayBlock_y[3] = cam.position.y + 8;


   for (Block block : world.getBlocks()) {

 //spriteBatch.draw(blockTextureRegion, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);

   switch (block.blockID)

    {

    case 0:
          spriteBatch.draw(new TextureRegion(blockTextureRegion), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 1:
          spriteBatch.draw(new TextureRegion(blockTextureRegion_metal), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 2:
          spriteBatch.draw(new TextureRegion(btr_basket), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 3:
          spriteBatch.draw(new TextureRegion( btr_sand ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;
    case 4:
          spriteBatch.draw(new TextureRegion( btr_stone1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 5:
          spriteBatch.draw(new TextureRegion( btr_brick ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 6:
          spriteBatch.draw(new TextureRegion( btr_jewel ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 7:
          spriteBatch.draw(new TextureRegion(btr_waterlike), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 8:
          spriteBatch.draw(new TextureRegion(btr_dirtfull1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 9:
          spriteBatch.draw(new TextureRegion( btr_dirtleftside1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 10:
          spriteBatch.draw(new TextureRegion(btr_dirtrightside1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 11:
          spriteBatch.draw(new TextureRegion( btr_dirttop1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 12:
          spriteBatch.draw(new TextureRegion( btr_dirt_leftgrassend1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 13:
          spriteBatch.draw(new TextureRegion( btr_dirt_rightgrassend1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;
    case 14:
          spriteBatch.draw(new TextureRegion( btr_grass1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 15:
          spriteBatch.draw(new TextureRegion(btr_grass_leftend1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 16:
          spriteBatch.draw(new TextureRegion( btr_grass_rightend1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 17:
          spriteBatch.draw(new TextureRegion(btr_grasspatch1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 18:
          spriteBatch.draw(new TextureRegion(sand_full_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());               
    break;

    case 19:
          spriteBatch.draw(new TextureRegion(marble_white_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 20:
          spriteBatch.draw(new TextureRegion(marble_darkwhite_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 21:
          spriteBatch.draw(new TextureRegion(stone_darkgrey_2), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 22:
          spriteBatch.draw(new TextureRegion( stone_darkgrey_3 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;
    case 23:
          spriteBatch.draw(new TextureRegion( stone_grey_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 24:
          spriteBatch.draw(new TextureRegion( stone_grey_2 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 25:
          spriteBatch.draw(new TextureRegion( stoneinset_grey_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 26:
          spriteBatch.draw(new TextureRegion(stoneinset_darkgrey_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 27:
          spriteBatch.draw(new TextureRegion(stoneoutset_grey_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 28:
          spriteBatch.draw(new TextureRegion( stoneoutset_darkgrey_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 29:
          spriteBatch.draw(new TextureRegion(stone_decogrey_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 30:
          spriteBatch.draw(new TextureRegion( stone_decodarkgrey_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 31:
          spriteBatch.draw(new TextureRegion( stone_decogrey_2 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 32:
          spriteBatch.draw(new TextureRegion( stone_decodarkgrey_2 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 33:
          spriteBatch.draw(new TextureRegion( stone_decoblue_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 34:
          spriteBatch.draw(new TextureRegion(stone_decodarkblue_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 35:
          spriteBatch.draw(new TextureRegion( stone_decogreen_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 36:
          spriteBatch.draw(new TextureRegion(stone_decodarkgreen_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 37:
          spriteBatch.draw(new TextureRegion(stone_decoyellow_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());            
    break;

    case 38:
          spriteBatch.draw(new TextureRegion( stone_decodarkyellow_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 39:
          spriteBatch.draw(new TextureRegion( stone_decoorange_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 40:
          spriteBatch.draw(new TextureRegion( stone_decodarkorange_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;
    case 41:
          spriteBatch.draw(new TextureRegion( stone_decopurple_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 42:
          spriteBatch.draw(new TextureRegion(stone_decodarkpurple_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 43:
          spriteBatch.draw(new TextureRegion( woodpanel_top_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 44:
          spriteBatch.draw(new TextureRegion(woodpanel_mid_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 45:
          spriteBatch.draw(new TextureRegion(woodpanel_bottom_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   

    break;
    }

  }

      if (currentBlock_counter > 45)  {

   currentBlock_counter = 0;

      }

    }  

并且继承了drawPlayer方法:

private void drawPlayer01()  {
  player01_class player01 = world.getPlayer01();


player01Frame = player01.isFacingLeft() ? playerIdleLeft : playerIdleRight;

if(player01.getState().equals(State.WALKING)) {

    player01Frame = player01.isFacingLeft() ?        walkLeftAnimation.getKeyFrame(player01.getStateTime(), true) : 

    walkRightAnimation.getKeyFrame(player01.getStateTime(), true);

} else if (player01.getState().equals(State.JUMPING)) { 

    if (player01.getVelocity().y > 0) { 
        player01Frame = player01.isFacingLeft() ? player01JumpLeft : player01JumpRight;     
    } else {    
        player01Frame = player01.isFacingLeft() ? player01FallLeft : player01FallRight;     
    }

}  else if (player01.getState().equals(State.ATTACKING)) {  

    player01Frame = player01.isFacingLeft() ? playerAtkLeft : playerAtkRight;

}

//spriteBatch.draw(player01Frame, player01.getPosition().x * ppuX, player01.getPosition().y * ppuY, player01.SIZE * ppuX, player01.SIZE * ppuY);

//Attack 
if (player01.boolWeapon_isStriking)  {
    int a = 0;

    player01Frame = player01.isFacingLeft() ? playerAtkLeft : playerAtkRight;       
    sword01Frame = player01.isFacingLeft() ? sword01L : sword01R;       

    //if(System.currentTimeMillis() - lastPressProcessed > 20) {

        if (!player01.isFacingLeft()) {

            player01.weaponPosition.set(player01.getPosition().x + 1, player01.getPosition().y);

        } else if (player01.isFacingLeft()){

            //player01Frame = player01.isFacingLeft() ? sword01L : sword01R;        
            player01.weaponPosition.set(player01.getPosition().x - 1.5f, player01.getPosition().y);

        }

     //spriteBatch.draw(player01Frame, player01.getPosition().x, player01.getPosition().y, player01.getSizeW() , player01.getSizeH() );

     spriteBatch.draw(new TextureRegion(sword01Frame), player01.weaponPosition.x, player01.weaponPosition.y - 0.00f, player01.getWeaponSizeW() , player01.getWeaponSizeH()); 

}

spriteBatch.draw(player01Frame, player01.getPosition().x, player01.getPosition().y, player01.getSizeW() , player01.getSizeH() );

}

答案 3 :(得分:0)

像之前提到过的人一样..必须删除draw()方法中的任何'new'语句! 这绝对是你问题的原因!

你必须明白每帧都会调用draw方法!因此,如果您多次分配40kb大小的纹理 - 每秒60次,这是对memmory带宽的大量使用!

那里也存在严重的内存泄漏,因为每个帧都会创建一个纹理区域而不会调用.dispose()。

你要去的方法是在类init上创建纹理区域(例如在构造函数中)并将它们存储在私有的TextureRegion属性中,然后只使用它们。

在整个绘制方法中再没有新的关键字!这也适用于新的Color(1,1,1,1)..请改用priv属性并使用alocate一次或使用Color.WHITE ..

你使用的所有字符串都一样!没有+的字符串..也没有.toString()e.t.c。

要偏执,你甚至不应该在draw中声明一个float / int属性..创建一个私有的aNd重用......