当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>J2ME

基于MIDP1.0实现组合按键

  我们知道有些手机是不支持重复按键功能,为了模拟重复按键的功能,我们不得不使用一个线程来进行处理。对于重复按键网上已经有一些文章进行了探讨,其实利用线程我们还可以实现组合按键的功能。当然,在手机这种设备上,利用组合按键进行游戏,有一定的限制,因为手机各个按键排列比较紧凑,同时按下两个甚至三个按键在操作上会有局限性。但这也是仁者见仁、智者见智的问题,如果你想提高游戏的可玩性和娱乐性,组合按键仍是必不可少的条件。

  我定义了一个进行按键监听的类,这个类实现了Runnable接口。在这个类里关键的一个数据结构就是一个字节型的数组,它是用来存储当前所有被按下的键。我这里把数组的长度设定为2,主要是从应用的角度来考虑的,因为毕竟在当前手机设备上同时按三个键进行游戏的不多。下面就是实现组合按键的核心代码。

  按键事件类:

import javax.microedition.lcdui.*;

public class NewEvent implements Runnable{

        KeyEventCanvas c=null;

        /*
         * 用来判断是否有键被按下
         */
        protected boolean switchPoint;

        /*
         * 这个数组用来存储所有同时被按键的键码
         */
        protected byte[] keyCode;

        /*
         * 访问数组的索引
         */
        byte index;


        public NewEvent(KeyEventCanvas c){
                this.c=c;
                keyCode=new byte[2];
        }

        /*
         * 这个方法把被按键的键码存储到数组中
         */
        public synchronized void startAction(int keyCode){
                this.keyCode[index]=(byte)keyCode;
                index=(byte)((index+1)%this.keyCode.length);
        }

        /*
         * 当某个键被释放时,这个方法把该键存储在数组中的键码置为0
         */
        public synchronized void stopAction(int keyCode){
                for(int i=0;i<=this.keyCode.length-1;i++){
                        if(this.keyCode[i]==(byte)keyCode){
                                this.keyCode[i]=0;
                                break;
                        }
                }
        }
        
        /*
         * 这个方法用来判断是否有被按下的键,返回true表示有键按下,返回false表示没有键被按下
         */
        public synchronized boolean getSwitchPoint(){
                this.switchPoint=false;
                for(int i=0;i<=this.keyCode.length-1;i++){
                        if(this.keyCode[i]!=0){
                                this.switchPoint=true;
                                break;
                        }
                }
                return this.switchPoint;
        }

        /*
         * 返回字节数组
         */
        public synchronized byte[] getKeyCode(){
                return this.keyCode;
        }
        
        public void run(){
                byte[] tmp=this.getKeyCode();
                while(true){
                        while(this.getSwitchPoint()){
                                /*
                                 * 通过这个循环遍历数组,遇到被按下的键就进行处理
                                 */
                                for(int i=0;i<=tmp.length-1;i++){

                                        if(tmp[i]!=0){
                                                switch(c.getGameAction(tmp[i])){
                                                
                                                case Canvas.UP:
                                                        c.y--;//y是在Canvas中定义的一个属性,表示y轴坐标
                                                        break;
                                                case Canvas.DOWN:
                                                        c.y++;
                                                        break;
                                                case Canvas.LEFT:
                                                        c.x--;//x是在Canvas中定义的一个属性,表示x轴坐标
                                                        break;
                                                case Canvas.RIGHT:
                                                        c.x++;
                                                        break;
                                                }
                                        }
                                }
                                c.repaint();
                                try{
                                        Thread.sleep(10);
                                }catch(InterruptedException e){
                                        
                                }
                        }
                }
        }
}




  Canvas类:

import javax.microedition.lcdui.*;

public class KeyEventCanvas extends Canvas{

        int x,y;
        NewEvent mke=null;
        
        public KeyEventCanvas(){
                mke=new NewEvent(this);
                new Thread(mke).start();
        }

        public void keyPressed(int keyCode){
                mke.startAction(keyCode);
        }
        
        public void keyReleased(int keyCode){
                mke.stopAction(keyCode);
        }

        public void paint(Graphics g){
                g.setColor(0xffffff);
                g.fillRect(0,0,this.getWidth(),this.getHeight());
                g.setColor(0xff0000);
                g.fillRect(x,y,8,8);
        }
}
相关内容
赞助商链接