博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

J2ME游戏开发之层:动画

Posted on 2011-08-25 09:39  zhangweia  阅读(392)  评论(0编辑  收藏  举报

 

package com.slinw.TiledLayout;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.TiledLayer;

import com.slinw.gamecanvas.RootGameCanvas;

/**
 * 
 * @author 章伟
 * int ANI = tiledLayer.createAnimatedTile(STR);
 *         创建一个动态贴图,动态贴图必须与一个静态贴图关联
 * mBackground.setAnimatedTile(ANI,SHN);
 *         切换动画效果
 * 
 * 拿下面的工程来说,是怎样实现星星闪烁的动画的
 *         1. 首先定义一个动画贴片 ANI,通过调用createAnimatedTile方法使这个动画贴片与星星关联
 *         2. 然后通过调用setAnimatedTile(ANI,SHN)是星星和闪烁交替呈现而实现动画
 */
public class AnimTiledLayout extends RootGameCanvas{

      private TiledLayer mBackground;//地图
      private int aniCounter = 0;
      private int MON = 1;//月亮moon
      private int SHN = 2;//闪烁shine
      private int STR = 3;//星星star
      private int AIR = 4;//夜空air
      private int ANI = -1;//动画贴片animated
      
    public AnimTiledLayout(){
        initData();
    }
    
    private void initData(){
        
        
        try{
            mBackground = createBackground();//创建背景
        }catch(IOException e){
        System.out.println("create error");
        }

        mBackground.setPosition(50,50);  

    }
    
      private TiledLayer createBackground()throws IOException {
            Image image = Image.createImage("/star.png");//创建背景图片
            TiledLayer tiledLayer = new TiledLayer(10, 10, image, 32, 32);//注意:背景的参数的设置!
            int columns = 10;
            
            ANI =  tiledLayer.createAnimatedTile(STR);
            
            int[] map = {//地图数组
               AIR,AIR,AIR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,
               AIR,ANI,AIR,AIR,AIR, AIR,AIR,AIR,STR,AIR,
               AIR,AIR,STR,AIR,AIR, ANI,AIR,AIR,STR,AIR,
               AIR,AIR,AIR,MON,STR, AIR,AIR,STR,AIR,AIR,
               AIR,AIR,STR,STR,AIR, AIR,AIR,AIR,AIR,AIR,
               AIR,AIR,STR,AIR,ANI, AIR,AIR,AIR,ANI,AIR,
               AIR,AIR,AIR,AIR,AIR, STR,AIR,AIR,AIR,AIR,
               AIR,AIR,STR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,
               AIR,STR,AIR,AIR,AIR, STR,AIR,AIR,AIR,AIR,
               AIR,AIR,AIR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,
            };//显示地图
            
            for (int i = 0; i < map.length; i++) {
              int column = i % columns;
              int row = (i - column) / columns;
              tiledLayer.setCell(column, row, map[i]);//通过一个循环来设置tiledLayer
            }
            
            return tiledLayer;//返回
          }
      
    public void drawScreen(Graphics g) {
        // TODO Auto-generated method stub
         aniCounter++;
         
         if(aniCounter%6 == 0){
             mBackground.setAnimatedTile(ANI,SHN);
         }        
         else{
             mBackground.setAnimatedTile(ANI,STR);
         }
         
        mBackground.paint(g);
        flushGraphics();
    }

    public void keyLeftPressedEvent() {
        // TODO Auto-generated method stub
        
    }

    public void keyRightPressedEvent() {
        // TODO Auto-generated method stub
        
    }

    public void keyUpPressedEvent() {
        // TODO Auto-generated method stub
        
    }

    public void keyDownPressedEvent() {
        // TODO Auto-generated method stub
        
    }

}