gpt4 book ai didi

java - 如何为 Android 游戏加载位图?

转载 作者:行者123 更新时间:2023-12-02 07:30:05 26 4
gpt4 key购买 nike

我一直在开发一个没有位图或任何东西的游戏,我使用矩形作为对象,并根据其目的更改它们的颜色,例如玩家的红色矩形和墙壁的灰色矩形。我的问题是用位图/图像替换矩形的正确方法是什么?

我知道要加载位图,您可以这样做:

Bitmap randomBitmap = BitmapFactory.decodeResource(getResources(),
com.example.android4gametest.R.drawable.ic_launcher);

我应该加载所有位图并将它们传递给它们的类,还是应该加载它们的类中的位图而不是传递它?我该怎么做,因为我无法使用 BitmapFactory,因为我无法访问 getResources()!或者我应该从我的 Assets 文件夹加载我的位图/图像,我知道我不会有相同的“工具”,你可以说它会弄乱位图。

主要 Activity

public class MainActivity extends Activity {
Game theGame;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(new Game(this));
}
}

游戏面板 公共(public)类 Game 扩展 SurfaceView 实现 SurfaceHolder.Callback {

GameThread _thread;

public Game(Context context) {
super(context);

getHolder().addCallback(this);

setFocusable(true);

_thread = new GameThread(getHolder(), this);


}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();

}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

@Override
protected void onDraw(Canvas canvas) {
Log.d("OnDraw", "it is Drawing");
canvas.drawColor(Color.BLUE);
}

public void update() {
// TODO Auto-generated method stub
}
}

GameLoop 这里什么都没有

    public class GameThread extends Thread{
/*FPS Code*/
private final static int MAX_FPS = 30;
private static final int FRAME_PERIOD = 1000/MAX_FPS;




protected SurfaceHolder holder;
protected Game game;
private boolean isRunning = false;

public GameThread(SurfaceHolder _holder, Game _game) {
this.holder = _holder;
this.game = _game;
}



/**
* Returns True if the game is still running and False if the game is over
* @return
*/
public boolean isRunning() {
return isRunning;
}
/**
* Set to true for the game loop to start
* @param isRunning
*/
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}


@Override
public void run() {
Canvas c;
Log.d("Pallyways", "Starting game Loop");

long beingTime;
long timeDiff;
int sleepTime;
int framesSkipped;

sleepTime = 0;

while(isRunning){
c = null;
try{

c = holder.lockCanvas();
synchronized(holder){
beingTime = System.currentTimeMillis();
framesSkipped = 0;
game.update();//Update
game.onDraw(c);//Redraw

timeDiff = System.currentTimeMillis() - beingTime ;
sleepTime = (int) (FRAME_PERIOD - timeDiff);

if(sleepTime>0){
try{
Thread.sleep(sleepTime);}
catch (InterruptedException e) {
e.printStackTrace();}
finally{}
}

while(sleepTime<0 && framesSkipped < 5){
game.update();

sleepTime+= FRAME_PERIOD;
framesSkipped++;
}

}
}finally{if(c!=null){
holder.unlockCanvasAndPost(c);
}
}
}
}

}

英雄类我还没有开始,但我想知道如何在不同包上的类上加载位图

package com.example.android4gametest.Actors;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.example.android4gametest.R;

public class Hero {
//getContext() gives me an error and that is because it does not have a reference

private Bitmap hero = BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.ic_launcher);
public Hero(){

}}

最佳答案

根据位图的大小/数量,将它们加载到 Game 类的构造函数中可能没问题。但请注意,如果您一次将太多/太大的位图加载到内存中,则可能会在第一次绘制调用时遇到一些 OOM 错误或滞后。您必须确保在不再需要位图时有效地清空并回收它们。我的第一款游戏就遇到了这个问题

关于java - 如何为 Android 游戏加载位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com