cocos2d游戏开发中一些设置与代码
2012-01-20 16:27 张智清 阅读(966) 评论(0) 编辑 收藏 举报一、开启Retina高清支持
在cocos2d的AppDelegate.m文件中默认是将高清模式注释屏蔽掉的,需要我们手动取消注释后开启[director enableRetinaDisplay:YES]。
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
// if( ! [director enableRetinaDisplay:YES] )
// CCLOG(@"Retina Display Not supported");
二、获取当前iOS设备屏幕宽高
CGSize size = [[CCDirector sharedDirector] winSize];
三、改变游戏速度
[[CCScheduler sharedScheduler] setTimeScale:2.0f]; //设为正常的2倍
四、禁止自动锁屏
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
五、获取触摸点在整个屏幕中的坐标
// 返回一个UIView相关的坐标viewPoint
CGPoint viewPoint = [touch locationInView:[touch view]];
// 获得触摸点在整个屏幕中的坐标
[[CCDirector sharedDirector] convertToGL:viewPoint];
// 这三行,标准获取屏幕坐标并转换
// 首先获取触摸的屏幕坐标,是UIKit里的坐标系
CGPoint touchLocation = [touch locationInView:[touch view]];
// 其次将屏幕坐标转换成OpenGL坐标
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
// 最后把OpenGL坐标转换成所需的CCLayer坐标
touchLocation = [self convertToNodeSpace:touchLocation];
六、获取触摸点相对于某个精灵的坐标
CGPoint touchPoint = [sprite convertTouchToNodeSpaceAR:touch];
七、游戏中背景音乐的循环播放
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background.wav" loop:YES]
八、开启摄像头增强现实感
// 准备overlay视图,并添加到window上
overlay = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
overlay.opaque = NO;
overlay.backgroundColor = [UIColor clearColor];
[window addSubview:overlay];
// 开启摄像头来增强现实
#define CAMERA_TRANSFORM 1.24299 //定义一常量来缩放摄像头,使之与屏幕匹配(摄像头比率4:3,而iPhone屏幕3:4)
UIImagePickerController *uip;
@try {
uip = [[[UIImagePickerController alloc] init] autorelease];
uip.sourceType = UIImagePickerControllerSourceTypeCamera;
uip.showsCameraControls = NO;
uip.toolbarHidden = YES;
uip.navigationBarHidden = YES;
uip.wantsFullScreenLayout = YES;
uip.cameraViewTransform = CGAffineTransformScale(uip.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
}
@catch (NSException *exception) {
[uip release];
uip = nil;
}
@finally {
if (uip) {
[overlay addSubview:[uip view]];
[overlay release];
}
}
[window bringSubviewToFront:viewController.view];
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架