XNA游戏:手势触控
在XNA游戏中使用到手势触控操作时,需要引入using Microsoft.Xna.Framework.Input.Touch;
空间,在该空间下下面两个类在触控编程中会用到。
TouchLocation 用来保存某一个触摸点的状态信息。
TouchCollection 是保存了当前所有触控状态(TouchLocation)的集合。
当我们把一个指头在屏幕上操作,可能会有这样三种动作:按,移动,移开。那么这三个操作在WP7的XNA里如何获取呢?我们就需要了解XNA里的TouchPanel和TouchCollection这两个类
- TouchCollection touchState= TouchPanel.GetState();
- Foreach(TouchLocation location in touchState)
- {
- switch(location.State)
- {
- case TouchLocationState.Pressed://按下
- ……
- break;
- case TouchLocationState.Moved://移动
- ……
- break;
- case TouchLocationState.Released://释放
- ……
- break;
- }
- }
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))
和触控操作类似的还有叫“手势”的,也算复杂的触控吧。
- TouchPanel.EnabledGestures = GestureType.FreeDrag;//用来指定手势,必须要先设定,否则
- 报错
- if (TouchPanel.EnabledGestures != GestureType.None)
- {
- switch (TouchPanel.ReadGesture())
- {
- case GestureType.Tap: //单击
- break;
- case GestureType.DoubleTap://双击
- break;
- case GestureType.FreeDrag://自由拖动
- break;
- case GestureType.DragComplete://拖动完成
- break;
- case GestureType.Flick://轻弹
- break;
- case GestureType.Hold://按住不动
- break;
- case GestureType.HorizontalDrag://横向拖动
- break;
- case GestureType.None://无手势
- break;
- case GestureType.Pinch://捏
- break;
- case GestureType.PinchComplete://捏完
- break;
- case GestureType.VerticalDrag://纵向拖动
- break;
- }
- }
示例一各种手势的测试:
- 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.Input.Touch;
- using Microsoft.Xna.Framework.Media;
- namespace Gestures
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- SpriteFont spriteFont;
- String message = "Do something";
- Vector2 messagePos = Vector2.Zero;
- Color color = Color.Black;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- // Frame rate is 30 fps by default for Windows Phone.
- TargetElapsedTime = TimeSpan.FromTicks(333333);
- }
- /// <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()
- {
- //添加各种手势的支持
- TouchPanel.EnabledGestures = GestureType.Tap | GestureType.DoubleTap | GestureType.Hold | GestureType.HorizontalDrag
- | GestureType.VerticalDrag | GestureType.FreeDrag | GestureType.DragComplete | GestureType.Pinch
- | GestureType.PinchComplete | GestureType.Flick;
- 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);
- //记载字体资源
- spriteFont = Content.Load<SpriteFont>("SpriteFont1");
- }
- /// <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)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- //判断手势的类别
- if (TouchPanel.IsGestureAvailable)
- {
- GestureSample gesture = TouchPanel.ReadGesture();
- switch (gesture.GestureType)
- {
- case GestureType.Tap:
- message = "That was a Tap";
- color = Color.Red;
- break;
- case GestureType.DoubleTap:
- message = "That was a Double Tap";
- color = Color.Orange;
- break;
- case GestureType.Hold:
- message = "That was a Hold";
- color = Color.Yellow;
- break;
- case GestureType.HorizontalDrag:
- message = "That was a Horizontal Drag";
- color = Color.Blue;
- break;
- case GestureType.VerticalDrag:
- message = "That was a Vertical Drag";
- color = Color.Indigo;
- break;
- case GestureType.FreeDrag:
- message = "That was a Free Drag";
- color = Color.Green;
- break;
- case GestureType.DragComplete:
- message = "Drag gesture complete";
- color = Color.Gold;
- break;
- case GestureType.Flick:
- message = "That was a Flick";
- color = Color.Violet;
- break;
- case GestureType.Pinch:
- message = "That was a Pinch";
- color = Color.Violet;
- break;
- case GestureType.PinchComplete:
- message = "Pinch gesture complete";
- color = Color.Silver;
- break;
- }
- messagePos = gesture.Position;
- }
- 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.CornflowerBlue);
- //绘制屏幕的文字
- spriteBatch.Begin();
- spriteBatch.DrawString(spriteFont, message, messagePos, color);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
示例二多点触控的测试:
- 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.Input.Touch;
- using Microsoft.Xna.Framework.Media;
- namespace MultiTouchMe
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- SpriteFont spriteFont;
- TouchCollection touchCollection;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- // Frame rate is 30 fps by default for Windows Phone.
- TargetElapsedTime = TimeSpan.FromTicks(333333);
- }
- /// <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);
- // TODO: use this.Content to load your game content here
- spriteFont = Content.Load<SpriteFont>("SpriteFont1");
- }
- /// <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)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- // TODO: Add your update logic here
- touchCollection = TouchPanel.GetState();
- 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.CornflowerBlue);
- // TODO: Add your drawing code here
- spriteBatch.Begin();
- foreach (TouchLocation touch in touchCollection)
- spriteBatch.DrawString(spriteFont, "ID: " + touch.Id.ToString() + " (" +
- (int)touch.Position.X + "," + (int)touch.Position.Y + ")", touch.Position, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078389
XNA游戏:手势触控相关推荐
- 安卓开机画面_iPad拜拜!虎贲芯片+安卓10全局手势触控,quot;国产之光quot;台电P20HD...
近日,台电全新推出了平板电脑P20HD.这款平板电脑在拥有AI加速.立体声效.7小时续航.双频WiFi等一流配置的基础上,还搭载了国产芯片虎贲SC9863A.正式基于这些优点,台电P20HD受到了网友 ...
- html hover效果手势,触控设备中Hover效果的互动设计
对于网页浏览者来说,按钮控件的变化可以说是再熟悉不过的一种机制了:一般使用鼠标浏览时最容易感受到的有Normal.Hover以及Archive这三种效果,分别为按钮平时的状态.鼠标游标移到上方的效果. ...
- 梅林安装opkg后安装iperf3_MacBook安装双系统后手势触控问题
手上有台MacBookPro 2016款,由于工作原因需要安装Windows系统,使用macos自带的 Boot Camp安装了win10,这一步骤是没有什么难度的,就像安装软件一样,安装完会自动重启 ...
- 苹果鼠标右键怎么按_Mac触控板常用的手势操作,让你告别Windows鼠标!
https://www.zhihu.com/video/967471235399155712 在Windows电脑上,鼠标是一个划时代的发明,如果没有它,似乎用电脑进行的大量工作都要停滞.从学习电脑开 ...
- pyaudio usb playback_5.5寸触控屏IP电话会议USB全向麦克风NK-OAM600U_影视工业网
寸触控屏视频会议USB全向麦克风(拾音器)NK-OAM600U 概述: 派尼珂NK-OAM600U视频会议USB全向麦克风,是一款配置多点手势触控FHD屏的高清会议电话,便捷的连接方式:支持USB/以 ...
- 多点触控液晶三维电子沙盘 实景三维电子沙盘
多点触控液晶三维电子沙盘 实景三维电子沙盘 多点触控液晶三维电子沙盘 实景三维电子沙盘(3dgis.top)采用大数据.三维GIS.物联网.可视化等先进技术,具有手势触控.语音控制.深度学习.视频识别 ...
- 触控科技陈昊芝:捕鱼达人装机量1亿 月活跃用户3246万
触控科技CEO陈昊芝透露<捕鱼达人>总装机量已达一亿,日活跃和月活跃用户超过<愤怒的小鸟>在中国四个版本总和的50%,截止8月份<捕鱼达人>在中国地区月活跃用户达3 ...
- Windows Phone 7范例游戏Platformer实战5——多点触控编程
即使是再有经验的XNA程序员,在开始Windows Phone 7上的游戏开发时也不得不学习下多点触控这个新的实现方法.虽然目前有些Windows Phone 7手机附带了键盘,但是为了对所有WP7手 ...
- Windows Phone 7 XNA触控操作之Gestures
这一讲我将集中讨论Gestures(手势),以及如何在Silverlight应用程序中使用XNA程序集来简单地识别触控输入. 什么是XNA? 这整个系列的焦点是Silverlight,XNA技术可在W ...
最新文章
- 学习Oracle 最好的5本书,最畅销的Oracle 5本书
- ASP.NET 一般处理程序
- ue4 导出模型_UE4构建光照后模型变黑,二套UV解决办法
- 深度学习实战篇-基于RNN的中文分词探索
- SSM中进行Junit单元测试时无法注入service
- c语言switch编写计算器,超级新手,用switch写了个计算器程序,求指导
- STM32那点事(2)_时钟树(上)
- string 是值类型,还是引用类型(.net)
- JavaScript:windows关机效果
- linux挂载磁盘组,11G ASM磁盘组不能自动MOUNT处理
- Java并发包下的CAS相关的原子操作
- bootstrap之项目一的填坑
- GoldWave的消音、淡入淡出、改变音乐速率
- vasp和ms_采用MS建模的基本步骤以及vasp新手入门需要注意的十个简单问题
- 什么是 BI?和报表有什么关系?有了 BI 还要做报表吗?
- 一条让人不安的坐地龙
- SQLServer中sp_Who、sp_Who2和sp_WhoIsActive介绍和查看监视运行
- 中考考试的指令广播_中考考试指令系统使用的说明.doc
- 从《计算机网络》到TCP/IP
- CALPHAD方法中“外推”的理解