游戏控制器介绍及代码集成
在iOS7中,苹果加强了对游戏开发的支持,其中一个功能就是增加了Game Controller Framework (GameController.framework)可以让你在程序中发现游戏控制器硬件并进行配置。通过Game Controller Framework可以通过无线或者蓝牙连接iOS设备。当控制器可用的时候,该Framework会通知应用程序。
Game Controller Framework 支持三种类型的控制器
• 标准的控制器:该控制器具有一个方向键,一个暂停按钮,四个按钮和两个肩部按钮。
• 扩展的控制器:该控制器跟标准控制器类似,但它可以有两个杆和四个肩部按钮。
• 扩展无线控制器:跟上面的控制器类似,但它以无线方式工作,不需要将IOS设备放置到控制器里面。
[img]http://dl2.iteye.com/upload/attachment/0097/4664/f9537287-cacf-36f2-a48a-687a2182cd2e.png[/img]
软件环境:ios7 + xcode5
操作系统:OSX 10.8.4
实现过程:
1:将游戏控制器的Connet和Disconnet状态进行事件注册
- (void)configureGameControllers {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameControllerDidConnect:) name:GCControllerDidConnectNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameControllerDidDisconnect:) name:GCControllerDidDisconnectNotification object:nil];
[GCController startWirelessControllerDiscoveryWithCompletionHandler:^{
// we don't use any code here since when new controllers are found we will get notifications}];}
实现相应注册事件的代码
- (void)gameControllerDidConnect:(NSNotification *)notification {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"message:@"Game controller connected. Do you want to use it?"delegate:selfcancelButtonTitle:@"No"otherButtonTitles:@"Yes", nil];[alert show];}- (void)gameControllerDidDisconnect:(NSNotification *)notification {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"message:@"Game controller has disconnected."delegate:nilcancelButtonTitle:@"Ok"otherButtonTitles:nil, nil];[alert show];}
2:遍历所有的控制器,并对其按键进行注册
- (void)configureConnectedGameControllers {for (GCController *controller in [GCController controllers]) {[self setupController:controller ];}}
- (void)setupController:(GCController *)controller {/** 对X,Y,A,B键位进行注册**/GCControllerButtonValueChangedHandler XYABButtonHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {};if (controller.gamepad) {controller.gamepad.buttonA.valueChangedHandler = XYABButtonHandler;controller.gamepad.buttonB.valueChangedHandler = XYABButtonHandler;controller.gamepad.buttonX.valueChangedHandler = XYABButtonHandler;controller.gamepad.buttonY.valueChangedHandler = XYABButtonHandler;}if (controller.extendedGamepad) {controller.extendedGamepad.buttonA.valueChangedHandler = XYABButtonHandler;controller.extendedGamepad.buttonB.valueChangedHandler = XYABButtonHandler;controller.extendedGamepad.buttonX.valueChangedHandler = XYABButtonHandler;controller.extendedGamepad.buttonY.valueChangedHandler = XYABButtonHandler;}/** 对肩部的键位进行注册**/GCControllerButtonValueChangedHandler LRShoulderHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {};if (controller.gamepad) {controller.gamepad.leftShoulder.valueChangedHandler = LRShoulderHandler;controller.gamepad.rightShoulder.valueChangedHandler = LRShoulderHandler;}if (controller.extendedGamepad) {controller.extendedGamepad.leftShoulder.valueChangedHandler = LRShoulderHandler;controller.extendedGamepad.rightShoulder.valueChangedHandler = LRShoulderHandler;}/**对方向键进行注册**/GCControllerDirectionPadValueChangedHandler LRThumbHandler = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) {};if (controller.extendedGamepad) {controller.extendedGamepad.leftThumbstick.valueChangedHandler = LRThumbHandler;controller.extendedGamepad.rightThumbstick.valueChangedHandler = LRThumbHandler;}if (controller.gamepad.dpad) {controller.gamepad.dpad.valueChangedHandler = dpadMoveHandler;}
}
参考:《iOS 7 Game Development》
游戏控制器介绍及代码集成相关推荐
- Bhuman工具篇——bush和游戏控制器介绍
Bush 此软件主要来向机器人传编译好的代码,以及部分Config目录下的文件 图中的上面一排1-6的编号为Player,是机器人在Game中的号码,不同于机器人穿的队服上的编号,其中1号必为守门员且 ...
- 扫雷游戏详细介绍及代码
扫雷游戏的界面与二维数组十分相似,所以此次以二维数组构建扫雷游戏. 分成头文件game.h 源文件game.c text.c 游戏视图如下: ----------------------- ...
- python拼图游戏代码的理解_有意思的JS(1)拼图游戏 玩法介绍及其代码实现
我是你们的索儿呀,很幸运我的文章能与你相见,愿萌新能直观的感受到Javascript的趣味性,愿有一定基础者有所收获,愿大佬不吝赐教 拼图游戏是一张图片分为若干块,打乱次序,将其中一块变为空白块,其只 ...
- 突然讨厌做前端,讨厌代码_如何安全清洁讨厌的游戏控制器
突然讨厌做前端,讨厌代码 Roobcio/ShutterstockRoobcio /快门 Game controllers are magnets for dirt and grime. It doe ...
- C# 之 扑克游戏 -- 21点规则介绍和代码实现
C# 之 扑克游戏 -- 21点规则介绍和代码实现 一,游戏介绍 1.1 游戏规则 1.2 牌点计算 1.3 判断胜负 二,游戏设计 2.1 游戏流程 2.2 玩家类 2.3 AI类 三,参考代码 一 ...
- 拼图游戏 玩法介绍及其代码实现(有意思的JS 一)
我是你们的索儿呀,很幸运我的文章能与你相见 愿萌新能直观的感受到Javascript的趣味性,愿有一定基础者有所收获,愿大佬不吝赐教 拼图游戏是一张图片分为若干块,打乱次序,将其中一块变为空白块,其只 ...
- 借助近距离通信,实现手机作为游戏控制器掌控大屏游戏
一.前言 随着移动互联网时代的深入发展,人们对电子产品的体验效果要求越来越高,各种游戏体验和效果都得到很大的提升.本文主要介绍使用手机或者平板作为游戏控制器,借助近距离通信(Nearby Servic ...
- 西门子S7-200系列可编程控制器介绍
西门子S7-200系列可编程控制器介绍 本章要点 l 西门子S7-200 CPU224可编程控制器的结构.性能指标 l 西门子S7-200 CPU224可编程控制器工作方式 l 扩展模块介绍 l S7 ...
- 第1部分: 游戏引擎介绍, 渲染和构造3D世界
原文作者:Jake Simpson 译者: 向海 Email:GameWorldChina@myway.com ------------------------------------------- ...
最新文章
- ORA-24247: network access denied by access control list (ACL)
- 计算机专业术语graphui,计算机专业术语英译
- 10.1 掌握大数据-机器学习笔记-斯坦福吴恩达教授
- 第二章 搜索 【未完结】
- 6,synchronized, lock 区别
- python之递归函数和内建函数
- 安装distro ./test.sh 找不到路径
- Qt:Qt实现飞秋拦截助手—ARP攻击
- php 开启coredump,GDB分析PHP连接Memcached 导致coredump问题
- S5PV210体系结构与接口05:时钟系统编程
- mysql查找有小数点的数据_MySQL中查询中位数?
- 什么才是年轻人需要的手机?看完OPPO R11就有了答案!
- Sparsity and Some Basics of L1 Regularization
- C#生成JSON数据格式的函数
- 计算机图形学之阴影解读
- js获取汉字拼音首字母
- Html5餐饮管理app,哗啦啦餐饮软件 餐饮管理系统
- PS4 Pro 拆机记
- Java的图片处理工具类(放缩、切割、水印等)
- Logstash 中type 和 tags