J2ME控制响应按键的算法以及冲突检测的方法和动画的处理

来源:互联网 发布:南昌淘宝大学 编辑:程序博客网 时间:2024/05/01 03:47

一 响应按键

//保证左右移动停止后,恢复正向图像帧
    plane_sp.setFrame(0);
    int KeyState = getKeyStates(); // 获得当前按键状态
    // 根据按键状态,飞机图象做相对移动,并判断是否超出屏幕边界

if ( (KeyState & UP_PRESSED) != 0) { // 上
      if (plane_sp.getY() - 3 < 0) {
        plane_sp.move(0, 0 - plane_sp.getY()); //出边界的处理
      }
      else {
        plane_sp.move(0, -3); //正常处理
      }
      plane_sp.setFrame(0);
    }
    if ( (KeyState & DOWN_PRESSED) != 0) { // 下
      if (plane_sp.getY() + 3 + plane_sp.getHeight() > win_height) {
        plane_sp.move(0, win_height - plane_sp.getY() - plane_sp.getHeight());
      }
      else {
        plane_sp.move(0, 3);
      }
      plane_sp.setFrame(0);
    }
    if ( (KeyState & LEFT_PRESSED) != 0) { // 左
      if (plane_sp.getX() - 3 < 0) {
        plane_sp.move(0 - plane_sp.getX(), 0);
      }
      else {
        plane_sp.move( -3, 0);
      }
      plane_sp.setFrame(1);
    }
    if ( (KeyState & RIGHT_PRESSED) != 0) { // 右
      if (plane_sp.getX() + 3 + plane_sp.getWidth() > win_width) {
        plane_sp.move(win_width - plane_sp.getX() - plane_sp.getWidth(), 0);
      }
      else {
        plane_sp.move(3, 0);
      }
      plane_sp.setFrame(2);
    }

二  冲突检测的方法

plane_sp.collidesWith(cloud_img, 20, 20, true)

sp0.collidesWith(sp1, pixelLevel)

分析:语法
 精灵与精灵的碰撞处理
public final boolean collidesWith(Sprite s, boolean pixelLevel)
                  pixelLevel取true代表像素检测模式,false代表区域检测模式

精灵与背景层的碰撞处理
public final boolean collidesWith(TiledLayer t,boolean pixelLevel)
                 pixelLevel取true代表像素检测模式,false代表区域检测模式

精灵与图象的碰撞处理
public final boolean collidesWith(Image image,int x,int y,boolean pixelLevel)
               image 碰撞图象,x,y为图象左上角坐标
               pixelLevel取true代表像素检测模式,false代表区域检测模式

如果要显示爆炸的图象指定出现位置的算法是
  飞机当前x 坐标-(爆炸图片的宽度-飞机的宽度)/2

三 动画的处理

1  Sprite类的构造器
创建只含有一针图象的非动画精灵对象
     public Sprite (Image image)
创建一个指定每针图象大小的动画精灵对象
    public Sprite (Image image,int frameWidth,int frameHeight)
指定一个已经创建好的精灵对象来创建一个新的精灵对象
   public Sprite (Sprite s)

2  Sprite类的一些方法
    ⑴重新设置精灵对象的Image图象
    public void setImage(Image img,int frameWidth,int frameHeight)
     返回所有原始图象针总数
     public int getRawFrameCount()
     依次正确播放动画
     public void nextFrame()
     逆向播放
     public void prevFrame()
     单针图象处理
     public void setFrame(int num)
                 num是指定图象的索引
     多针动画处理
     public void setFrameSequence(int[ ] sequence)
                 sequence是指定播放序列的整数数组
         获得精灵对象中指定的动画播放序列中一次循环的画面针数,用getFrameSequenceLength()得
         用 getFrame()返回当前图象针.
    ⑵设置精灵的位置
        public void setPosition(int x,int y)
       移动的处理
        public void move(int dx,int dy)
                   dx,dy是水平和垂直移动的距离.
       返回精灵的当前x,y坐标
        public final int getX()
        public final int getY()
      方位变换的处理
        public void setTransform(int transform)
        transform的值
            Sprite.TRANS_NONE      取值  0   效果:保持原位
            Sprite.TRANS_ROT90     取值  5   效果:顺时针旋转90度
            Sprite.TRANS_ROT180   取值  3   效果:顺时针旋转180度
            Sprite.TRANS_ROT270   取值  6   效果:顺时针旋转270度
            Sprite.TRANS_MIRROR   取值  2   效果:沿垂直轴翻转
            Sprite.TRANS_MIRROR_ROT90   取值  7     效果:沿垂直轴翻转再顺时针旋转90度
            Sprite.TRANS_MIRROR_ROT180   取值  1   效果:沿垂直轴翻转再顺时针旋转180度
            Sprite.TRANS_MIRROR_ROT270   取值  4   效果:沿垂直轴翻转再顺时针旋转270度

⑶  引用像素点处理

  设置引用像素点
  语法:   public void defineReferencePixel( int x, int y )
     x,y是相对于图像(Sprite类的实例)的x,y坐标,
  例如:sp1.defineReferencePixel(img_width1 / 2, 0);

通过引用像素点来定位
  语法:   public void setRefPixelPosition ( int x, int y )
           x,y是相对于手机屏幕的左上角点的坐标
例如:sp1.setRefPixelPosition(x1, y1);

获得当前精灵的引用像素点在系统中的位置
  语法:   public final int getX( )
              public final int getY( )

 

原创粉丝点击