![](https://csdnimg.cn/release/download_crawler_static/20686230/bg1.jpg)
TBackStageSprite 背景精灵类详解 DelphiX 编程交流群 852532488 第 1 页 共 4 页
TBackStageSprite = class(TImageSprite) //背景精灵、背景地图
TBackStageSprite背景精灵类详解
Private //本文的图章块、切片块、单元格 是同一个意义
FScrollEdge: Integer; //边缘快速滚动象素
FXScroll: Integer; //鼠标按左右方向键滚动象素
FYScroll: Integer; //鼠标按上下方向键滚动象素
FMap: Pointer; // TMapType 图章块信息指针
FMapWidth: Integer; //横向图章块数目,X 方向切片块数目
FMapHeight: Integer; //纵向图章块数目,Y 方向切片块数目
FChipsRect: TRect; //正在绘制的图章块的矩形坐标(含偏移)
FChipsPatternIndex: Integer; //正在绘制的图章块索引的图章序号(0~N-1)
function GetCollisionMapItem(X, Y: Integer): Boolean;
function GetChip(X, Y: Integer): Integer;
procedure SetChip(X, Y: Integer; Value: Integer);
procedure SetCollisionMapItem(X, Y: Integer; Value: Boolean);
procedure SetMapHeight(Value: Integer);
procedure SetMapWidth(Value: Integer);
function GetCollisionRectItem(X, Y: Integer): TRect;
function GetMap(X, Y: Integer): TMapType;
function GetTagMap(X, Y: Integer): Integer;
procedure SetCollisionRectItem(X, Y: Integer; Value: TRect);
procedure SetMap(X, Y: Integer; Value: TMapType);
procedure SetTagMap(X, Y, Value: Integer);
function GetOverlap(X, Y: Integer): Integer;
procedure SetOverlap(X, Y: Integer; const Value: Integer);
protected
procedure ReadMapData(Stream: TStream);
procedure WriteMapData(Stream: TStream);
procedure DoDraw; override;
function GetBoundsRect: TRect; override;
function TestCollision(Sprite: TSprite): Boolean; override;
procedure SetImage(Img: TPictureCollectionItem); override;
procedure DefineProperties(Filer: TFiler); override;
procedure DoMove(MoveCount: Integer);override;
public
constructor Create(AParent: TSprite); override;
destructor Destroy; override;
procedure SetMapSize(AMapWidth, AMapHeight: Integer);
function IsMapEmpty: Boolean;
property Chips[X, Y: Integer]: Integer read GetChip write SetChip;
property CollisionMap[X, Y: Integer]: Boolean read GetCollisionMapItem write
SetCollisionMapItem;
property CollisionRect[X, Y: Integer]: TRect read GetCollisionRectItem write
SetCollisionRectItem;
property Overlap[X, Y: Integer]: Integer read GetOverlap write SetOverlap;
property TagMap[X, Y: Integer]: Integer read GetTagMap write SetTagMap;
property Map[X, Y: Integer]: TMapType read GetMap write SetMap;
procedure Assign(Source: TPersistent); override;
property ChipsRect: TRect read FChipsRect write FChipsRect;
property ChipsPatternIndex: Integer read FChipsPatternIndex write FChipsPatternIndex
default 0;
property MapHeight: Integer read FMapHeight write SetMapHeight;
property MapWidth: Integer read FMapWidth write SetMapWidth;
property ScrollEdge: Integer read FScrollEdge;
property XScroll: Integer read FXScroll write FXScroll;
property YScroll: Integer read FYScroll write FYScroll;
published
property DXImageList;
property DXImageName;
property OnDraw;
property OnMove;
property OnCollision;
property OnGetImage;
end;
PMapType = ^TMapType; //本类用到的图章块信息记录 摘自 DXSprite.pas
TMapType = packed record //图章块信息记录
MapChip: Integer; //Y 行 X 列图章块的索引
//ImageName: string[127];
CollisionChip: Boolean; //是否碰撞砖
CollisionRect: TRect; //碰撞区域,可与图章块区域不同
Overlap: Integer; //重叠,震动图像,如同缩放 for pulse image, like zoom etc.
AnimLooped: Boolean; //是否循环播放,图章块可以是活动图像
AnimStart, AnimCount: Integer; //循环播放开始序号、播放图像数
AnimSpeed, AnimPos: Double; //循环播放速度、位置
Rendered: TRenderType; //渲染、混合类型
Alpha: Byte; //混合级别
Angle: Single; //图像输出角度
CenterX, CenterY: Double; //中心点坐标 XY
MirrorFlip: TRenderMirrorFlipSet; //镜面翻转,左右或者上下镜像
TextureFilter: TD2DTextureFilter; //贴图过滤形式
Tag: Integer; //标记整数
end;
constructor TBackStageSprite.Create(AParent: TSprite);//★ 创建对象
begin //构造方法
inherited Create(AParent);
FMap := nil;
FMapWidth := 0;
FMapHeight := 0;
Collisioned := False;
FScrollEdge:=30;
end;
destructor TBackStageSprite.Destroy; //★ 析构对象
begin //析构方法
SetMapSize(0, 0);
inherited Destroy;
end;
procedure TBackStageSprite.DoMove(MoveCount: Integer); //★ 自身移动处理
begin //TSprite.Move 方法调用此方法
inherited DoMove(MoveCount); //调用父类处理,里面有调用事件 OnMove 代码
if (XScroll<0)and(800-X-XScroll<=Width) then //向右滚动
X := X + XScroll;
if (XScroll>0)and(X+XScroll<=0) then //向左滚动
X := X + XScroll;
if (YScroll<0)and(600-Y-YScroll<=Height) then //向下滚动
Y := Y + YScroll;
if (YScroll>0)and(Y+YScroll<=0) then //向上滚动
Y := Y + YScroll;
YScroll:=0;
XScroll:=0;
PostMessage(Application.MainForm.Handle,WM_SCROLL_MAP_DONE,0,0);
end;
procedure TBackStageSprite.DoDraw; ////★ 自身绘制处理
var //最重要的方法:按照单元格绘制
TmpX, TmpY, cx, cy, cx2, cy2, PatternIndex, ChipWidth, ChipHeight: Integer;
StartX, StartY, EndX, EndY, StartX_, StartY_, OfsX, OfsY, dWidth, dHeight: Integer;
ptRect: TRect; Q: TMapType;
begin
LoadImage; //载入图片
if Image = nil then
Exit;
if (FMapWidth <= 0) or (FMapHeight <= 0) then
Exit;
ptRect := Image.PatternRects[0];
ChipWidth := ptRect.Right - ptRect.Left;
ChipHeight := ptRect.Bottom - ptRect.Top;
dWidth := (Engine.SurfaceRect.Right + ChipWidth) div ChipWidth + 1;
dHeight := (Engine.SurfaceRect.Bottom + ChipHeight) div ChipHeight + 1;
TmpX := Round(WorldX);
TmpY := Round(WorldY);