⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sprite.java

📁 一款直版动作过关游戏
💻 JAVA
字号:
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class sprite extends layer{
    
    Image ThisImage;
    Image NONEImage;
    Image transformImage;
    int frameWidth,frameHeight;
    int framesCount;
    int rows,cols;
    int framePieceX[],framePieceY[];
    int FrameIndex;
    int[] currentFrameSequence;
    

    
    public sprite(Image image,int fWidth,int fHeight,int transformIndex){
        super(fWidth, fHeight);
        if(image.getWidth()%fWidth!=0 || image.getHeight()%fHeight!=0)
    	{
//        if(fWidth<1||fHeight<1 ){
            throw new IllegalArgumentException();
//        }
        }

        ThisImage = image;
        this.frameWidth = fWidth;
        this.frameHeight = fHeight;
        initializeFrames(ThisImage);
    }
    public void setImage(Image img,int fWidth,int fHeight)
    {
    	 if(fWidth<1||fHeight<1){
             throw new IllegalArgumentException();
         }
         ThisImage = img;
         this.frameWidth = fWidth;
         this.frameHeight = fHeight;
         initializeFrames(ThisImage);
    }  
    //画
    public void paint(Graphics g){
        if(g==null)
            throw new NullPointerException();
        g.setClip(x,y,frameWidth,frameHeight);
        g.drawImage(ThisImage, x - framePieceX[currentFrameSequence[FrameIndex]],
                y - framePieceY[currentFrameSequence[FrameIndex]],Graphics.TOP|Graphics.LEFT);
        //这里请输入你屏幕的尺寸
        g.setClip(0,0,176, 208);
    }
    
    public void initializeFrames(Image image){
        if(image==null)
            throw new NullPointerException();
//        frameWidth = fWidth;
//        frameHeight = fHeight;
        rows = image.getWidth() / frameWidth;
        cols = image.getHeight() / frameHeight;
        framesCount = rows*cols;
        framePieceX = new int[framesCount];
        framePieceY = new int[framesCount];
        currentFrameSequence = new int[framesCount];
        //为图片开辟空的CELL X,Y坐标
        for(int i=0;i<cols;i++){
            for(int j=0;j<rows;j++){
                int tempint = i*rows+j;
                framePieceX[tempint] = j*frameWidth;
                framePieceY[tempint] = i*frameHeight;
                currentFrameSequence[tempint] = tempint;
            }
        }
    }
    
    //设置当前帧
    public void setFrame(int sIndex){
        if(sIndex<0||sIndex>currentFrameSequence.length)
            throw new IndexOutOfBoundsException();
        FrameIndex = sIndex;
    }
    
    //得到当前帧
    public int getFrame(){
        return FrameIndex;
    }
    
    //设置当前帧序列
    public void setFrameSequence(int[] cFrameSequence){
        currentFrameSequence = cFrameSequence;
    }
    
    //得到当前帧序列
    public int[] getFrameSequence(){
        return currentFrameSequence;
    }
    
    //前一帧;
    public void prevFrame(){
        if(FrameIndex==0){
            FrameIndex = currentFrameSequence.length-1;
        }else{
            FrameIndex--;
        }
    }
    //下一帧;
    public void nextFrame(){
        if(FrameIndex==currentFrameSequence.length-1){
            FrameIndex = 0;
        }else{
            FrameIndex++;
        }
    }
    
    //矩形碰撞;
    public boolean collidesWith(sprite sp,boolean pixelLevel,int excursionX,int excursionY){
        if(!visible || !sp.visible)
            return false;
        //记录自己最右,下边的x,y坐标
        int myRightX = x + frameWidth;
        int myDownY = y + frameHeight;
        //记录自己最左,上边的x,y坐标
        int myLeftX = x;
        int myUpY = y;
        //记录碰撞精灵最右,下边的x,y坐标
        int sRightX = sp.x + excursionX;
        int sDownY = sp.y + excursionY;
//        int sRightX = sp.x + sp.frameWidth;
//        int sDownY = sp.y + sp.frameHeight;
        //记录碰撞精灵最左,上边的x,y坐标;
        int sLeftX = sp.x;
        int sUpY = sp.y;
        //如果是象素检测;
        if(pixelLevel){
            
        //如果不是象素检测;
        }else{
            
            //此精灵最右边在s精灵的最左,上边还左,上,那么肯定没有碰撞;
            if(myRightX<=sLeftX || myDownY<=sUpY || sRightX<=myLeftX || sDownY<=myUpY)
                return false;
        }
        return true;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -