代码改变世界

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

2012-01-20 16:27  张智清  阅读(964)  评论(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];