これはあなたのために動作するかどうか私は知らない、またはあなたがこれを解決しますが、私はこのアプローチを行った場合:
Activity.class
//Create an activity to hold everything
public class GameActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Rel_main_game = (RelativeLayout) findViewById(R.id.rlt_main);
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
final int heightS = dm.heightPixels;
final int widthS = dm.widthPixels;
//this is the SurfaceView that will do the magic
game_panel = new GamePanel(getApplicationContext(),this,widthS, heightS);
Rel_main_game.addView(game_panel);
}
SurfaceView
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
public MainThread thread;
public int ScreenWidth;
public int Screenheigt;
public GameActivity game;
public GamePanel(Context context, GameActivity game, int width , int height) {
super(context);
getHolder().addCallback(this);
this.game = game;
this.ScreenWidth=width;
this.Screenheigt=height;
thread = new MainThread(getHolder(),this);
ship = new Ship(BitmapFactory.decodeResource(getResources(), R.drawable.player), 100, 0, ScreenWidth, Screenheigt);
}
void Draw(Canvas canvas){
if (canvas!=null)
{
ship.draw(canvas);
}
}
void Update(float dt){
ship.udpdate(dt);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
while (retry)
{
try
{
thread.join();
retry=false;
}
catch (InterruptedException e)
{
}
}
}
}
とし、描画メソッドと更新メソッドを持つ子クラスを追加します。
public class Ship {
public Bitmap bitmap;
public int x;
private int y;
private int Speed;
private int inc;
private int ScreenWidth;
private int ScreenHeight;
public int direction=0;
boolean death;
float animTime=0;
float totalAnimTime = 1;
float numFrames;
public Ship(Bitmap decodeResource, int x, int y, int screenWidth, int screenheight) {
this.bitmap = decodeResource;
this.x =screenWidth/2 - bitmap.getWidth()/2;
this.y = screenheight - bitmap.getHeight();
Speed=1;
inc=0;
death=false;
ScreenWidth =screenWidth;
ScreenHeight =screenheight;
}
public void draw(Canvas canvas){
if (!death)
{
canvas.drawBitmap(bitmap, x, y - bitmap.getHeight()/2, null);
}
}
public void update(float dt){
if (death)
{
animTime += dt;
}
else
{
float movement =ScreenWidth/2*dt;
if (x - (bitmap.getWidth()/ 2)<ScreenWidth || x > - bitmap.getWidth()/ 2)
switch(direction)
{
case(1):
{
x+=8;//movement*dt;
break;
}
case(2):
{
x-=8;//movement*dt;
break;
}
}
}
}
など。
誰でもアイデアはありますか? – user788511
これには解決策がありましたか? – Aiapaec