Saturday 5 May 2012

A conveyor belt of Spawning a horizontal Sprite

A Horizontal spawning Sprite is similar to a patrolling platform sprite in it’s behavior and also similar to a falling sprite. But simpler.

A simple algorithm for a collection of Spawning sprites is as follows.

  1. Set up a timer to control when to Spawn the next sprite. The class that creates the Spawning sprite collection (Game1 here) controls this timer.
  2. The Spawning Sprite object has a simple state that is either Spawned or not. So that is Boolean.
  3. An Un-spawned sprite will not be updated or drawn.
  4. The objects are spawned off screen to the left and have a target off screen to the right.
  5. While they are spawned they just move to the target position at a certain speed.
  6. When they get to the target position Spawned is set to false and they return to the starting position waiting to be Spawned again when the timer goes off.

So in a new Game Class we create an array to hold our Spawning Objects. Our Base classes are as we previously used

image

 


The Spawning enemy class variables and constructor is as follows


class Spawn : Enemy
{
Vector2 EndPosition;
public bool Spawned;
public Spawn(Game g, Texture2D texture,
Vector2 Position1, Vector2 Position2,
int framecount) : base(g,texture,Position1,framecount)
{
// start, Target and End positions are defined in Enemy
// as most if not all enemies will have them
startPosition = Position1;
TargetPosition = EndPosition = Position2;
Spawned = false;
}

public override void Update(GameTime gt)
{
if (Spawned)
{
// Move X * Velocity as we are moving Horizontally
position += new Vector2(1, 0) * Velocity;
if (Vector2.Distance(position, EndPosition) < Velocity)
{
position = startPosition;
TargetPosition = EndPosition;
Spawned = false;
}
}
base.Update(gt);

}


When a Spawn is created it is initially false and it is activated by the timer in the Game1 class. The update of the Spawn just moves it horizontally. You could do the same vertically. When it reaches the target. It is reset to the start position Spawned is set to false this will cause it to be activated when the timer next goes off in Game1.



Now to Game1



The class variables



Arrays are used here as other collections have not been covered in First year programming



const float MAXSPWAN = 1000;
float SpawnTime = MAXSPWAN; // milliseconds
Spawn[] cokeBottles;


protected override void Initialize()
{
// TODO: Add your initialization logic here
cokeBottles = new Spawn[5];
base.Initialize();
}


We fill the array in the Load Content method. The start Vector is off to the left of the viewport. The End and target position are off the right of the viewport



for(int i = 0; i < 5;i++)
cokeBottles[i] = new Spawn(this,
Content.Load<Texture2D>("CokeBottle"),
new Vector2(-30, 300),
new Vector2(GraphicsDevice.Viewport.Width + 100, 300),1);


So now we have an array of 5 Spawn objects. The Update of Game1 controls the Spawning of a sprite based on the Timer.




if (SpawnTime > 0)
//Count down the spawn time
SpawnTime -= gameTime.ElapsedGameTime.Milliseconds;
else
{
// reset the spawn time
SpawnTime = MAXSPWAN;
// if any of the Spawn reach the target
// which is off to the right of the viewport
// then the Spawn will set it's
// Spawned property to False
foreach (Spawn s in cokeBottles)
if (!s.Spawned)
{
s.Spawned = true;
break; // This is Key only one spawned at a time
}
}
// Normal Update Spawned or not
foreach (Spawn s in cokeBottles)
s.Update(gameTime);



The Draw for Game1 just draws the sprite collection



foreach (Spawn s in cokeBottles)
s.Draw(spriteBatch);


and that’s it

4 comments:

  1. This blog is giving all good and related information on conveyor belt manufacturer

    ReplyDelete
  2. Warm Greetings!



    Today, I visit your website and after reading your blog i realize that it is very informative. I'm highly impressed to see the comprehensive resources being offered by your site.


    Thanks and Regards




    Belt Conveyors

    ReplyDelete
  3. Thanks for uploading this useful information for conveying belt system, manufacturing of conveyor belt.

    ReplyDelete
  4. Hello,
    This is nice Information. Thank you very much.
    Mahajan Conveyors manufacturers Conveyor Belts, Transmission Rubber Belts, Rubber Sheets, conveyor belts manufacturers, Rubber Belting, Elevator Conveyor Belts, Rubber Conveyor Belts, Industrial Conveyor Belts, Conveyor Rubber Belts, Flat Transmission Rubber Belts.

    ReplyDelete