我一直在没有刻度图或任何东西的游戏上工作,我将矩形用作对象,并为其目的而更改其颜色,例如播放器的红色矩形和墙壁的灰色矩形。我的问题是,用位图/图像替换矩形的正确方法是什么?
我知道要加载位图,您可以做到这一点:
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(){
}}
取决于位图的大小/数量,将它们加载到游戏类的构造函数中可能是可以的。不过请注意,如果您一次将太多/太大的位图加载到内存中,则可能会遇到一些OOM错误或第一次绘制通话时陷入困境。您必须确保自己无效,当您不再需要时会有效回收位图。我的第一个游戏
请勿将Bitmap
加载到游戏对象的构造函数中。如果这些Bitmaps
中的任何一个将由多个对象(即敌人类)使用,则如果将它们加载到对象的构造函数中,则将在内存中具有多个Bitmap
的副本。这可能会导致OutOfMemoryException
。而是将Bitmap
加载在构造函数之外(在您的游戏线程或游戏面板中),然后将Bitmap
对象传递给构造函数或设置器,然后以这种方式设置私有Bitmaps
。您只需要在内存中保留有限数量的Bitmaps
。
您可能还需要考虑加载"屏幕",该加载栏或旋转器加载 Bitmaps
。
加载并不重要。我认为您的主要问题是访问资源。从视图中,您可以致电:
getContext().getResources()
编辑:
因此,在英雄构造函数的方法签名中添加上下文作为参数:
public Hero(Context context){
private Bitmap hero = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}
不要!使用动态对象。我的意思是根本不会在游戏中创建动态对象,这些对象会触发GC并在需要时在运行时使所有东西都在其过程中。您必须自己控制一切,并使用静态/最终的"对象"以及障碍物和敌人的数组。对象将被加载到启动时的堆,这意味着您将加载它们,直到应用程序关闭为止。因此,您必须在这些静态对象中找到平衡,而不是在找到它之间,但是当您找到它时很容易。
位图应在这些静态非动力对象之外加载,这些对象应仅执行计算。
,永远不要使用任何不合理的计算,循环或线程。c = holder.lockcanvas();和holder.unlockcanvasandpost(c);就像您在代码中所做的一样。这可以并且最终将使游戏闪烁。
问候,我