int count = 0;
foreach (Rectangle bot in map.blocksBot)
{
foreach (Lucas player in players)
{
if (bot.Intersects(player.headBounds))
{
player.position.Y += 0.05f * gameTime.ElapsedGameTime.Milliseconds;
isFixed = true;
player.jumpSpeed = 0.1f;
player.UpdateBounds(gameTime);
HandleCoin(count, player, map);
count++;
}
}
}
What I'm sort of worried about here is the int count. Does it work the way that it's intended (the foreach loops the list in the order that the blocks were inserted to it, making this method work)?
public void HandleCoin(int count, Lucas player, Map map)
{
if (map.listOfBlocks[count].containsCoin && map.listOfBlocks[count].timeSinceLastCoin >= 1)
{
player.collectedCoins += 1;
map.listOfBlocks[count].RemoveCoins();
coinSoundInstance.Play();
map.listOfBlocks[count].timeSinceLastCoin = 0;
}
}
I'm printing the nbr of coins collected, so I know it works for up to 2 coins (aswell as I can hear the sound playing). If this seems correct there must be a problem with the insertion of the coins, but the code for that is in the constructor of the block:
public Block(Game1 game, Vector2 pos):
base(game)
{
totalBlocks++;
sprite = new Sprite(game.Content.Load<Texture2D>("Block"));
position = pos;
Random rand = new Random();
/*if (rand.Next(10) <= 5)
{
containsCoin = true;
}
if (containsCoin)
{
nbrOfCoins = rand.Next(4);
}
if (position.Y >= 700)
{
while (nbrOfCoins > 0)
{
RemoveCoins();
}
}*/
containsCoin = true;
nbrOfCoins = 1;
}
At the moment I'm testing, so the random-insertion is commented, and all blocks should contain a coin.
The whole program is in the attachment - move with arrow keys. There are invisible blocks in the beginning of the map aswell (these and the normal blocks are created in the map-class).
It seems like it should work for me, but I'm no experienced programmer either. I would appreciate some help on this!
/Pkaff

New Topic/Question
Reply



MultiQuote





|