2 smallant93 smallant93 于 2014.09.02 22:53 提问

在SurfaceView extends Surface中定义了class,在里面重载onkeyup,就是执行不了

代码如下:

package com.rzy.plane;

import java.util.Random;
import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class MainActivity extends Activity {
MySurfaceView mysurfaceview = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mysurfaceview = new MySurfaceView(this);
setContentView(mysurfaceview);
}
}

class MySurfaceView extends SurfaceView implements Callback {

private SurfaceHolder sfh;
private Paint paint;
//定义游戏状态变量
public static final int GAME_MENU = 0;
public static final int GAMEING = 1;
public static final int GAME_WIN = 2;
public static final int GAME_LOST = 3;
public static final int GAME_PAUSE = -1;
//当前游戏状态(默认初始在游戏菜单界面)
public static int gameState = GAME_MENU;
public static int screenW;
public static int screenH;
//声明一个Resources实例便于加载图片
private Resources res = this.getResources();
private Bitmap bmpBackGround;//游戏背景
private Bitmap bmpBoom;//爆炸效果
private Bitmap bmpBoosBoom;//boss爆炸效果
private Bitmap bmpButton;//游戏开始按钮
private Bitmap bmpButtonPress;//游戏开始按钮点击
private Bitmap bmpEnemyduck;//怪物鸭子
private Bitmap bmpEnemyFiy;//怪物苍蝇
private Bitmap bmpEnemyBoss;//怪物boss
private Bitmap bmpGameWin;//游戏胜利背景
private Bitmap bmpGameLost;//游戏失败背景
private Bitmap bmpPlayer;//游戏主角飞机
private Bitmap bmpPlayerHp;//游戏主角飞机血量
private Bitmap bmpMenu;//菜单背景
private Bitmap bmpBullet;//子弹
private Bitmap bmpEnmeyBullet;//敌机飞机
private Bitmap bmpBossBullet;//Boss子弹
//声明一个菜单对象
private GameMenu gameMenu;
//声明一个滚动游戏背景对象
private GameBg backGround;
//声明主角对象
private Player player;
String tag = "MySurfaceView";

public MySurfaceView(Context context) {
    super(context);
    sfh = this.getHolder();
    sfh.addCallback(this);
    paint  = new Paint();
    paint.setColor(Color.WHITE);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    screenW = this.getWidth();
    screenH = this.getHeight();
    initGame();
    myDraw();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

public void myDraw() {
    Canvas canvas = sfh.lockCanvas();
    switch (gameState) {
    case GAME_MENU:
        gameMenu.draw(canvas, paint);
        Log.i(tag, "00");
        break;
    case GAMEING:
        //游戏 背景
        backGround.draw(canvas, paint);
        //主角绘图函数
        player.draw(canvas, paint);
        Log.i(tag, "0");
        break;
    case GAME_WIN: 
        break;
    case GAME_LOST: 
        break;
    case GAME_PAUSE:
        break;
    }
    sfh.unlockCanvasAndPost(canvas);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (gameState) {
    case GAME_MENU:
        break;
    case GAMEING:
        //主角的按键按下事件
        player.onKeyDown(keyCode, event);
        Log.i(tag, "1");
        break;
    case GAME_WIN: 
        break;
    case GAME_LOST:
        break;
    case GAME_PAUSE: 
        break;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (gameState) {
    case GAME_MENU:
        break;
    case GAMEING:
        //按键抬起事件
        player.onKeyUp(keyCode, event);
        Log.i(tag, "2");
        break;
    case GAME_WIN: 
        break;
    case GAME_LOST:
        break;
    case GAME_PAUSE:
        break;
    }
    return super.onKeyUp(keyCode, event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
//  Canvas canvas = sfh.lockCanvas();
    switch (gameState) {
    case GAME_MENU: 
        gameMenu.onTouchEvent(event);
        Log.i(tag, "1");
        myDraw();
        break;
    case GAMEING: 
        break;
    case GAME_WIN:
        break;
    case GAME_LOST: 
        break;
    case GAME_PAUSE:
        break;
    }

// sfh.unlockCanvasAndPost(canvas);
return true;
}

private void logic() {
    switch (gameState) {
    case GAME_MENU:
        break;
    case GAMEING:
        //背景逻辑
        backGround.logic();
        //主角逻辑
        player.logic();
        Log.i(tag, "3");
        break;
    case GAME_WIN: 
        break;
    case GAME_LOST: 
        break;
    case GAME_PAUSE:
        break;
    }
}

private void initGame() {
    if (gameState == GAME_MENU) {
        bmpBackGround = BitmapFactory.decodeResource(res, R.drawable.background);
        bmpBoom = BitmapFactory.decodeResource(res, R.drawable.boom);
        bmpBoosBoom = BitmapFactory.decodeResource(res, R.drawable.boom);
        bmpButton = BitmapFactory.decodeResource(res, R.drawable.button);
        bmpButtonPress = BitmapFactory.decodeResource(res, R.drawable.button_press);
        bmpEnemyduck = BitmapFactory.decodeResource(res, R.drawable.boss_duck);
        bmpEnemyFiy = BitmapFactory.decodeResource(res, R.drawable.boss_fly);
        bmpEnemyBoss = BitmapFactory.decodeResource(res, R.drawable.boss_pig);
        bmpGameWin = BitmapFactory.decodeResource(res, R.drawable.win);
        bmpGameLost = BitmapFactory.decodeResource(res, R.drawable.lost);
        bmpPlayer = BitmapFactory.decodeResource(res, R.drawable.zhujue);
        bmpPlayerHp = BitmapFactory.decodeResource(res, R.drawable.hp);
        bmpMenu = BitmapFactory.decodeResource(res, R.drawable.menu);
        bmpBullet = BitmapFactory.decodeResource(res, R.drawable.bullte);
        bmpEnmeyBullet = BitmapFactory.decodeResource(res, R.drawable.enemy_bullye);
        bmpBossBullet = BitmapFactory.decodeResource(res, R.drawable.bossbullte);

    }
    gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress);
    backGround = new GameBg(bmpBackGround);
    player = new Player(bmpPlayer, bmpPlayerHp);        
}

public class GameMenu {
    //菜单背景
    private Bitmap bmpMenu;
    //按钮图片资源(按下和未按下图)
    private Bitmap bmpButton, bmpButtonPress;
    //按钮坐标
    private int btnX, btnY;
    //按钮标记
    private Boolean isPress;
    //菜单初始化
    public GameMenu(Bitmap bmpMenu, Bitmap bmpButton, Bitmap bmpButtonPress) {
        this.bmpMenu = bmpMenu;
        this.bmpButton = bmpButton;
        this.bmpButtonPress = bmpButtonPress;
        //X居中,Y紧接屏幕底部
        btnX = MySurfaceView.screenW / 2 - bmpButton.getWidth() / 2;
        btnY = MySurfaceView.screenH / 2 - bmpButton.getHeight() / 2;
        isPress = false;
    }
    //菜单绘图函数
    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bmpMenu, 0, 0, paint);
        //绘制未按下按钮图
        if(isPress) {
            canvas.drawBitmap(bmpButtonPress, btnX, btnY, paint);
        } else {
            canvas.drawBitmap(bmpButton, btnX, btnY, paint);
        }
    }
    //菜单触屏事件函数,主要用于处理按钮事件
    public void onTouchEvent(MotionEvent event) {
        int pointX = (int) event.getX();
        int pointY = (int) event.getY();
        if (event.getAction() == MotionEvent.ACTION_DOWN ||
                event.getAction() == MotionEvent.ACTION_MOVE) {
            if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) {
                if (pointY > btnY && pointY < btnY + bmpButton.getHeight()) {
                    isPress = true;Log.i(tag, "TRYE");
                } else {
                    isPress = false;Log.i(tag, "FALSE1");
                }
            } else {
                isPress = false;Log.i(tag, "FALSE2");
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) {
                if (pointY > btnY && pointY < btnY + bmpButton.getHeight()) {
                    isPress = false;Log.i(tag, "FALSE3");
                    MySurfaceView.gameState = MySurfaceView.GAMEING;
                }
            }
        }
    }   
}

