android:两个位图的碰撞

时间:2013-08-12 15:10:00

标签: android

美好的一天...我是一个新的程序员在android ...实际上它是我第一次这样做... 但我对java有基本的了解......

所以这里......我的游戏将有一个可以通过操纵杆控制的图标......以及另一个在屏幕上上下移动的图标(不可控)我希望速度增加一点......我已经启动了代码,但不知道在哪里以及如何开始碰撞..另一个是控制器的图标总是通过屏幕并弹出相反的方向......

    public class GameSurface1 extends SurfaceView implements SurfaceHolder.Callback {

private Context _context;
private GameThread1 _thread;
private GameControl _controls;
private GameJoystick1 _joystick;
private int y = 0; 

private int xSpeed = 1;


private Bitmap _pointer, bmp;


public GameSurface1(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    _context = context;
    init();
}


private void init(){
    //initialize our screen holder
    SurfaceHolder holder = getHolder();
    holder.addCallback( this);

    //initialize our game engine

    //initialize our Thread class. A call will be made to start it later
    _thread = new GameThread1(holder, _context, new Handler(),this);
    setFocusable(true);


    _joystick = new GameJoystick1(getContext().getResources());
    _pointer = (Bitmap)BitmapFactory.decodeResource(getResources(), R.drawable.icon1);
    bmp = (Bitmap)BitmapFactory.decodeResource(getResources(), R.drawable.bad1);
    //contols
    _controls = new GameControl();
    setOnTouchListener(_controls);
}


public void doDraw(Canvas canvas){
     if (y == getHeight() - bmp.getHeight()) {
         xSpeed = -1;
  }
  if (y == 0) {
         xSpeed = 1;
  }
  y = y + xSpeed;


    //update the pointer
    _controls.update(null);

    //draw the pointer
    canvas.drawBitmap(_pointer, _controls._pointerPosition.x, _controls._pointerPosition.y, null);

    //draw the joystick background
    canvas.drawBitmap(_joystick.get_joystickBg(), 15,215, null);

    //draw the dragable joystick
    canvas.drawBitmap(_joystick.get_joystick(),_controls._touchingPoint.x - 26, _controls._touchingPoint.y - 26, null);
    canvas.drawBitmap(bmp, 280, y, null);
}



//these methods are overridden from the SurfaceView super class. They are automatically called 
//when a SurfaceView is created, resumed or suspended.
@Override 
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {}
private boolean retry;
@Override 
public void surfaceDestroyed(SurfaceHolder arg0) {
    retry = true;
    //code to end gameloop
    _thread.state = GameThread1.STOPED;
    while (retry) {
        try {
            //code to kill Thread
            _thread.join();
            retry = false;
        } catch (InterruptedException e) {
        }
    }

}

@Override 
public void surfaceCreated(SurfaceHolder arg0) {
    if(_thread.state==GameThread1.PAUSED){
        //When game is opened again in the Android OS
        _thread = new GameThread1(getHolder(), _context, new Handler(),this);
        _thread.start();
    }else{
        //creating the game Thread for the first time
        _thread.start();
    }
}
感谢你能给予的所有帮助...... 谢谢

0 个答案:

没有答案