5 Replies - 3008 Views - Last Post: 09 October 2012 - 02:19 PM

#1 Hintze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-September 12

XNA passing through if else statement, even though meeting if()

Posted 28 September 2012 - 02:57 PM

Hi everyone,
I have a Problem which i have tried solving for hours now and i canīt understand why my code behaves like this.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace test
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D playField;
        Texture2D cross;
        Texture2D circle;
        MouseState mouseinfo;
        Rectangle mouseRect;
        int turn = 0;
        bool player1Turn = true;
        char[,] tilePlace = new char[3, 3];
        Vector2[] playerplacement1pos = new Vector2[3];
        Vector2[] playerplacement2pos = new Vector2[3];
        string playerTurn = "player1";
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 600;
            graphics.PreferredBackBufferHeight = 600;
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {


            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            playField = Content.Load<Texture2D>("Background");
            cross = Content.Load<Texture2D>("cross");
            circle = Content.Load<Texture2D>("Circle");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {


            mouseinfo = Mouse.GetState();
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here



            mouseRect = new Rectangle(mouseinfo.X, mouseinfo.Y, 1, 1);
            if (mouseinfo.LeftButton == ButtonState.Pressed)
            {
                collisionDetect();
            }
            // playerTurn();
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            spriteBatch.Draw(playField, Vector2.Zero, Color.White);
            spriteBatch.Draw(circle, playerplacement1pos[0], Color.White);
       //     spriteBatch.Draw(circle, playerplacement1pos[1], Color.White);
       //    spriteBatch.Draw(circle, playerplacement1pos[2], Color.White);
            spriteBatch.Draw(cross, playerplacement2pos[0], Color.White);
       //     spriteBatch.Draw(cross, playerplacement2pos[1], Color.White);
       //     spriteBatch.Draw(cross, playerplacement2pos[2], Color.White);

            spriteBatch.End();
            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }

        void collisionDetect()
        {
            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 3; x++)
                {
                    Rectangle tileRect1 = new Rectangle(x * 200, y * 200, 200, 200);
                    if (tileRect1.Intersects(mouseRect))
                    {
                        Vector2 pos = new Vector2(x * 200, y * 200);
                        playerturn(pos);


                        y = 3;
                        x = 3;
                        //    turn++; 

                    }
                }
            }

        }
        public void playerturn(Vector2 pos)
        {
            int x = 0;
            int y = 0;
            

            switch (playerTurn)
            {
                case "player1":
                    {
                        playerplacement1pos[x] = pos;
                        playerTurn = "player2";
                        x++;
                        break;
                    }
                case "player2":
                    {

                        playerplacement2pos[y] = pos;
                        playerTurn = "player1";
                        y++;
                        break;
                    }

            }
        }
    }
}



Thanks for you time.

Hintze

Is This A Good Question/Topic? 0
  • +

Replies To: XNA passing through if else statement, even though meeting if()

#2 Hintze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-September 12

Re: XNA passing through if else statement, even though meeting if()

Posted 28 September 2012 - 03:04 PM

Sorry for not really saying my problem.
my problem is that it writes out both the circle and the cross,


I couldnīt find an edit button.

Hintze
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: XNA passing through if else statement, even though meeting if()

Posted 28 September 2012 - 03:45 PM

Quote

Passing Through If Else Statement, Even Though Meeting If()


Simple statement of fact: If the condition is met then it is NOT going to pass through. You do not have the only computer in the world where if() works for everyone else but not you.

Given that fact, the next deduction is that your conditions is NOT really being met.

You have 177 lines of code with multiple if() checks so I have no idea which one you are talking about.

Put a breakpoint on it and double-check your values and execute line by line with F10 (see the link in my signature for what does this error mean? That has good debugging techniques and other links.

If you can't find why the if() is failing then provide us a screenshot with the execution stopped on that line and the Locals pallet visible so we can see the actual values.
Was This Post Helpful? 1
  • +
  • -

#4 Hintze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-September 12

Re: XNA passing through if else statement, even though meeting if()

Posted 29 September 2012 - 02:31 AM

View PosttlhIn`toq, on 28 September 2012 - 03:45 PM, said:

Quote

Passing Through If Else Statement, Even Though Meeting If()


Simple statement of fact: If the condition is met then it is NOT going to pass through. You do not have the only computer in the world where if() works for everyone else but not you.

Given that fact, the next deduction is that your conditions is NOT really being met.

You have 177 lines of code with multiple if() checks so I have no idea which one you are talking about.

Put a breakpoint on it and double-check your values and execute line by line with F10 (see the link in my signature for what does this error mean? That has good debugging techniques and other links.

If you can't find why the if() is failing then provide us a screenshot with the execution stopped on that line and the Locals pallet visible so we can see the actual values.


I'm so sorry I guess i was in a rush,
The problem is in the playerturn() function, where i had an if else statement at first, but then tried to switch it to an switch case.
The same problem i have with both of these, is that if i put a breakpoint for example on my if statement or anywhere before that i never pass through the else statement, or in this case the second case, which is exactly what i want to happen, but if i put the breakpoint in the second case, then I get to that point in code which theoretically should never happen.
So the playeplacement2pos[0] gets assigned the same position as playeplacement1pos[0] ,
If you need any more info just tell me
Thanks for your time:)

Hintze
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5479
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: XNA passing through if else statement, even though meeting if()

Posted 29 September 2012 - 01:10 PM

I already told you want I need to help.
Put a breakpoint at the if/case statement you think is not working right.
Make sure the Locals and Autos pallet is visible so we can see the actual values of the variables being used.
Take a screen shot and post it in this thread.
Was This Post Helpful? 0
  • +
  • -

#6 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: XNA passing through if else statement, even though meeting if()

Posted 09 October 2012 - 02:19 PM

Have you tried changing the order for your rectangle intersection logic as it seems to me that you are trying to fit the door into the key instead of the key into the door if you get my analogy.

Since I can't see anything wrong with the IF/SWITCH logic in your playerturn() function it must be where you call it, and since you've only got it active in collisionDetect() I'd say that is where your problem lies, otherwise the only other place is where that function is called in Update().

void collisionDetect()
        {
            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 3; x++)
                {
                    Rectangle tileRect1 = new Rectangle(x * 200, y * 200, 200, 200);
                    if (mouseRect.Intersects(tileRect1))
                    {
                        Vector2 pos = new Vector2(x * 200, y * 200);
                        playerturn(pos);


                        y = 3;
                        x = 3;
                        //    turn++; 

                    }
                }
            }

        }



There is another problem you will definitely encounter within your playerturn() function that I will give you a heads-up about now.
Where you declare int x = 0; and int y = 0; inside the routine, yet you are trying to increment them inside the routine without either a loop, or you are assuming that these values are meant to be globally available, then as soon as this function is finished those values will be reset, even more so if you redeclare them at the start of the function every single time you call it.

They will always be 0 when you run through the procedural logic and end at 1.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1