在XNA游戏中使用到手势触控操作时,需要引入using Microsoft.Xna.Framework.Input.Touch;

空间,在该空间下下面两个类在触控编程中会用到。
TouchLocation 用来保存某一个触摸点的状态信息。
TouchCollection 是保存了当前所有触控状态(TouchLocation)的集合。

当我们把一个指头在屏幕上操作,可能会有这样三种动作:按,移动,移开。那么这三个操作在WP7的XNA里如何获取呢?我们就需要了解XNA里的TouchPanel和TouchCollection这两个类

  1. TouchCollection touchState= TouchPanel.GetState();
  2. Foreach(TouchLocation location in touchState)
  3. {
  4. switch(location.State)
  5. {
  6. case TouchLocationState.Pressed://按下
  7. ……
  8. break;
  9. case TouchLocationState.Moved://移动
  10. ……
  11. break;
  12. case TouchLocationState.Released://释放
  13. ……
  14. break;
  15. }
  16. }

TouchLocation :
State 触摸状态,包含4个状态
> TouchLocationState.Pressed 表示屏幕被触摸时手指按下的一瞬间
> TouchLocationState.Moved 表示手指按下后正在移动,经过测试可知,在手指按下的一瞬间State为Pressed ,在手指按下后抬起前这段时间内的状态均是Moved
> TouchLocationState.Invalid 无效状态
> TouchLocationState.Released 表示手指抬起的一瞬间
ID 表示当前触摸事件的ID,一个完成的触控事件的过程应该是“Pressed -> Moved -> Released ”在这个过程中ID是一致的,用来在多点触摸时区分触摸的每个点。
Position 触摸位置,包含两个属性
> X 当前触摸位置的X轴坐标
> Y 当前触摸位置的Y轴坐标
(横屏全屏情况下,屏幕的左上角坐标为(0,0)右下角坐标为(800,480))

和触控操作类似的还有叫“手势”的,也算复杂的触控吧。

  1. TouchPanel.EnabledGestures = GestureType.FreeDrag;//用来指定手势,必须要先设定,否则
  2. 报错
  3. if (TouchPanel.EnabledGestures != GestureType.None)
  4. {
  5. switch (TouchPanel.ReadGesture())
  6. {
  7. case GestureType.Tap: //单击
  8. break;
  9. case GestureType.DoubleTap://双击
  10. break;
  11. case GestureType.FreeDrag://自由拖动
  12. break;
  13. case GestureType.DragComplete://拖动完成
  14. break;
  15. case GestureType.Flick://轻弹
  16. break;
  17. case GestureType.Hold://按住不动
  18. break;
  19. case GestureType.HorizontalDrag://横向拖动
  20. break;
  21. case GestureType.None://无手势
  22. break;
  23. case GestureType.Pinch://捏
  24. break;
  25. case GestureType.PinchComplete://捏完
  26. break;
  27. case GestureType.VerticalDrag://纵向拖动
  28. break;
  29. }
  30. }

