基于MIDP1.0的模仿Sprite类

感觉MIDP2中的Sprite类很好用,而目前开发手机程序还是MIDP1为主流,所以做此类,用起来还是比较方便的,呵呵
该类处于改进中,请关注本站最新版本
--------------------------

package theman;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import javax.microedition.lcdui.* ;
public class MySprite {
private Image image;
private int positionX;
private int positionY;
private int frameWidth;
private int frameHeight;
private int numFrames;
private int currentFrame;
public boolean visible=true;
public int DX;
public int DY;
public int collideX=0;//当两个角色交叉横向距离超过collideX时认为已经碰撞
public int collideY=0;//当两个角色交叉纵向距离超过collideY时认为已经碰撞
public int collideArea=0;//当两个角色互相入侵面积超过collideArea时认为已经碰撞(注意:这里的入侵面积!=交叉面积)

public MySprite(Image image, int frameWidth, int frameHeight, int numFrames) throws Exception {
    this.image = image;
    this.frameWidth = frameWidth;
    this.frameHeight = frameHeight;
    this.numFrames = numFrames;
    this.currentFrame=0;
}

public int getX() {
    return this.positionX;
}

public void setX(int positionX) {
    this.positionX = positionX;
}

public int getY() {
    return this.positionY;
}

public int getFrameWidth()
{
    return this.frameWidth;
}

public int getFrameHeight()
{
return this.frameHeight;
}
 
public void setY(int positionY) {
    this.positionY = positionY;
}

public void setCurrentFrame(int current) {
    this.currentFrame = current;
}

public void draw(Graphics g,MainCanvas theCanvas)
{
    if (visible)
    {
        g.clipRect(getX(), getY(), frameWidth, frameHeight);
        g.drawImage(image, getX() - currentFrame * frameWidth, getY(),
            g.TOP | g.LEFT);
        g.setClip(0, 0, theCanvas.getWidth(), theCanvas.getHeight());
    }
}

public void step()
{
    this.positionX+=DX;
    this.positionY+=DY;
}
 
public boolean collidesWith(MySprite sprite)
{
    return this.collidesWith(sprite,false);
}

public boolean collidesWith(MySprite sprite, boolean CheckType) 
{
    if (! visible || ! sprite.visible)
    {
        return false;
    }
    if (CheckType)
    {
        //当两个角色互相入侵面积超过collideArea时认为已经碰撞(注意:这里的入侵面积!=交叉面积)
        int inX=sprite.getX()+sprite.frameWidth-this.getX()
                    >this.getX()+this.frameWidth-sprite.getX()
                    ?this.getX()+this.frameWidth-sprite.getX()
                    :sprite.getX()+sprite.frameWidth-this.getX() 
                    ;
        int inY=sprite.getY()+sprite.frameHeight-this.getY()
                    >this.getY()+this.frameHeight-sprite.getY()
                    ?this.getY()+this.frameHeight-sprite.getY()
                    :sprite.getY()+sprite.frameHeight-this.getY() 
                    ;
        if (inX>0 && inY>0 && inX*inY>this.collideArea+sprite.collideArea)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        if (sprite.getX()+sprite.frameWidth-sprite.collideX>this.getX()
            && this.getX()+this.frameWidth-this.collideX>sprite.getX()
            && sprite.getY()+sprite.frameHeight-sprite.collideY>this.getY()     
            && this.getY()+this.frameHeight-this.collideY>sprite.getY())
        {
            return true; 
        }
        else
        {
            return false;
        }
    }
}
 
public boolean collidesWith(Image image, int x, int y)
{
    return this.collidesWith(image, x, y,false);
}

public boolean collidesWith(Image image, int x, int y, boolean CheckType)
{
    if (! visible)
    {
        return false;
    }

    if (CheckType)
    {
        //当两个角色互相入侵面积超过collideArea时认为已经碰撞(注意:这里的入侵面积!=交叉面积)
        int inX=x+image.getWidth()-this.getX()
        >this.getX()+this.frameWidth-x
        ?this.getX()+this.frameWidth-x
        :x+image.getWidth()-this.getX() 
        ;
        int inY=y+image.getHeight()-this.getY()
                    >this.getY()+this.frameHeight-y
                    ?this.getY()+this.frameHeight-y     
                    :y+image.getHeight()-this.getY() 
                    ;
        if (inX>0 && inY>0 && inX*inY>this.collideArea)
        {
            return true;
        }
        else
        {
            return false; 
        }
    }
    else     
    { 
        if (x+image.getWidth()>this.getX()
                && this.getX()+this.frameWidth-this.collideX>x
                && y+image.getHeight()>this.getY()
                && this.getY()+this.frameHeight-this.collideY>y) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
}

-----------------------------------------
飘飘何所似?天地一沙鸥。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值