博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[译]Chipmunk教程 - 3 初始化
阅读量:7251 次
发布时间:2019-06-29

本文共 1885 字,大约阅读时间需要 6 分钟。

初始化Chipmunk需要三件事情要去做:

  1. 初始化它
  2. 使用一个 timer 来让Chipmunk计算模拟器的步骤。
  3. 创建并且配置Space

 

初始化Chipmunk是很简单的一部分,你只需要调用cpInitChipmunk 函数就行了,把它放在程序初始化的地方。时间的设置,使用一个简单的NSTimer对象,或者一些你想要使用的游戏引擎。也许你要用的Timer就在引擎自身里面。最后,创建一个新的Space,只需要使用cpSpaceNew方法就行了。

为了完成这三步,只需要再你的controller文件,引入chipmunk.h头文件就好了。

 
#import "chipmunk.h"
之后,在文件的开始,定义两个可以存储我们space的变量,以及两个方法,当头文件定义完了以后,将会看到如下的效果:
#import 
#import "chipmunk.h" @interface ChipmunkTutorialViewController : UIViewController { UIImageView *floor; // Holds our floor image UIImageView *ball; // Holds our ball image cpSpace *space; // Holds our Space object } - (void)setupChipmunk; // Bootstraps chipmunk and the timer - (void)tick:(NSTimer *)timer; // Fires at each "frame" @end
在实现文件里面,viewDidLoad调用这个方法。:
[self setupChipmunk];
最后,实现我们声明的两个方法:
// Bootsraps chipmunk and the timer    - (void)setupChipmunk {  // Start chipmunk    cpInitChipmunk();  // Create a space object    space = cpSpaceNew();  // Define a gravity vector    space->gravity = cpv(0, -100);  // Creates a timer firing at a constant interval (desired frame rate)  // Note that if you are using too much CPU the real frame rate will be lower and  // the timer might fire before the last frame was complete.  // There are techniques you can use to avoid this but I won't approach them here.    [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];  }  // Called at each "frame" of the simulation    - (void)tick:(NSTimer *)timer {  // Tell Chipmunk to take another "step" in the simulation    cpSpaceStep(space, 1.0f/60.0f);  }

转载地址:http://pjhbm.baihongyu.com/

你可能感兴趣的文章
JS 设计模式二(封装)
查看>>
JavaScript “跑马灯”抽奖活动代码解析与优化(一)
查看>>
为什么我们选择 segmentfault 写作?
查看>>
多模型融合推荐算法在达观数据的运用
查看>>
JDK 11 马上就要来了!JDK 12 还会远吗?
查看>>
Kali Linux 2019.1 发布,Metasploit 更新到 5.0 版本
查看>>
【mysql的设计与优化专题(1)】ER图,数据建模与数据字典
查看>>
Jibo’s Name: How did we pick it?
查看>>
device's media capture mechanism,利用input:file调用设备的照相机/相册、摄像机、录音机...
查看>>
BroadLink:三款新品力求无障碍人机交互,三大平台分三期对外开放 ...
查看>>
掌门1对1获3.5亿美元E-1轮融资,华人文化产业基金、中金甲子基金等投资 ...
查看>>
Unity中的通用对象池
查看>>
ORA-00600: internal error code, arguments: [16703], [1403], [28], [...
查看>>
忆芯科技发布新一代国产主控芯片STAR1000P!4月完成量产版本 ...
查看>>
如何用条码标签打印软件实现商品价签制定会员价 ...
查看>>
如何轻松实现个性化推荐系统
查看>>
Mysql高级查询 内连接和外连接详解
查看>>
基于AWS的电子商务网站架构——Web前端
查看>>
基于险企传统资源优势的“一核三环”规划——互联网平台建设
查看>>
社交网络:有意义的不仅是邓巴数
查看>>