示例一各种手势的测试:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. using Microsoft.Xna.Framework.Media;
  12. namespace Gestures
  13. {
  14. /// <summary>
  15. /// This is the main type for your game
  16. /// </summary>
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. SpriteBatch spriteBatch;
  21. SpriteFont spriteFont;
  22. String message = "Do something";
  23. Vector2 messagePos = Vector2.Zero;
  24. Color color = Color.Black;
  25. public Game1()
  26. {
  27. graphics = new GraphicsDeviceManager(this);
  28. Content.RootDirectory = "Content";
  29. // Frame rate is 30 fps by default for Windows Phone.
  30. TargetElapsedTime = TimeSpan.FromTicks(333333);
  31. }
  32. /// <summary>
  33. /// Allows the game to perform any initialization it needs to before starting to run.
  34. /// This is where it can query for any required services and load any non-graphic
  35. /// related content.  Calling base.Initialize will enumerate through any components
  36. /// and initialize them as well.
  37. /// </summary>
  38. protected override void Initialize()
  39. {
  40. //添加各种手势的支持
  41. TouchPanel.EnabledGestures = GestureType.Tap | GestureType.DoubleTap | GestureType.Hold | GestureType.HorizontalDrag
  42. | GestureType.VerticalDrag | GestureType.FreeDrag | GestureType.DragComplete | GestureType.Pinch
  43. | GestureType.PinchComplete | GestureType.Flick;
  44. base.Initialize();
  45. }
  46. /// <summary>
  47. /// LoadContent will be called once per game and is the place to load
  48. /// all of your content.
  49. /// </summary>
  50. protected override void LoadContent()
  51. {
  52. // Create a new SpriteBatch, which can be used to draw textures.
  53. spriteBatch = new SpriteBatch(GraphicsDevice);
  54. //记载字体资源
  55. spriteFont = Content.Load<SpriteFont>("SpriteFont1");
  56. }
  57. /// <summary>
  58. /// UnloadContent will be called once per game and is the place to unload
  59. /// all content.
  60. /// </summary>
  61. protected override void UnloadContent()
  62. {
  63. // TODO: Unload any non ContentManager content here
  64. }
  65. /// <summary>
  66. /// Allows the game to run logic such as updating the world,
  67. /// checking for collisions, gathering input, and playing audio.
  68. /// </summary>
  69. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  70. protected override void Update(GameTime gameTime)
  71. {
  72. // Allows the game to exit
  73. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  74. this.Exit();
  75. //判断手势的类别
  76. if (TouchPanel.IsGestureAvailable)
  77. {
  78. GestureSample gesture = TouchPanel.ReadGesture();
  79. switch (gesture.GestureType)
  80. {
  81. case GestureType.Tap:
  82. message = "That was a Tap";
  83. color = Color.Red;
  84. break;
  85. case GestureType.DoubleTap:
  86. message = "That was a Double Tap";
  87. color = Color.Orange;
  88. break;
  89. case GestureType.Hold:
  90. message = "That was a Hold";
  91. color = Color.Yellow;
  92. break;
  93. case GestureType.HorizontalDrag:
  94. message = "That was a Horizontal Drag";
  95. color = Color.Blue;
  96. break;
  97. case GestureType.VerticalDrag:
  98. message = "That was a Vertical Drag";
  99. color = Color.Indigo;
  100. break;
  101. case GestureType.FreeDrag:
  102. message = "That was a Free Drag";
  103. color = Color.Green;
  104. break;
  105. case GestureType.DragComplete:
  106. message = "Drag gesture complete";
  107. color = Color.Gold;
  108. break;
  109. case GestureType.Flick:
  110. message = "That was a Flick";
  111. color = Color.Violet;
  112. break;
  113. case GestureType.Pinch:
  114. message = "That was a Pinch";
  115. color = Color.Violet;
  116. break;
  117. case GestureType.PinchComplete:
  118. message = "Pinch gesture complete";
  119. color = Color.Silver;
  120. break;
  121. }
  122. messagePos = gesture.Position;
  123. }
  124. base.Update(gameTime);
  125. }
  126. /// <summary>
  127. /// This is called when the game should draw itself.
  128. /// </summary>
  129. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  130. protected override void Draw(GameTime gameTime)
  131. {
  132. GraphicsDevice.Clear(Color.CornflowerBlue);
  133. //绘制屏幕的文字
  134. spriteBatch.Begin();
  135. spriteBatch.DrawString(spriteFont, message, messagePos, color);
  136. spriteBatch.End();
  137. base.Draw(gameTime);
  138. }
  139. }
  140. }

