我一直在开发一个没有位图或任何东西的游戏,我使用矩形作为对象,并根据它们的目的改变它们的颜色,比如玩家的红色矩形和墙壁的灰色矩形。我的问题是用位图/图像替换矩形的正确方法是什么?
我知道加载位图你可以这样做:
Bitmap randomBitmap = BitmapFactory.decodeResource(getResources(),
com.example.android4gametest.R.drawable.ic_launcher);
我应该加载所有位图并将它们传递给他们的类,还是应该在他们的类中加载位图而不是传递它?以及我将如何做到这一点,因为我无法使用 BitmapFactory,因为我无法访问 getResources()!或者我应该从我的资产文件夹中加载我的位图/图像,我知道我不会有相同的“工具”,你可以说会弄乱位图。
主要活动
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));
}
}
游戏面板公共类 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(){
}}