![](https://csdnimg.cn/release/download_crawler_static/20686216/bg1.jpg)
TImageSprite 精灵图像类详解 DelphiX 编程交流群 852532488 第 1 页 共 4 页
TImageSprite = class(TSprite)
TImageSprite精灵图像类详解
Private //以下补充定义了 17 个数据。
FAnimCount: Integer; //动画格数
FAnimLooped: Boolean; //动画是否循环播放
FAnimPos: Double; //动画位置,参考 DXImageList
FAnimSpeed: Double; //动画速度
FAnimStart: Integer; //动画起始哪格
FPixelCheck: Boolean; //碰撞时做象素检查,否则只做矩形比较
FTile: Boolean; //精灵矩形大于图像情况下,是否重复贴图,默认=false
FTransparent: Boolean; //是否透明,默认=true,颜色 0 为透明色
FAngle: Single; //绘制图像倾角,默认=0 不倾斜
FAlpha: Integer; //混合比例,默认=255 最大
FBlendMode: TRenderType; //混合模式,默认=rtDraw
FCenterX: Double; //中心点 X
FCenterY: Double; //中心点 Y
FBlurImageArr: TBlurImageArr; //模糊图像数组,默认=(0,…,0)
FBlurImage: Boolean; //是否模糊图像,默认=false
FMirrorFlip: TRenderMirrorFlipSet; //镜像或者翻转模式,默认=[]
FTextureFilter: TD2DTextureFilter; //贴图过滤器,默认=D2D_POINT
function GetDrawImageIndex: Integer;
function GetDrawRect: TRect;
function ImageCollisionTest(suf1, suf2: TDirectDrawSurface;
const rect1, rect2: TRect; x1, y1, x2, y2: Integer;
DoPixelCheck: Boolean): Boolean;
function StoreCenterX: Boolean;
function StoreCenterY: Boolean;
function StoreAlpha: Boolean;
procedure SetBlurImage(const Value: Boolean);
procedure SetBlurImageArr(const Value: TBlurImageArr);
function GetImage: TPictureCollectionItem;
procedure SetMirrorFlip(const Value: TRenderMirrorFlipSet);
protected
…(省略 Read***、Write*** 声明)…
procedure LoadImage; virtual;
procedure DoDraw; override;
procedure DoMove(MoveCount: Integer); override;
function GetBoundsRect: TRect; override;
function TestCollision(Sprite: TSprite): Boolean; override;
procedure SetImage(AImage: TPictureCollectionItem); virtual;
public
constructor Create(AParent: TSprite); override;
procedure Assign(Source: TPersistent); override;
procedure ReAnimate(MoveCount: Integer); override;
property Image: TPictureCollectionItem read GetImage write SetImage;
property BlurImageArr: TBlurImageArr read FBlurImageArr write SetBlurImageArr;
{un-published property}
property BlendMode: TRenderType read FBlendMode write FBlendMode default
rtDraw;
property Angle: Single read FAngle write FAngle stored StoreAlpha;
property Alpha: Integer read FAlpha write FAlpha default $FF;
property CenterX: Double read FCenterX write FCenterX stored StoreCenterX;
property CenterY: Double read FCenterY write FCenterY stored StoreCenterY;
property AnimCount: Integer read FAnimCount write FAnimCount default 0;
property AnimLooped: Boolean read FAnimLooped write FAnimLooped default False;
property AnimPos: Double read FAnimPos write FAnimPos;
property AnimSpeed: Double read FAnimSpeed write FAnimSpeed;
property AnimStart: Integer read FAnimStart write FAnimStart default 0;
property PixelCheck: Boolean read FPixelCheck write FPixelCheck default False;
property Tile: Boolean read FTile write FTile default False;
property BlurImage: Boolean read FBlurImage write SetBlurImage default False;
property MirrorFlip: TRenderMirrorFlipSet read FMirrorFlip write SetMirrorFlip
default [];
property TextureFilter: TD2DTextureFilter read FTextureFilter write FTextureFilter
default D2D_POINT;
published
property DXImageList; //图像列表
property DXImageName; //正在使用的图像的图片名,LoadImage 方法中设置
//公开 4 个事件接口:绘制、移动、碰撞、获取图片 ↓
property OnDraw; //绘制事件,DoDraw 方法中显式调用
property OnMove;// 移动事件,DoMove 方法中显式调用
property OnCollision;// 碰撞事件,DoCollision 方法中显式调用
property OnGetImage; //获取图片事件,LoadImage 方法中显式调用
end;
constructor TImageSprite.Create(AParent: TSprite);
begin //★ 重载:创建对象
inherited Create(AParent);
FTransparent := True;
FAlpha := 255;
FAngle := 0;
FBlendMode := rtDraw;
FCenterX := 0.5;
FCenterY := 0.5;
FBlurImage := False;
FillChar(FBlurImageArr, SizeOf(FBlurImageArr), 0);
FTextureFilter := D2D_POINT;
end;
procedure TImageSprite.SetImage(AImage: TPictureCollectionItem);
begin //☆ 虚方法:设置精灵的图像
FDXImage := AImage;
FDXImageName := '';
if AImage <> nil then begin
Width := AImage.Width;
Height := AImage.Height;
FDXImageName := FDXImage.Name;
end else begin
Width := 0;
Height := 0;
end;
end;
function TImageSprite.GetBoundsRect: TRect;
var dx, dy: Integer; //★ 重载:获得本图像精灵的屏幕坐标 (-5,-10,250,200)
begin
dx := Round(WorldX);
dy := Round(WorldY);
if FTile then begin
dx := Mod2(dx, FEngine.SurfaceRect.Right + Width);
dy := Mod2(dy, FEngine.SurfaceRect.Bottom + Height);
if dx > FEngine.SurfaceRect.Right then
dx := (dx - FEngine.SurfaceRect.Right) - Width;
if dy > FEngine.SurfaceRect.Bottom then
dy := (dy - FEngine.SurfaceRect.Bottom) - Height;
end;
Result := Bounds(dx, dy, Width, Height);
end;
procedure TImageSprite.DoMove(MoveCount: Integer);
begin //★重载:Move 移动显式调用 DoMove 调用用户代码
if AsSigned(FOnMove) then
FOnMove(Self, MoveCount)
else begin
ReAnimate(MoveCount);
end;
end;
function TImageSprite.GetDrawImageIndex: Integer;
begin //☆ 获得精灵的当前图像的索引,在 DoDraw、TestCollision 显式调用
Result := FAnimStart + Trunc(FAnimPos); //solve 1.07f to Round()
end;
function TImageSprite.GetDrawRect: TRect;
begin //☆ 精灵的绘制矩形,在 TestCollision 碰撞检测中调用
Result := BoundsRect;
OffsetRect(Result, (Width - Image.Width) div 2, (Height - Image.Height) div 2);
end;