示例二多点触控的测试:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. using Microsoft.Xna.Framework.Media;
  12. namespace MultiTouchMe
  13. {
  14. /// <summary>
  15. /// This is the main type for your game
  16. /// </summary>
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. SpriteBatch spriteBatch;
  21. SpriteFont spriteFont;
  22. TouchCollection touchCollection;
  23. public Game1()
  24. {
  25. graphics = new GraphicsDeviceManager(this);
  26. Content.RootDirectory = "Content";
  27. // Frame rate is 30 fps by default for Windows Phone.
  28. TargetElapsedTime = TimeSpan.FromTicks(333333);
  29. }
  30. /// <summary>
  31. /// Allows the game to perform any initialization it needs to before starting to run.
  32. /// This is where it can query for any required services and load any non-graphic
  33. /// related content.  Calling base.Initialize will enumerate through any components
  34. /// and initialize them as well.
  35. /// </summary>
  36. protected override void Initialize()
  37. {
  38. // TODO: Add your initialization logic here
  39. base.Initialize();
  40. }
  41. /// <summary>
  42. /// LoadContent will be called once per game and is the place to load
  43. /// all of your content.
  44. /// </summary>
  45. protected override void LoadContent()
  46. {
  47. // Create a new SpriteBatch, which can be used to draw textures.
  48. spriteBatch = new SpriteBatch(GraphicsDevice);
  49. // TODO: use this.Content to load your game content here
  50. spriteFont = Content.Load<SpriteFont>("SpriteFont1");
  51. }
  52. /// <summary>
  53. /// UnloadContent will be called once per game and is the place to unload
  54. /// all content.
  55. /// </summary>
  56. protected override void UnloadContent()
  57. {
  58. // TODO: Unload any non ContentManager content here
  59. }
  60. /// <summary>
  61. /// Allows the game to run logic such as updating the world,
  62. /// checking for collisions, gathering input, and playing audio.
  63. /// </summary>
  64. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  65. protected override void Update(GameTime gameTime)
  66. {
  67. // Allows the game to exit
  68. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  69. this.Exit();
  70. // TODO: Add your update logic here
  71. touchCollection = TouchPanel.GetState();
  72. base.Update(gameTime);
  73. }
  74. /// <summary>
  75. /// This is called when the game should draw itself.
  76. /// </summary>
  77. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  78. protected override void Draw(GameTime gameTime)
  79. {
  80. GraphicsDevice.Clear(Color.CornflowerBlue);
  81. // TODO: Add your drawing code here
  82. spriteBatch.Begin();
  83. foreach (TouchLocation touch in touchCollection)
  84. spriteBatch.DrawString(spriteFont, "ID: " + touch.Id.ToString() + " (" +
  85. (int)touch.Position.X + "," + (int)touch.Position.Y + ")", touch.Position, Color.White);
  86. spriteBatch.End();
  87. base.Draw(gameTime);
  88. }
  89. }
  90. }

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078389

