【问题标题】:Apply gesture effects only when the user touches the object仅在用户触摸对象时应用手势效果
【发布时间】:2013-12-21 13:21:21
【问题描述】:

我是游戏开发的新手,我正在使用 XNA 的单游戏实现开发 Windows 商店的概念,其中,我正在通过轻弹手势进行球移动。但是,我的问题是球的移动不仅是在球上应用手势,而且在屏幕上的任何位置应用手势都会使我的球移动。我想要的是,我的球只会在我轻弹球本身而不是屏幕的其他区域时移动。我正在使用矩形边界框来查看用户是否触摸了球对象但徒劳无功。

我正在分享我的更新游戏时间的方法

    //Global vars
    //cat is the ball object here
    GraphicsDeviceManager _graphics;
    SpriteBatch _spriteBatch;
    private Texture2D cat;
    private Vector2 _spritePosition;
    private SpriteFont _fontMiramonte;
    BounceableImage mBall;
    const float DECELERATION = 1000;
    Vector2 position = Vector2.Zero;
    Vector2 velocity;

  //Update method
 protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                if (IsPointInObject(position))
                {
                    if (gesture.GestureType == GestureType.Flick)
                        velocity += gesture.Delta;
                    if (gesture.GestureType == GestureType.Hold)
                        position = gesture.Position;
                }
            }

            // Use velocity to adjust position and decelerate
            if (velocity != Vector2.Zero)
            {
                float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
                position += velocity * elapsedSeconds;
                float newMagnitude = velocity.Length() - DECELERATION * elapsedSeconds;
                velocity.Normalize();
                velocity *= Math.Max(0, newMagnitude);
            }
            UpdateSprite(gameTime, ref position, ref velocity);
            base.Update(gameTime);
        }



//method to detect whether the touched point lies inside the object ball
  public bool IsPointInObject(Vector2 positionVector)
        {
            Rectangle bbx = new Rectangle((int)position.X , (int)position.Y, (int)cat.Width, (int)cat.Height);
            return bbx.Contains((int)(positionVector.X), (int)(positionVector.Y));
        }

【问题讨论】:

    标签: c# windows-8 windows-store-apps xna-4.0 monogame


    【解决方案1】:

    您必须在 TouchPanel 上设置一个画布,并设置并锁定该画布以供玩家进行手势移动。您必须将面板中的画布块设置得更小,以便查看触摸效果。

    【讨论】:

    • 抱歉,您能帮我找一个工作演示吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多