我一直在對遊戲沒有位圖或任何東西,我使用矩形作爲對象,並改變它們的顏色,他們的目的就像一個紅色矩形的播放機和牆壁灰色矩形。我的問題是用位圖/圖像替換矩形的正確方法是什麼?如何爲Android遊戲加載位圖?
我知道加載位圖,你可以只是這樣做:
Bitmap randomBitmap = BitmapFactory.decodeResource(getResources(),
com.example.android4gametest.R.drawable.ic_launcher);
我應該載入我所有的位圖,並把它們傳遞給他們班或者我應該加載其類中的位圖,而不是通過它的?我怎麼做,因爲我不能使用BitmapFactory,因爲我沒有訪問getResources()!或者我應該加載我的資產文件夾我的位圖/圖像,我知道我不會有相同的「工具」,你可以說混亂的位圖。
MainActivity
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));
}
}
遊戲盤 公共類遊戲延伸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(){
}}
我將不得不使用位圖傳遞到上下文我的課,以加載它?所以我們可以說我的類英雄構造函數會像'英雄(上下文)' –
啊哈我不好,認爲你的類擴展視圖。是的,那也行得通。只是不要保留對Context的全局引用或將它保留在WeakReference中 – Ljdawson
我將用我的4個類更新我的問題 –