XNA游戏:手势触控相关推荐

  1. 安卓开机画面_iPad拜拜!虎贲芯片+安卓10全局手势触控,quot;国产之光quot;台电P20HD...

    近日,台电全新推出了平板电脑P20HD.这款平板电脑在拥有AI加速.立体声效.7小时续航.双频WiFi等一流配置的基础上,还搭载了国产芯片虎贲SC9863A.正式基于这些优点,台电P20HD受到了网友 ...

  2. html hover效果手势,触控设备中Hover效果的互动设计

    对于网页浏览者来说,按钮控件的变化可以说是再熟悉不过的一种机制了:一般使用鼠标浏览时最容易感受到的有Normal.Hover以及Archive这三种效果,分别为按钮平时的状态.鼠标游标移到上方的效果. ...

  3. 梅林安装opkg后安装iperf3_MacBook安装双系统后手势触控问题

    手上有台MacBookPro 2016款,由于工作原因需要安装Windows系统,使用macos自带的 Boot Camp安装了win10,这一步骤是没有什么难度的,就像安装软件一样,安装完会自动重启 ...

  4. 苹果鼠标右键怎么按_Mac触控板常用的手势操作,让你告别Windows鼠标!

    https://www.zhihu.com/video/967471235399155712 在Windows电脑上,鼠标是一个划时代的发明,如果没有它,似乎用电脑进行的大量工作都要停滞.从学习电脑开 ...

  5. pyaudio usb playback_5.5寸触控屏IP电话会议USB全向麦克风NK-OAM600U_影视工业网

    寸触控屏视频会议USB全向麦克风(拾音器)NK-OAM600U 概述: 派尼珂NK-OAM600U视频会议USB全向麦克风,是一款配置多点手势触控FHD屏的高清会议电话,便捷的连接方式:支持USB/以 ...

  6. 多点触控液晶三维电子沙盘 实景三维电子沙盘

    多点触控液晶三维电子沙盘 实景三维电子沙盘 多点触控液晶三维电子沙盘 实景三维电子沙盘(3dgis.top)采用大数据.三维GIS.物联网.可视化等先进技术,具有手势触控.语音控制.深度学习.视频识别 ...

  7. 触控科技陈昊芝:捕鱼达人装机量1亿 月活跃用户3246万

    触控科技CEO陈昊芝透露<捕鱼达人>总装机量已达一亿,日活跃和月活跃用户超过<愤怒的小鸟>在中国四个版本总和的50%,截止8月份<捕鱼达人>在中国地区月活跃用户达3 ...

  8. Windows Phone 7范例游戏Platformer实战5——多点触控编程

    即使是再有经验的XNA程序员,在开始Windows Phone 7上的游戏开发时也不得不学习下多点触控这个新的实现方法.虽然目前有些Windows Phone 7手机附带了键盘,但是为了对所有WP7手 ...

  9. Windows Phone 7 XNA触控操作之Gestures

    这一讲我将集中讨论Gestures(手势),以及如何在Silverlight应用程序中使用XNA程序集来简单地识别触控输入. 什么是XNA? 这整个系列的焦点是Silverlight,XNA技术可在W ...

最新文章

  1. 学习Oracle 最好的5本书,最畅销的Oracle 5本书
  2. ASP.NET 一般处理程序
  3. ue4 导出模型_UE4构建光照后模型变黑,二套UV解决办法
  4. 深度学习实战篇-基于RNN的中文分词探索
  5. SSM中进行Junit单元测试时无法注入service
  6. c语言switch编写计算器,超级新手,用switch写了个计算器程序,求指导
  7. STM32那点事(2)_时钟树(上)
  8. string 是值类型,还是引用类型(.net)
  9. JavaScript:windows关机效果
  10. linux挂载磁盘组,11G ASM磁盘组不能自动MOUNT处理
  11. Java并发包下的CAS相关的原子操作
  12. bootstrap之项目一的填坑
  13. GoldWave的消音、淡入淡出、改变音乐速率
  14. vasp和ms_采用MS建模的基本步骤以及vasp新手入门需要注意的十个简单问题
  15. 什么是 BI?和报表有什么关系?有了 BI 还要做报表吗?
  16. 一条让人不安的坐地龙
  17. SQLServer中sp_Who、sp_Who2和sp_WhoIsActive介绍和查看监视运行
  18. 中考考试的指令广播_中考考试指令系统使用的说明.doc
  19. 从《计算机网络》到TCP/IP
  20. CALPHAD方法中“外推”的理解

热门文章

  1. 找出一批学生的最高分
  2. 小心!手机这样充电被1秒窃取信息!
  3. 癞子麻将胡牌以及听牌算法实现
  4. ELK环境搭建+入门使用
  5. kickstart 2020 A Bundling
  6. 疫情之下的企业该如何生存?
  7. 揭秘:女谍川岛芳子究竟是不是双性恋
  8. Python题目——实现人机对战的尼姆游戏:假设有一堆物品,计算机和人类玩家轮流从其中拿走一部分。在每一步中,人或计算机可以自由选择拿走多少物品,但是必须至少拿走一个并且最多只能拿走一半物品
  9. TreeMap类型通过实体类添加数据并排序
  10. 教你一秒钟画N多人像素描