School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,973 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,851 people online right now. Registration is fast and FREE... Join Now!




Help on XNA tutorial 2D ****RESOLVED****

 

Help on XNA tutorial 2D ****RESOLVED****

frostyraver

6 Nov, 2009 - 11:53 AM
Post #1

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 83



Thanked: 2 times
Dream Kudos: 50
My Contributions
Right, I have this game i've been mucking around with, you have a ball and collect things, watching out for baddies, to get to the end, basic stuff.

The problem i'm having is with the bricks deleteing and removing from the environment, they disappear, but are still physically there;

CODE

        /// <summary>
        /// Update the block to destroy here
        /// </summary>
        private void UpdateBlocks(GameTime gameTime)
        {
            for (int i = 0; i < blocks.Count; ++i)
            {
                Blocks bl = blocks[i];

                bl.Update(gameTime);

                if (bl.BoundingCircle.Intersects(Player.BoundingRectangle))
                {

                    blocks.RemoveAt(i--);
                    //blocks.Insert(i, 0);
                    //blocks.Insert(new Tile(null, TileCollision.Passable);
                    //Point position = GetBounds(x, y).Center;
                    //blocks.Add(new Blocks(this, new Vector2(position.X, position.Y)));
                    ////
                    //new Tile(null, TileCollision.Passable);
                }
            }


This loops through and removes the collisiond block, but it's not REMOVING, it's just removing the graphic.

Can anyone help me? I generally love figuring stuff out but I think this is a tad more complex.

This post has been edited by frostyraver: 10 Nov, 2009 - 02:10 AM

User is offlineProfile CardPM
+Quote Post


modi123_1

RE: Help On XNA Tutorial 2D ****RESOLVED****

6 Nov, 2009 - 12:27 PM
Post #2

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,835



Thanked: 80 times
Dream Kudos: 150
My Contributions
Are you trying to remove the object or just not have it interact with your game?
User is offlineProfile CardPM
+Quote Post

frostyraver

RE: Help On XNA Tutorial 2D ****RESOLVED****

6 Nov, 2009 - 03:11 PM
Post #3

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 83



Thanked: 2 times
Dream Kudos: 50
My Contributions
QUOTE(modi123_1 @ 6 Nov, 2009 - 12:27 PM) *

Are you trying to remove the object or just not have it interact with your game?


Well you see it's a ledge for my game and I want it gone, weather it be non interactive or removed.
User is offlineProfile CardPM
+Quote Post

Hellbroth

RE: Help On XNA Tutorial 2D ****RESOLVED****

7 Nov, 2009 - 07:52 AM
Post #4

D.I.C Head
**

Joined: 15 Aug, 2009
Posts: 53



Thanked: 1 times
My Contributions
I want to do a similar thing when i kill an enemy remove him complete but i can only do it with changing the coordinates of his position into someone that it's not in the screen although it's still there.

Is there any way to remove it?
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Help On XNA Tutorial 2D ****RESOLVED****

7 Nov, 2009 - 08:15 AM
Post #5

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 3,050



Thanked: 169 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
It is hard to tell just from the code you posted. Have you tried setting a break point at the line:

CODE

                    blocks.RemoveAt(i--);


To make sure that the code is reaching that point?
User is offlineProfile CardPM
+Quote Post

frostyraver

RE: Help On XNA Tutorial 2D ****RESOLVED****

8 Nov, 2009 - 11:31 PM
Post #6

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 83



Thanked: 2 times
Dream Kudos: 50
My Contributions
QUOTE(SixOfEleven @ 7 Nov, 2009 - 08:15 AM) *

It is hard to tell just from the code you posted. Have you tried setting a break point at the line:

CODE

                    blocks.RemoveAt(i--);


To make sure that the code is reaching that point?

Well here's my code, sorry for the cluster of files or tons of commented stuff out, i'm still learning and I tend to be messy.

Sorry to just GIVE you it but there tends to be a lengthy explination and it would be quicker for you to see what it's doing, and yes i've used a breakpoint smile.gif.

I might try moving them off screen but then that's just bad coding, and I want to learn this fix.
User is offlineProfile CardPM
+Quote Post

frostyraver

RE: Help On XNA Tutorial 2D ****RESOLVED****

9 Nov, 2009 - 01:35 AM
Post #7

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 83



Thanked: 2 times
Dream Kudos: 50
My Contributions
CODE

blocks.Remove(bl);

Does the exact same thing as
CODE

blocks.Remove(i--);


I'll update this as I go for others that maybe interested, biggrin.gif.

There MUST be a simple solution, THIS is what I love about programming.
User is offlineProfile CardPM
+Quote Post

frostyraver

RE: Help On XNA Tutorial 2D ****RESOLVED****

9 Nov, 2009 - 01:52 AM
Post #8

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 83



Thanked: 2 times
Dream Kudos: 50
My Contributions
WHAT we could do is, start the array at 1, leave 0 for a 'spare' tile, not sure to how to go about that.
User is offlineProfile CardPM
+Quote Post

lesPaul456

RE: Help On XNA Tutorial 2D ****RESOLVED****

9 Nov, 2009 - 07:56 AM
Post #9

D.I.C Regular
Group Icon

Joined: 16 Apr, 2009
Posts: 258



Thanked: 35 times
Dream Kudos: 175
My Contributions
What exactly is blocks? Obviously it's a collection. But a collection of what? Textures? A user-defined type?

What do you mean by remove the block? Remove the texture? Prevent the user from interacting with the object? Or do you mean you want to actually remove the object from memory (dispose of the object and all it's resources)?
User is offlineProfile CardPM
+Quote Post

frostyraver

RE: Help On XNA Tutorial 2D ****RESOLVED****

9 Nov, 2009 - 08:10 AM
Post #10

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 83



Thanked: 2 times
Dream Kudos: 50
My Contributions
QUOTE(lesPaul456 @ 9 Nov, 2009 - 07:56 AM) *

What exactly is blocks? Obviously it's a collection. But a collection of what? Textures? A user-defined type?

What do you mean by remove the block? Remove the texture? Prevent the user from interacting with the object? Or do you mean you want to actually remove the object from memory (dispose of the object and all it's resources)?


Well the block is made up of an object and a texture, I can make the texture disappear but not the psyical part of it.

Sorry i'm new to xna, I still can work out what stuff does, a large portion of it, i'm just stompt on this.
User is offlineProfile CardPM
+Quote Post

lesPaul456

RE: Help On XNA Tutorial 2D ****RESOLVED****

9 Nov, 2009 - 01:52 PM
Post #11

D.I.C Regular
Group Icon

Joined: 16 Apr, 2009
Posts: 258



Thanked: 35 times
Dream Kudos: 175
My Contributions
So the problem is that the block disapears, but the player still collides with it?
User is offlineProfile CardPM
+Quote Post

frostyraver

RE: Help On XNA Tutorial 2D ****RESOLVED****

9 Nov, 2009 - 08:12 PM
Post #12

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 83



Thanked: 2 times
Dream Kudos: 50
My Contributions
QUOTE(lesPaul456 @ 9 Nov, 2009 - 01:52 PM) *

So the problem is that the block disapears, but the player still collides with it?

Correct.
User is offlineProfile CardPM
+Quote Post

frostyraver

RE: Help On XNA Tutorial 2D ****RESOLVED****

10 Nov, 2009 - 02:08 AM
Post #13

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 83



Thanked: 2 times
Dream Kudos: 50
My Contributions
BREAK THROUGH!!!

I have worked out MOST of the kinks!

What you want to do is make a NEW class file called 'blocks', copy paste this code in it, yes it's pretty much the same as the gem without the movement
CODE

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace XNA_Platform
{
    /// <summary>
    /// A valuable item the player can collect.
    /// </summary>
    class Blocks
    {
        Random randy = new Random((int)DateTime.Now.Ticks);
        private Texture2D texture;
        private Vector2 origin;
        //private SoundEffect collectedSound;

        public const int PointValue = 30;
        public readonly Color Color = Color.Yellow;

        int textureNumber;

        // The block is animated from a base position along the Y axis.
        private Vector2 basePosition;

        public Level Level
        {
            get { return level; }
        }
        Level level;

        /// <summary>
        /// Gets the current position of this block in world space.
        /// </summary>
        public Vector2 Position
        {
            get
            {
                return basePosition + new Vector2(0.0f, 0.0f);
            }
        }

        /// <summary>
        /// Gets a circle which bounds this block in world space.
        /// </summary>
        public Circle BoundingCircle
        {
            get
            {
                return new Circle(Position, Tile.Width / 2.6f);
            }
        }

        /// <summary>
        /// Constructs a new block.
        /// </summary>
        public Blocks(Level level, Vector2 position)
        {
            this.level = level;
            this.basePosition = position;

            LoadContent();
        }

        /// <summary>
        /// Loads the block texture and collected sound.
        /// </summary>
        public void LoadContent()
        {
            textureNumber = randy.Next(1, 7);
            texture = Level.Content.Load<Texture2D>("Tiles/BlockA" + textureNumber);
            origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);
            //collectedSound = Level.Content.Load<SoundEffect>("Sounds/GemCollected");
            
        }

        /// <summary>
        /// Bounces up and down in the air to entice players to collect them.
        /// </summary>
        public void Update(GameTime gameTime)
        {
            // Bounce control constants


            // Bounce along a sine curve over time.
            // Include the X coordinate so that neighboring gems bounce in a nice wave pattern.            
            
        }

        /// <summary>
        /// Called when this gem has been collected by a player and removed from the level.
        /// </summary>
        /// <param name="collectedBy">
        /// The player who collected this block. Although currently not used, this parameter would be
        /// useful for creating special powerup gems. For example, a gem could make the player invincible.
        /// </param>
        public void OnCollected(Player collectedBy)
        {
            //Blocks..PointValue = 0;
            //collectedSound.Play();
        }

        /// <summary>
        /// Draws a gem in the appropriate color.
        /// </summary>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, Position, null, Color, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
        }
    }
}


Now we need to set up a destructable thing in the level class..I don't know the correct name for it in under 'instantiates a gem' code block you want to add this section of code

CODE

        /// <summary>
        /// Instantiates a destructable block and puts it in the level.
        /// </summary>
        private Tile LoadDestructble(int x, int y)
        {
            Point position = GetBounds(x, y).Center;
            blocks.Add(new Blocks(this, new Vector2(position.X, position.Y)));
            if (!blocks.Equals(null))
            {
                return new Tile(null, TileCollision.Passable);
            }
            else
            {
                return new Tile(null, TileCollision.Impassable);
            }
        }

What this does is sets the block to impassible by default and then passable if it has a null value aka destroyed.

Right now you want to scroll up at

CODE

private Tile LoadTile(char tileType, int x, int y)


and make the tile ".destructable", it should look like this:

CODE

                case '#':
                    return LoadDestructble(x, y);


You could ALWAYS use a different letter, I'm using this now.

Now we want to update it add this code under allt he other updates:
CODE

        /// <summary>
        /// Update the block to destroy here
        /// </summary>
        private void UpdateBlocks(GameTime gameTime)
        {

            for (int i = 1; i < blocks.Count; ++i)
            {
                Blocks bl = blocks[i];
                //int bl2 = blocks[0];
                if (bl.BoundingCircle.Intersects(Player.BoundingRectangle))
                {
                    blocks.Equals(null);
                    blocks.RemoveAt(i--);

                }
                bl.Update(gameTime);
            }


Run the code, it -=SHOULD=- WORK!

When you hit an object it SHOULD destroy, it's different from collect because we removed it, and added all that other stuff in, I bet we could even remove if you jump off the block biggrin.gif.

So THERE WE HAVE IT, I have solo worked this out, i'm VERY proud of it!

Maybe I should make a tutorial, this pretty much is a tutorial lol.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:57AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month