由template產生的範例,有一些內部設定可以瞭解一下,當作未來修改的參考
原始檔案列表
1. GameConfig.h
//
// Supported Autorotations:
// None,
// UIViewController,
// CCDirector
//
#define kGameAutorotationNone 0
//不使用自動旋轉
#define kGameAutorotationCCDirector 1
//速度考量使用,不匹配UIKit元件
#define kGameAutorotationUIViewController 2
// 相容性考量(基本設定)
//
// Define here the type of autorotation that you want for your game
//
// 3rd generation and newer devices: Rotate using UIViewController. Rotation should be supported on iPad apps.
// TIP:
// To improve the performance, you should set this value to "kGameAutorotationNone" or "kGameAutorotationCCDirector"
#if defined(__ARM_NEON__) || TARGET_IPHONE_SIMULATOR
#define GAME_AUTOROTATION kGameAutorotationUIViewController
// ARMv6 (1st and 2nd generation devices): Don't rotate. It is very expensive
#elif __arm__
#define GAME_AUTOROTATION kGameAutorotationNone
// Ignore this value on Mac
#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
#else
#error(unknown architecture)
#endif
2. AppDelegate.m,所有cocos2d的基本設定都在此處完成
#import "cocos2d.h"
#import "AppDelegate.h"
#import "GameConfig.h"
#import "HelloWorldLayer.h"
#import "RootViewController.h"
@implementation AppDelegate
@synthesize window;
- (void) removeStartupFlicker
{
//
// THIS CODE REMOVES THE STARTUP FLICKER
//
// Uncomment the following code if you Application only supports landscape mode
//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
// 此處的作用還是不清楚,書面資料是要取代原來cocos2D的圖片顯示,但並沒有成功顯示出成果???
// 還是因為啓動顯示還是直立的關係,此處是要避免橫向啓動時的閃動。 CC_ENABLE_DEFAULT_GL_STATES();
CCDirector *director = [CCDirector sharedDirector];
CGSize size = [director winSize];
CCSprite *sprite = [CCSprite spriteWithFile:@"travel.png"];
sprite.position = ccp(size.width/2, size.height/2);
sprite.rotation = -90;
[sprite visit];
[[director openGLView] swapBuffers];
CC_ENABLE_DEFAULT_GL_STATES();
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController
}
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
//確保設定cocos2D成功 CCDirector *director = [CCDirector sharedDirector];
// 呼叫sharedDirector來 初始化director // Init the View Controller 初始 viewController
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
//
// Create the EAGLView manually
建立EAGLView給遊戲畫面(OPENGLES設定基本運作格式) // 1. Create a RGB565 format. Alternative: RGBA8
// 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
//
//
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
// attach the openglView to the director
[director setOpenGLView:glView];
// 將OPENGLES畫面設定給予director // // 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");
//
// VERY IMPORTANT:
// If the rotation is going to be controlled by a UIViewController
// then the device orientation should be "Portrait".
//
// IMPORTANT:
// By default, this template only supports Landscape orientations.
// Edit the RootViewController.m file to edit the supported orientations.
//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
// 設定原始Device的方向,原始為縱向,可改為橫向 //[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft]; // 橫向設定後,字才會是橫向的,此處是自己為了橫向顯示而加的,所以mark起來
#else [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
[director setAnimationInterval:1.0/60];
// frame rate 為60 [director setDisplayFPS:YES];
// 顯示現在frame rate的數值,非Debug必要時可關閉 // make the OpenGLView a child of the view controller
[viewController setView:glView];
// 設定glView為viewController的子元件,為了畫面的計算 // make the View Controller a child of the main window
[window addSubview: viewController.view];
// 將圖顯是在畫面上 [window makeKeyAndVisible];
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
預設使用最高位元來顯示貼圖 [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// Removes the startup flicker
[self removeStartupFlicker];
// 在橫向顯示啓動時會有閃動畫面,因此需要使用此函數 // Run the intro Scene ,執行HelloWorldLayer這個scene //[[CCDirector sharedDirector] runWithScene: [HelloWorldLayer scene]];
[director runWithScene: [HelloWorldLayer scene]];
}
//執行順序 applicationWillResignActive -> applicationDidEnterBackground- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
// ipad或iphone螢幕關上時,暫停運作}
// 執行順序 applicationWillEnterForeground -> applicationDidBecomeActive- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
// ipad或iphone螢幕開啓時,重新運作}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[CCDirector sharedDirector] purgeCachedData];
// 記憶體不足時,丟棄無用的貼圖及字型資料}
-(void) applicationDidEnterBackground:(UIApplication*)application {
[[CCDirector sharedDirector] stopAnimation];
// 程式被移到背景時,暫停運作}
-(void) applicationWillEnterForeground:(UIApplication*)application {
[[CCDirector sharedDirector] startAnimation];
// 程式回到前景時,重新運作}
// director被移除時,停止運作,釋放記憶體- (void)applicationWillTerminate:(UIApplication *)application {
CCDirector *director = [CCDirector sharedDirector];
[[director openGLView] removeFromSuperview];
[viewController release];
[window release];
[director end];
}
- (void)applicationSignificantTimeChange:(UIApplication *)application {
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
// 設定呼叫事件之間的時間差為零,避免不正常行為。}
- (void)dealloc {
[[CCDirector sharedDirector] end];
[window release];
[super dealloc];
}
@end
3. RootViewController.m 基本狀態下不會進入其中的子程式,因此不看了
4. HelloWorldLayer.m,一個scene的物件,作為顯示一組動作元件之用
// Import the interfaces
#import "HelloWorldLayer.h"
// HelloWorldLayer implementation
@implementation HelloWorldLayer
+(CCScene *) scene
// 設定HelloWorldLayer 的scene並回傳後使用,初始化HelloWorldLayer之用
// 其中至少要包含了一組以上的Layer{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 設定scene是一個CCNode的物件 // CCScene <- CCNode // 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// 設定layer是一個CCNode的物件 // 因為HelloWorldLayer <- CCLayer <- CCNode 繼承關係
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
// 外部呼叫此子程式來使用HelloWorldLayer的scene,進而顯示動作效果{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
// 在此Layer下,所有動作設定的位置。如要加入新的顯示元件,也是在此。 // create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
}
return self;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
@end
5. 縱向及橫向結果顯示
[director setDeviceOrientation:kCCDeviceOrientationPortrait]; // 設定原始Device的方向,原始為縱向,可改為橫向
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft]; // 橫向設定後,字才會是橫向的