public class GameBg {
    //游戏背景的图片资源
    //为了循环播放,这里定义两个位图对象
    //其资源引用的是同一张图片
    private Bitmap bmpBackGround1;
    private Bitmap bmpBackGround2;
    //游戏背景坐标
    private int bg1x, bg1y, bg2x, bg2y;
    //背景滚动速度
    private int speed = 3;

    //游戏背景构造函数
    public GameBg(Bitmap bmpBackGround) {
        this.bmpBackGround1 = bmpBackGround;
        this.bmpBackGround2 = bmpBackGround;
        //首先让第一张背景底部正好填满整个屏幕
        bg1y = -Math.abs(bmpBackGround1.getHeight() - MySurfaceView.screenH);
        //第二张背景图紧接在第一张背景的上方
        //+101的原因:虽然两张背景图无缝隙连接但是因为图片资源头尾
        //直接连接不和谐,为了让视觉看不出是两张图连接而修正的位置
        bg2y = bg1y - bmpBackGround1.getHeight() + 101;
    }
    //游戏背景的绘图函数
    public void draw(Canvas canvas, Paint paint) {
        //绘制两张背景
        canvas.drawBitmap(bmpBackGround1, bg1x, bg1y, paint);
        canvas.drawBitmap(bmpBackGround2, bg2x, bg2y, paint);
    }
    //游戏背景的逻辑函数
    public void logic() {
        bg1y += speed;
        bg2y += speed;
        //当第一张图片的Y坐标超出屏幕,
        //立即将其坐标设置到第二张图的上方
        if (bg1y > MySurfaceView.screenH) {
            bg1y = bg2y - bmpBackGround1.getHeight() + 111;
        }
        if (bg2y > MySurfaceView.screenH) {
            bg2y = bg1y - bmpBackGround1.getHeight() + 111;
        }
    }
}

