Sprite自用版

來源:互聯網
上載者:User

import javax.microedition.lcdui.*;
 
 
    public abstract  class Sprite  {
   
 
   
int x,y; //不用解釋X,Y座標
  /*                             */ 
  public int getX() { return x; }
        
  public int getY() { return y; }
  /*                             */
   
   
   
     ////////////////////////////////////////////  
int sequence[];//自訂序列

      void setFrameSequence(int[] seq){
         sequence =new int[seq.length];
         for(int i=0;i<seq.length;i++){
             sequence[i]=seq[i];
            }                               }
           
      void setFrame(int t){
         sequence =new int[1];
         sequence[0]=t;
         if(sequence[0]>=framecount)
              sequence[0]=0;
         if(sequence[0]<0)
              sequence[0]=framecount;   }
    

int framecount;  //禎圖象數量

int count=0;
   
       
    ////////////////////
    /////2大建構函式///
    ///////////////////

  
int n;
Image sprite; 

 int width,height;

    //從圖片擷取SPRITE
        
     Sprite(Image img,int frameWidth,int frameHeight){
    
           this.sprite=img;
    
           this.width=frameWidth;
           this.height=frameHeight;
      
        int a=img.getWidth()/width;
            n=a;
        int b=img.getHeight()/height;
           
           framecount=a*b;                                 }
Image img=null;   
    //單圖SPRITE

     Sprite(Image img) {
     
       this.img=img;        } 
  
          
    ////////////////////////
    ///     是否可見    ///
    ////////////////////////     
static boolean visible=true;//預設可見

     void setVisible(boolean visible){
    
      this.visible=visible;                }
     
     boolean isVisible(){
    
      return visible;         }
     
     ///////////////////////////
     /////////描繪部分/////////
     ///////////////////////////

public void setPosition(int x,int y){

   this.x=x;this.y=y;
}

     
      void paint(Graphics g){
              
      
        if(visible){
                   if(img!=null)
                     g.drawImage(img,x,y,0);
                     else
                   
                    
                     if(sequence==null){ //預設正常序列
                     
                     int a=(count%framecount%n)*width;
                     int b=(count%framecount/n)*height;//優先順序問題必須注意
                    
                     Trans(g,x-a,y-b,transform);//帶有翻轉的顯示
                                    
                     count++;
                     }else{
                     g.setClip(x,y,width,height);
                     int d=sequence.length;
                     int c=count%d;
                     Trans(g,x-sequence[c]%n*width,y-(sequence[c]/n)*height,transform);
                    
                      count++; 
                                     } 
                   }                         }
     /////////////////////////////////////////////
     ///翻轉出於技術考慮唯寫了水平翻轉和垂直翻轉//
     ////////////////////////////////////////////
int transform=0;//翻轉控制
       void Trans(Graphics g,int j,int k,int transform){
                 if(transform==0){//正常
                     g.setClip(x,y,width,height);
                     g.drawImage(sprite,j,k,0);
                                 }
                if(transform==1){//水平
                  for(int i=0;i<height;i++){
                    g.setClip(x+i,y,1,height);
                    g.drawImage(sprite,j-width+2*i+1,k,0);                  
                               } }
                if(transform==2){//垂直
                  for(int i=0;i<width;i++){
                    g.setClip(x,y+i,width,1);
                    g.drawImage(sprite,j,k-height+2*i+1,0);
                   }  }               }
      void setTransform(int transform){//設定翻轉1是水平2是垂直0是正常
                 this.transform=transform;
                 if(this.transform>2|this.transform<0)
                 this.transform=0;           }
       ////////////////////////////////
       ////////////擷取圖片////////////     
       ///////////////////////////////
Image tupian;      
     Image getImage(String lujing) {
    
       try {
          tupian=Image.createImage("/"+lujing+".png");
           }catch(Exception e) {}
        return tupian;                         }
            
        } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.