wp7触摸屏游戏的主角控制移动方式-部分代码

 #region Input触摸屏移动

        /// <summary>

        /// Input helper method provided by GameScreen.  Packages up the various input values for ease of use. 

        ///  Here it checks for pausing and handles controlling the player's tank.在这里,它检查暂停和处理控制球员的坦克。

        /// </summary>

        /// <param name="input">游戏手柄的状态</param>

        /// 这个方法将会读取当前玩家的输入并在以后响应游戏变量变化时会用到

        public override void HandleInput(InputState input)

        {

            if (input == null)

                //抛出一个新路径并命名为input;

                throw new ArgumentException("input");

            //判断游戏状态是否为暂停

            if (input.PauseGame)

            {

                if (gameOver == true)

                    finishCurrentGame();

            }

            else

            {

                //获取触控面板的状态

                touchState = TouchPanel.GetState();

                //设置一个bool定义是否碰触控件

                bool buttonTouched = false;

                //循环遍历每个触控面板的状态

                foreach (TouchLocation location in touchState)

                {

                    switch (location.State)

                    {

                        //按压

                        case TouchLocationState.Pressed:

                            buttonTouched = true;

                            break;

                        //移动

                        case TouchLocationState.Moved:

                            break;

                        //释放

                        case TouchLocationState.Released:

                            break;

                    }

                }

                //a被定义为float型,0.0f后的f说明是float常量 若没有f 则0.0是double型

                float movement = 0.0f;

                if (accelState != null)

                {

                    if (Math.Abs(accelState.X) > 0.0f)

                    {

                        if (accelState.X > 0.0f)

                            movement = 1.0f;

                        else

                            movement = -1.0f;

                    }

                }

                //获取键盘输入状态

                KeyboardState keyState = Keyboard.GetState();

                //更新的球员流速大于X轴# 1

                player.Velocity.X = movement;

                //根据键盘的输入字符控制游戏

                //这个部分处理坦克运动。我们只允许一个“运动”行动发生一次,以免touchpad设备无法得到双重的打击

                if (input.CurrentGamePadStates[0].DPad.Left == ButtonState.Pressed ||

                    keyState.IsKeyDown(Keys.Left))

                {

                    //更新的球员流速大于X轴# 2

                    player.Velocity.X = -1.0f;

                }

                else if (input.CurrentGamePadStates[0].DPad.Right ==

                    ButtonState.Pressed || keyState.IsKeyDown(Keys.Right))

                {

                    //更新的球员流速大于X轴# 3

                    player.Velocity.X = 1.0f;

                }

                else

                {

                    //更新的球员流速大于X轴# 4

                    player.Velocity.X =

                        MathHelper.Min(input.CurrentGamePadStates[0].ThumbSticks.Left.X * 2.0f, 1.0f);

                }

                //B按钮,或按压的上半部垫或空间上键盘或触摸触摸屏火灾武器。

                if (input.CurrentGamePadStates[0].IsButtonDown(Buttons.B) ||

                    input.CurrentGamePadStates[0].IsButtonDown(Buttons.A) ||

                    input.CurrentGamePadStates[0].ThumbSticks.Left.Y > 0.25f ||

                    keyState.IsKeyDown(Keys.Space) || buttonTouched)

                {

                    if (player.FireTimer <= 0.0f && player.IsAlive && !gameOver)

                    {

                        Bullet bullet = CretePlayerBullet();

                        bullet.Position = new Vector2((int)(player.Position.X + player.Width / 2) - bulletTexture.Width / 2, player.Position.Y - 4);

                        bullet.Velocity = new Vector2(0, -256.0f);

                        player.FireTimer = 1.0f;

                        particles.CreatePlayerFireSmoke(player);

                        playerFired.Play();

                    }

                    else if (gameOver)

                        finishCurrentGame();

                }

            }

            base.HandleInput(input);

        }

public virtual void HandleInput(InputState input) { }

  

你可能感兴趣的:(wp7)