public class Player {
    //主角血量与血量位图
    //默认3血
    private int playerHp = 3;
    private Bitmap bmpPlayerHp;
    //主角的坐标以及位图
    public int x, y;
    private Bitmap bmpPlayer;
    //主角移动速度
    private int speed = 5;
    //主角移动白哦是
    private boolean isUp, isDown, isLeft, isRight;
    //主角的构造函数
    public Player(Bitmap bmpPlayer, Bitmap bmpPlayerHp) {
        this.bmpPlayer = bmpPlayer;
        this.bmpPlayerHp = bmpPlayerHp;
        x = MySurfaceView.screenW / 2 - bmpPlayer.getWidth() / 2;
        y = MySurfaceView.screenH - bmpPlayer.getHeight();
    }
    //主角的绘图函数
    public void draw(Canvas canvas, Paint paint) {
        //绘制主角
        canvas.drawBitmap(bmpPlayer, x, y, paint);
        //绘制血量
        for (int i = 0; i < playerHp; i++) {
            canvas.drawBitmap(bmpPlayerHp, i * bmpPlayerHp.getWidth(),
            MySurfaceView.screenH - bmpPlayerHp.getHeight(), paint);
        }
    }
    //试题按键
    public void onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            isUp = true;
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            isDown = true;
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            isLeft = true;
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            isRight = true;
        }
    }
    //实体按键抬起
    public void onKeyUp(int keyCode, KeyEvent event) {
        Log.i(tag, "player.keyup");
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            isUp = false;
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            isDown = false;
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            isLeft = false;
        }
        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            isRight = false;
        }
    }
    //主角的逻辑
    public void logic() {
        //处理主角移动
        if(isLeft) {
            x -= speed;
        }
        if(isRight) {
            x += speed;
        }
        if(isUp) {
            y -= speed;
        }
        if(isDown) {
            y += speed;
        }
        //判断品目X边界
        if (x + bmpPlayer.getWidth() >= MySurfaceView.screenW) {
            x = MySurfaceView.screenW - bmpPlayer.getWidth();
        } else if (x <= 0) {
            x = 0;
        }
        //判断品目Y边界
        if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
            y = MySurfaceView.screenH - bmpPlayer.getHeight();
        } else if (y <= 0) {
            y = 0;
        }
    }
    //设置主角血量
    public void setPlayerHp(int hp) {
        this.playerHp = hp;
    }
    //获取主角血量
    public int getplayerHp() {
        return playerHp;
    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!