procedure TForm1.DGCScreen1Initialize(Sender: TObject); : : //Create the Animations. Note: Here we are just defining animations //not the sprites. The Animation name is optional (the first paramter). //The second paramter is LoopAnimation flag. DGCSpriteMgr1.Animations.Add('Invader', True); //We have added an animation (Index 0) so set the images for the animations. //If we want to use the same images for all 32 directions we use the //SetAllDirections method. 100=Speed, [1, 2]=Image Numbers DGCSpriteMgr1.Animations[0].SetAllDirections(100, [1, 2]); |
//Define the animations for the player controlled ship and explosion. Again //we are using the same images for all 32 directions so we use the //SetAllDirections method. DGCSpriteMgr1.Animations.Add('Ship', true); DGCSpriteMgr1.Animations[1].SetAllDirections(100, [0]); //For the explosion event we say not to loop because and event is fired //when the animation has reached the last frame. This will be our cue to //remove the player controlled ship as it has just been blown up. DGCSpriteMgr1.Animations.Add('Explode', False); DGCSpriteMgr1.Animations[2].SetAllDirections(100, [3, 4, 5, 6]); |
//Now we have defined the animations is is time to create the sprites. //Start with the player controlled ship. Note the Allow???? properties //are used so the sprite can only move left and right //Params=ID, X, Y, Direction (0=North), Speed, Animation# (Defined above) DGCSpriteMgr1.Sprites.AddPlayer8(0, 320, 440, 0, 3, 1); with DGCSpriteMgr1.Sprites[0] as TDGCPlayer8 do begin AllowUp := False; //Not now allow up AllowDown := False; //Do not allow down Acceleration := 0.1; //Do not allow the ship to reach maximum Decceleration := 0.1; //speed and stop instantly. //Each sprite can moved within a defined rectangle held in property //'Limits'. When the sprite reaches the edge of the rectangle an action //can occur. For example, when the sprites reaches the side of the //limits rectangle you can make it bounce off the side or just stop. //An action can be set for each side of the Limits rectangle. //If you want the same action for all four sides of the Limits //rectangle use the AllActions property. The Limits rectangle will default //to the entire screen. To set an action for just one side use the //LeftAction/TopAction,RightAction/BottomAction properties. //Note: Some of the Limits Actions values are ignored for player //controlled sprites. Try changing the AllActions property for //the bouncer sprite created next. AllActions := laWrap; //laWrap = Wrap from one side to the other //laBounce = Bounce off the side //laReverse = Reverse direction //laStopInside = Stop inside the rectangle //laStopOutside = Stop outside the rectangle //laEvent = Fire the OnSpriteEvent end; |
その他の Add メソッド AddBouncer AddRaceCar AddThruster AddStatic AddJumperそれぞれのパラメータは、スプライトの ID 、スプライトの初期座標、スプライトの向き、スプライトの動く速度(同時に最大速度も決まる)、スプライトが使うアニメーションの ID (先ほど定義したもの)である。今回は全方向とも同じイメージを使うようにアニメーションを指定しているので、スプライトの向きはいくつでも関係ない。また、先ほど定義したスターシップのアニメーションは Animations[1] だったので、最後は1となる。
with DGCSpriteMgr1.Sprites[0] as TDGCPlayer8 do |
if DGCSpriteMgr1.Sprites[0] is TDGCPlayer8 do |
最後に AllActions である。まとめて意訳してみると、 laWrap = 領域の端までいくと、反対側にワープする。 laBounce = 領域の端までいくと、スプリングがあり、跳ね返される。 laReverse = 領域の端までいくと、その方向の速度が反転し、跳ね返る。 laStopInside = 領域の内側までいくと止まる。 laStopOutside = 領域の外側までいくと止まる。(要注意) laEvent = 領域を出たところで OnSprite イベントが発生する。laBounce については、説明しにくいので実際に試してみると良い。なお、ここで領域とは、Sprites の Limits プロパティであり、デフォルトでは全画面が選択されている。また、AllActions プロパティの代わりに LeftAction/TopAction/RightAction/BottomAction を使うことによって、それぞれの領域での挙動を指定できる。
//Create the Invader sprite (TDGCBouncer) DGCSpriteMgr1.Sprites.AddBouncer(0, 90, 300, 14, 5, 0); with DGCSpriteMgr1.Sprites[1] as TDGCBouncer do begin Limits := Rect(0, 200, 640, 468); Acceleration := 0.01; //Invader will increase speed slowly AllActions := laBounce; //try changing this to laWrap, laReverse, //laStopinside or laStopOutside. end; |
//Set the clipping region to the area we are drawing the sprite in DGCScreen1.ClipRect := Rect(0, 200, 640, 468); TraceString('Initialise'); //Now Start the page flipping DGCScreen1.FlippingEnabled := True; |
TraceString('Initialise'); |
procedure TForm1.DGCScreen1Flip(Sender: TObject); var ShipHit: Boolean; begin //Erase the area theat the sprites will move in //This is also our screen clipping region DGCScreen1.Back.EraseRect(Rect(0, 200, 640, 468), 0); //Draw the sprites DGCSpriteMgr1.Draw; //Update the sprites DGCSpriteMgr1.Update; |
//Is there a collision between sprite 0 and 1 (the invader and player ship)? if (DGCSpriteMgr1.Sprites.Collision(0, 1)) and (DGCSpriteMgr1.Sprites[0].Animation <> 2) then begin //There is a collision so make the player sprite explode with DGCSpriteMgr1.Sprites[0] as TDGCPlayer8 do begin //A Player8 sprite has two types of animations //one for when it's moving when the keys are //help down and one when it's idle. ActiveAnimation := 2; IdleAnimation := 2; Automatic := False; //Disable automatic key checks end; //Make an explosion noise. Replay is the same as //using the Stop, Position := 0, and Play commands. DGCAudio1.Sound[0].Replay; //As well as making the player explode lets send the invader //wizzing off in a random direction. The AllActions property //is set to laStopOutside which means the invader will stop //when is completely leavs the LimitsRect. with DGCSpriteMgr1.Sprites[1] as TDGCBouncer do begin Randomize; Direction := Random(32); MaxSpeed := 5; //Set the maximum speed Speed := 5; //Set the current speed AllActions := laStopOutSide; end; end; |
//Make an explosion noise. Replay is the same as //using the Stop, Position := 0, and Play commands. DGCAudio1.Sound[0].Replay; |
//As well as making the player explode lets send the invader //wizzing off in a random direction. The AllActions property //is set to laStopOutside which means the invader will stop //when is completely leavs the LimitsRect. with DGCSpriteMgr1.Sprites[1] as TDGCBouncer do begin Randomize; Direction := Random(32); MaxSpeed := 5; //Set the maximum speed Speed := 5; //Set the current speed AllActions := laStopOutSide; end; |
procedure TForm1.DGCSpriteMgr1AnimationEnd(Sprite: TDGCSprite); begin //In this particular demo this event is called when the explosion //animation as reached the end. The OnAnimationEnd event is only //called if the animation is not looped. The only thing that blows //up in this demo is the player so hide and disable it. Although //you would normally decrease the player lives and do stuff like that. DGCSpriteMgr1.Sprites[0].Hide; DGCSpriteMgr1.Sprites[0].Disable; end; |
procedure TForm1.DGCSpriteMgr1SpriteDirChange(Sprite: TDGCSprite; LimitsSide: TLimitsSide); begin //Make a boing! noise when the invader hits the limits rectangle if Sprite is TDGCBouncer then DGCAudio1.Sound[1].Replay; end; |