在Android中移动精灵

时间:2014-07-18 13:05:44

标签: java android eclipse loops

我正在制作一款需要精灵向屏幕底部移动的游戏。你可以想象它有点像节奏游戏(吉他英雄)。但是,我遇到了问题,因为精灵根本没有移动,即使循环似乎工作正常。

GamePanel -

package com.jollygent.tapthepointer;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GamePanel extends SurfaceView{

SurfaceHolder holder;
GameLoopThread loop;
GamePanel game = this;
Random rand;
int y;
//ArrayList<Pointers> pointers = new ArrayList<Pointers>();
Pointers pointers;

public GamePanel(Context context) {
    super(context);
    holder = getHolder();
    loop = new GameLoopThread(this);
    rand = new Random();
    pointers = new Pointers(rand.nextInt(4)+1,getWidth()/2,0,this); 
    //placeholder 


    this.setBackgroundColor(Color.WHITE);

    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
            loop.setRunning(false);
        }

        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            loop = new GameLoopThread(game);

            if(!loop.isRunning()){
                loop.setRunning(true);
                loop.start();
            }
            else{
                loop.setPause(false);
            }

        }

        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stubn

        }
    });



}

public GameLoopThread getLoop(){
    return loop;
}

public void update(){



}

public void draw(Canvas canvas){

    pointers.draw(canvas);


    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(20);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawLine(0,getHeight()/2 + 50,getWidth(),getHeight()/2 + 50, paint);


  }

}

指针类

package com.jollygent.tapthepointer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;

public class Pointers {

GamePanel game;
private int x,y;
Bitmap pointer;
Bitmap rotated_pointer;
Matrix matrix;

public Pointers(int type,int x,int y,GamePanel game){

    this.game = game;
    this.x = x;
    this.y = y;
    pointer = BitmapFactory.decodeResource(game.getResources(),R.drawable.pointer_xx);
    matrix = new Matrix();

    switch(type){


    //points right
    case 1:
        matrix.postRotate(90);
        break;
    //points left
    case 2:
        matrix.postRotate(-90);
        break;
    //points up
    case 3:
        rotated_pointer = pointer;
        break;
    //points down
    case 4:
        matrix.postRotate(180);
        break;

    }

    rotated_pointer =   Bitmap.createBitmap(pointer,0,0,pointer.getHeight(),pointer.getWidth(), matrix,true);
}


public void update(){

    /*if(y < game.getHeight()){
        y += pointer.getHeight();
    }*/

}

public int getHeight(){
    return pointer.getHeight();
}

public void draw(Canvas canvas){

    y++;//placeholder movement to see if the sprite actually moves. It doesn't.
    canvas.drawBitmap(rotated_pointer,x,y,null);

 }

}

最后,我认为发布GameLoopThread是必要的 -

package com.jollygent.tapthepointer;

import android.graphics.Canvas;

public class GameLoopThread extends Thread{

GamePanel game;
boolean running  = false;
boolean paused = false;
Canvas canvas;
static final int FPS = 10;



public GameLoopThread(GamePanel game){
    this.game = game;
}

public boolean isRunning(){
    return running;
}

public void setRunning(boolean b){
    this.running = b;
}

public void setPause(boolean b){
    this.paused = b;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    long beginTime;
    long ticks = 1000/FPS;
    long sleepTime;


    while(running){


       if(!paused){ 
           beginTime = System.currentTimeMillis();
           canvas = null;
           try{
               canvas = game.holder.lockCanvas();
               synchronized(game.getHolder()){
                   game.draw(canvas);
               }
           }finally{
               if(canvas != null){
                   game.getHolder().unlockCanvasAndPost(canvas);
               }
           }

           sleepTime = (System.currentTimeMillis() - beginTime)/ticks;

           try{
               if(sleepTime > 0) 
                   Thread.sleep(sleepTime);
               else
                   Thread.sleep(10);

           }catch(Exception e){
               e.printStackTrace();
           }

       }
    }       

}




    }

我可能犯了一个非常愚蠢的错误,但无论我扫描多少代码并改变几位,我似乎无法弄清楚精灵的移动方式。另外,我怎样才能使精灵在任何设备中完全居中?

谢谢。

编辑:

package com.jollygent.tapthepointer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class GameActivity extends Activity {

GamePanel game;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    game = new GamePanel(this);
    setContentView(game);

}

@Override
public void onPause(){
    super.onPause();
    game.getLoop().setPaused(true);
}

@Override
public void onResume(){ 
    super.onResume();
    game.getLoop().setPaused(false);

  }
}

问题是循环只运行一次且仅运行一次。也许GameActivity是问题吗?

0 个答案:

没有答案