6 Replies - 1029 Views - Last Post: 31 July 2012 - 08:24 AM Rate Topic: -----

#1 pkaff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-July 12

How to let player collect coin on collision

Posted 29 July 2012 - 04:22 AM

I'm currently working on a platformer game in 2d (sort of super-mario style). I've made a collision that works fine for what I'm trying to achieve, but whenever I try to give the player coins for colliding from beneath a block (like in super mario) - the maximum of coins the player is able to get are 2. Also it seems that some of the blocks don't get the coins inserted correctly.

            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

Is This A Good Question/Topic? 0
  • +

Replies To: How to let player collect coin on collision

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1923
  • View blog
  • Posts: 5,732
  • Joined: 05-May 12

Re: How to let player collect coin on collision

Posted 29 July 2012 - 10:00 AM

Did you check to how big player.headBounds it? Did you check to make sure that map.blocksBot has more than 2 rectangles?

Based on the code you've posted above and your description of behavior, the player.headBounds just intersects with two blocks.
Was This Post Helpful? 0
  • +
  • -

#3 pkaff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-July 12

Re: How to let player collect coin on collision

Posted 29 July 2012 - 06:18 PM

View PostSkydiver, on 29 July 2012 - 10:00 AM, said:

Did you check to how big player.headBounds it? Did you check to make sure that map.blocksBot has more than 2 rectangles?

Based on the code you've posted above and your description of behavior, the player.headBounds just intersects with two blocks.


Thanks! Actually did some debugging on the loop, and found out that the count variable was plussed in the wrong place. Moving it out one } further made it work!
Was This Post Helpful? 0
  • +
  • -

#4 pkaff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-July 12

Re: How to let player collect coin on collision

Posted 30 July 2012 - 05:16 AM

I actually have a follow up question on this. I keep getting another problem now - the total number of coins (Stored as a static property in the block class) doesn't add up correctly. I sometimes get negative number of coins left, and sometimes there are coins that can't be found. The ground should not be able to contain coins with this code:
        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(2) == 1 && position.Y <= 675)
            {
                containsCoin = true;
            }
            if (containsCoin)
            {
                nbrOfCoins = rand.Next(3);
                totalNbrOfCoins += nbrOfCoins;
            }


The window is 720 pixels high, blocks are 50 pixels.

I have attached the updated program and would be really grateful if someone could take a look and see if they can find the problem. It's either in the Block/Invisibleblock class or the HandleCoin method in level1state class.

/Pkaff
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1923
  • View blog
  • Posts: 5,732
  • Joined: 05-May 12

Re: How to let player collect coin on collision

Posted 30 July 2012 - 06:54 PM

Are you sure you aren't overflowing your int:
            int i = int.MaxValue - 5;
            Console.WriteLine(i);
            for (int x = 0; x < 10; ++x)
                ++i;
            Console.WriteLine(i);


Was This Post Helpful? 0
  • +
  • -

#6 pkaff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-July 12

Re: How to let player collect coin on collision

Posted 31 July 2012 - 02:30 AM

Which int, the count?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1923
  • View blog
  • Posts: 5,732
  • Joined: 05-May 12

Re: How to let player collect coin on collision

Posted 31 July 2012 - 08:24 AM

I was referring to the number of coins:

View Postpkaff, on 30 July 2012 - 05:16 AM, said:

I actually have a follow up question on this. I keep getting another problem now - the total number of coins (Stored as a static property in the block class) doesn't add up correctly. I sometimes get negative number of coins left, and sometimes there are coins that can't be found.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1