firestart
追星花狸
2017-02-10 03:14

SurfaceView 中 触发OnTouch 事件时图片绘制异常,怎么解决

  • surfaceview ontouch
  • 异常
    • 1. public class PaintGameView extends SurfaceView implements Runnable, SurfaceHolder.Callback, View.OnTouchListener { Config config = new Config(); private SurfaceHolder holder; private Bitmap lsecCache; private ArrayList gameImages = new ArrayList<>(); private ArrayList bullets = new ArrayList<>(); private ArrayList enemys = new ArrayList<>(); private Boolean runState = false; private Context context; private Hero selectedPlane; public PaintGameView(Context context, Config config){ super(context); this.context = context; getHolder().addCallback(this); setOnTouchListener(this); } private void init(){ new importInit(config); enemys = importInit.getsBitmap("enemy"); gameImages.add(new BackgroundImage(config)); gameImages.add(new Enemy(config, enemys)); gameImages.add(new Hero(config)); lsecCache = Bitmap.createBitmap(config.getWidth(), config.getHeight(), Bitmap.Config.ARGB_8888); }

    @Override
    public void run() {
    Paint p = new Paint();
    int time = 0;
    int timeBullet = 0;
    Canvas canvas;
    try {
    while (runState){

            //描绘子弹部分
            if(selectedPlane != null){
                if(timeBullet++ == 4){
                    bullets.add(new Bullet(config, selectedPlane, bullets));
                    timeBullet = 0;
                }
            }
           canvas = new Canvas(lsecCache);
    
            //默认描绘
            for(GameViews gameImage : (ArrayList<GameViews>)gameImages.clone()){
                if(gameImage instanceof Enemy){
                    Enemy enemy = (Enemy) gameImage;
                    if(!enemy.flag){
                       enemy.attacked(bullets);
                    }else {
                        if(enemy.index++ > 3){
                            gameImages.remove(gameImage);
                            continue;
                        }
                    }
    
                }
                canvas.drawBitmap(gameImage.getBitmap(), gameImage.getX(), gameImage.getY(), p);
            }
            for(GameViews gameImage : (ArrayList<GameViews>)bullets.clone()){
                canvas.drawBitmap(gameImage.getBitmap(), gameImage.getX(), gameImage.getY(), p);
            }
            if(time++ == 50){
                gameImages.add(new Enemy(config, enemys));
                time = 0;
            }
    
            Canvas c = holder.lockCanvas();
    

    // c.drawBitmap(bg , 0, 0, p);
    c.drawBitmap(lsecCache, 0, 0, p);
    holder.unlockCanvasAndPost(c);
    Thread.sleep(10);
    }
    }catch (Exception e){
    Log.e("APP.TAG","异常",e);
    }
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    config.setContext(context);
    config.setSpeed(4);
    config.setGameViewses(gameImages);
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    runState = true;
    //获取屏幕尺寸
    config.setWidth(width);
    config.setHeight(height);

    init();
    this.holder = holder;
    new Thread(this).start();
    

    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    runState = false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_DOWN){
    for(GameViews gameView : gameImages) {
    if(gameView instanceof Hero){
    if(gameView.selected(event.getX(), event.getY())){
    // Log.i("APP.TAG", "我被选中!");
    selectedPlane = (Hero) gameView;
    }else {
    selectedPlane = null;
    }
    break;
    }
    }

    }else if(event.getAction() == MotionEvent.ACTION_MOVE){
        if(selectedPlane != null){
            selectedPlane.setX((int) event.getX() - selectedPlane.getBitmap().getWidth()/2);
            selectedPlane.setY((int) event.getY() -  selectedPlane.getBitmap().getHeight()/2);
        }
    }else if(event.getAction() == MotionEvent.ACTION_UP){
        selectedPlane = null;
    }
    return true;
    

    }
    }

发射子弹时,敌机加速了,是onTouch引发的,查过不是参数配置引起的,求解决

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答