1

我使用 Xna 和 C#。

我试图让我的列表中的特定对象做不同的事情。

我现在写出来的方式是它选择了我列表中的所有纹理,并且它们都做同样的事情,如果我尝试选择某个纹理,它们仍然做同样的事情:

if (planet.Load("planet0")) { if (planet.Bounds.Contains((int)tl.Position.X, (int)tl.Position.Y)) { changeScreenDelegate(ScreenState.Menu); } }

我基本上试图让每个选择进入不同的屏幕。

这是我班的其余部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace PlanetDrill2
{
    class StarSystemUrsa : Screen
    {
        Texture2D starSystemTexture;

    List<Planet> planets;

    Texture2D star;
    Vector2 position;
    Texture2D previousButton;


    public StarSystemUrsa(Game game, SpriteBatch spriteBatch, ChangeScreen changeScreen)
        : base(game, batch, changeScreen)
    {

        position = new Vector2((480 - 295) / 2, (800 - 295) / 2);



        planets = new List<Planet>();

        Planet planet = new Planet(content, spriteBatch);
        planet.Load("planet0");
        planet.velocity = 0.04f;
        planet.radius = 80;
        planet.angle = MathHelper.ToRadians(90);
        planets.Add(planet);

        planet = new Planet(content, spriteBatch);
        planet.Load("planet4");
        planet.velocity = 0.02f;
        planet.radius = 135;
        planet.angle = MathHelper.ToRadians(120);
        planets.Add(planet);

        planet = new Planet(content, spriteBatch);
        planet.Load("planet2");
        planet.velocity = 0.009f;
        planet.radius = 180;
        planet.angle = MathHelper.ToRadians(160);
        planets.Add(planet);

    }




    protected override void SetupInputs()
    {

        base.SetupInputs();
    }


    public override void Activate()
    {
        base.Activate();
    }

    protected override void LoadScreenContent(ContentManager content)
    {
        starSystemTexture = content.Load<Texture2D>("levelSelectMenu");
        star = content.Load<Texture2D>("ursaeMajorisStar");
        previousButton = content.Load<Texture2D>("previousButton2");



        base.LoadScreenContent(content);
    }

    protected override void UpdateScreen(GameTime gameTime, DisplayOrientation screenOrientation)
    {

        TouchCollection touchCollection = TouchPanel.GetState();

    foreach(TouchLocation tl in touchCollection)
        {
            if(tl.State == TouchLocationState.Pressed) // Moved? Is that right?
            {  
                foreach(Planet planet in planets)
                {

                    // Alternately: if(GetPlanetRectangle(planet).Contains(...))
                    if(planet.Bounds.Contains((int)tl.Position.X, (int)tl.Position.Y))
                    {
                        changeScreenDelegate(ScreenState.Menu);
                    }

                }

            }

            TouchPanel.EnabledGestures = GestureType.DoubleTap | GestureType.VerticalDrag;

            while (TouchPanel.IsGestureAvailable)
            {


                GestureSample gesture = TouchPanel.ReadGesture();

                switch (gesture.GestureType)
                {
                    case GestureType.DoubleTap:
                        changeScreenDelegate(ScreenState.launchScreen);
                        break;
                    case GestureType.VerticalDrag:

                        break;
                    default:
                        break;
                }
            }
        }







        foreach (Planet planet in planets)
        {
            planet.angle += planet.velocity;
            float orbitx = (float)(Math.Cos(planet.angle) * planet.radius);
            float orbity = (float)(Math.Sin(planet.angle) * planet.radius);

            float x = (game.GraphicsDevice.Viewport.Width - planet.image.Width) / 2 + orbitx;
            float y = (game.GraphicsDevice.Viewport.Height - planet.image.Height) / 2 + orbity;

            planet.position = new Vector2(x, y);


        }



        base.UpdateScreen(gameTime, screenOrientation);


    }

    protected override void DrawScreen(SpriteBatch batch, DisplayOrientation screenOrientation)
    {


        batch.Draw(starSystemTexture, Vector2.Zero, Color.White);

        foreach (Planet planet in planets)
        {
            planet.Draw();
        }

        batch.Draw(star,position, Color.White);
        batch.Draw(previousButton, new Vector2(240, 750), Color.White);
        base.DrawScreen(batch, screenOrientation);
    }





      protected override void SaveScreenState()
        {
            base.SaveScreenState();
        }
    }
}
4

2 回答 2

2

为您的行星类提供另一个名为 ID 的属性,然后在初始化它们时为每个行星提供不同的 ID。然后在你的 foreach planet in planets loop 中,你可以说

if(planet.ID == 0) { if (planet.Bounds.Contains((int)tl.Position.X, (int)tl.Position.Y)) { changeScreenDelegate(ScreenState.Menu); } }

对每个可能的 ID 号执行此操作,并将屏幕状态更改为您希望它切换到的任何屏幕状态。

另外,您可以列举 ID 号,以清楚地了解所引用的行星。

于 2012-08-04T21:49:07.777 回答
1

将 Menuscreen 附加为 Planet 对象的属性。给这些对象一个方法来告诉屏幕管理器激活他们的屏幕。在您的(如果 Planet.Bounds.Conains)逻辑块内的触摸星球上调用该方法。

于 2012-08-03T22:06:23.957 回答