代码改变世界

cocos2d游戏开发中一些设置与代码

  张智清  阅读(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];
复制代码



编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示