2013年10月24日 星期四

ipad及iphone預設的啓動背景圖

參考網頁 http://www.idev101.com/code/User_Interface/launchImages.html

原始官方文件 https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/App-RelatedResources/App-RelatedResources.html#//apple_ref/doc/uid/TP40007072-CH6-SW12


其中藍色字的是預設的檔名。 有了這些檔案後,IOS執行程式後,會自動先顯示這些圖檔,才會開始進入程式去執行,因此可以當作Logo來用。



Screen sizes (in pixels) of the iPhone 4S and iPhone 5















2013年10月23日 星期三

cocos2D基本動作例子

以原本template產生的HelloWorldLayer.m來作修改,原本Hello的Label移除,加入兩個移動的飛機圖,然後令他自行移動。


// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {
      
        // ask director the the window size
        CGSize size = [[CCDirector sharedDirector] winSize];
      
        /*
        // create and initialize a Label
        CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
      
        // position the label on the center of the screen
        label.position =  ccp( size.width /2 , size.height/2 );
        */
      
        CCSprite *spaceCargoShip = [CCSprite spriteWithFile:@"el-plane.png"];
        [spaceCargoShip setPosition:ccp(size.width/2, size.height/4)];
      
        [spaceCargoShip setScale:0.5];
      
        [spaceCargoShip setRotation:270];
      
        [self addChild:spaceCargoShip];
      
        // 原始點往目標點移動,每次移動5點
        id moveAction = [CCMoveTo actionWithDuration:5.0f position:ccp(size.width, size.height)];
                                            //position:ccp(0, size.height/2)];
        [spaceCargoShip runAction:moveAction];
      
      
        CCSprite *spaceCargoShip2 = [CCSprite spriteWithFile:@"url.png"];
        [spaceCargoShip2 setPosition:ccp(size.width/3, size.height/3)];
      
        [spaceCargoShip2 setScale:0.2];
      
        [spaceCargoShip2 setRotation:270];
      
        [self addChild:spaceCargoShip2];
      
        // 原始點往目標點移動,每次移動5點
        id moveAction2 = [CCMoveTo actionWithDuration:5.0f position:ccp(0, size.height)];
      
        [spaceCargoShip2 runAction:moveAction2];
        

      
      
        // add the label as a child to this Layer
        //[self addChild: label];
    }
    return self;
}


結果截圖


2013年10月22日 星期二

cocos2D基本範例資訊

由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]; // 橫向設定後,字才會是橫向的


2013年10月21日 星期一

安裝cocos2d

為了寫出更好的遊戲,嘗試使用cocos2D,所以就先試著安裝cocos2d

1. 先到官網 Cocos2D 下載目前的最新版  cocos2d-iphone-2.1.tar.gz  ,或者較舊的版本cocos2d-iphone-1.0.1.tar.gz,兩者皆可,可同時安裝。

2. 下載完後,先將檔案放在一個自建的目錄如Cocos2D,然後點擊檔案後自動解壓縮,就會看到所產生的目錄cocos2d-iphone-1.0.1,或cocos2d-iphone-2.1。

3. 使用終端機terminal



4. 使用cd 到解壓縮之後的子目錄下面,如.../Cocos2D/cocos2d-iphone-1.0.1,然後打入 sudo ./install-templates.sh ,會出現方框需要輸入你的密碼,就會安裝所需的檔案(templates)到Xcode的目錄下了。


5. 如果Xcode在安裝時是正在使用中,就需要關閉後再啓動Xcode,這樣才能使用。


6.  在Xcode下使用File>New Project,就會看見template,左邊上面是cocos2d-1.01,下面是cocos2d-2.1,右邊是1.01的三個template,2.1有四個template。


7. 選取一個來作Hello World的測試,因為是template,所以產生後就立刻可以Run了。如果剛升級到Xcode5.0,因為template是針對Xcode4.0所做的,因此要調整一下LLVM到5.0,不然會無法執行。

 8. 順利執行Hello World在iphone5S的Simulator上