2012-02-22 5 views
3

UIViewControllerのiOS用の通常のxcodeプロジェクトに、cocos2dに基づく1つのアニメーションを混在させることは可能ですか?UIKitベースのアプリケーションでUIViewController内にCocos2dアニメーションを読み込む方法は?

私はUIImageViewによって行われたアニメーションの問題を実験しています。代わりにcocos2dを使用しようとしています。私はアニメーションが配置されているビューのペアを持っています。

UIViewControllerで使用するためにCC2dAnimationObjectを作成することはできますか?

ありがとうございました。

が解決しよう -

こんにちはすべてを、私は幸せな男です。私はアプローチを得る。

CCScoreBoardLG(名前は確定していません)のペアを作成しました。このクラスはUIViewControllerのサブクラスであり、アニメーションのコンテナであるオブジェクトOTAnimationCC2dを持つEAGLViewを読み込みます。

CCScoreBoardLGは、UIKit View Controllerのどこにでも配置することができ、バックグラウンドはアニメーションを配置し、背後にあるコンテンツを見るために透明です。

一部の部分はテスト目的でハードコードされていますが、どのような種類のアニメーションをロードするのにも簡単に適応できます。アニメーションはCCSceneの中央に配置され、ループモードになります。ここで

クラス:

OTAnimationCC2d.h

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 

@interface OTAnimationCC2d : CCLayer{ 

} 
+(id) scene; 
@end 

OTAnimationCC2d.m

#import "OTAnimationCC2d.h" 


@implementation OTAnimationCC2d 
+(id) scene 
{ 
    // 'scene' is an autorelease object. 
    CCScene *scene = [CCScene node]; 

    // 'layer' is an autorelease object. 
    OTAnimationCC2d *layer = [OTAnimationCC2d node]; 

    // add layer as a child to scene 
    [scene addChild: layer]; 

    // return the scene 
    return scene; 
} 
-(id)init { 
    self = [super init]; 

    if (self) { 

     [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"OTScoreBoard_LG_Atlas_Team_1_4.plist"]; 
     CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"OTScoreBoard_LG_Atlas_Team_1_4.png"]; 
     [self addChild:spriteSheet]; 

     NSMutableArray *animFrames = [NSMutableArray array]; 
     for (int i = 1; i <= 50; i++) { 
      NSString *file; 

      if (i<10) 
       file = [NSString stringWithFormat:@"Frame_00%d.png", i]; 
      else if (i<100) 
       file = [NSString stringWithFormat:@"Frame_0%d.png", i]; 
      else if (i<1000) 
       file = [NSString stringWithFormat:@"Frame_%d.png", i]; 

      CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:file]; 

      [animFrames addObject:frame]; 
     } 

     CGSize s = [[CCDirector sharedDirector] winSize]; 

     CCSprite *player = [CCSprite spriteWithSpriteFrameName:@"Frame_001.png"]; 
     player.rotation = -90; 
     player.position = ccp(s.width/2,s.height/2); 

     [spriteSheet addChild:player]; 

     CCAnimation *anim = [CCAnimation animationWithFrames:animFrames delay:0.05f]; 
     CCRepeatForever *repeat = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]]; 
     [player runAction:repeat]; 
    } 

    return self; 
} 
@end 

CCScoreBoardLG.h

#import <UIKit/UIKit.h> 
#import "cocos2d.h" 
#import "CCOTConfig.h" 
#import "OTAnimationCC2d.h" 

@interface CCScoreBoardLG : UIViewController 
- (void)startCCLayer; 
@end 

CCScoreBoardLG.m

#import "CCScoreBoardLG.h" 

@implementation CCScoreBoardLG 
- (void) removeStartupFlicker 
{ 
#if GAME_AUTOROTATION == kGameAutorotationUIViewController 

     CC_ENABLE_DEFAULT_GL_STATES(); 
     CCDirector *director = [CCDirector sharedDirector]; 
     CGSize size = [director winSize]; 
     CCSprite *sprite = [CCSprite spriteWithFile:@"OTScoreBoardBackground.png"]; 
     sprite.position = ccp(size.width/2, size.height/2); 
     sprite.rotation = -90; 
     [sprite visit]; 
     [[director openGLView] swapBuffers]; 
     CC_ENABLE_DEFAULT_GL_STATES(); 

#endif 
} 
- (void)startCCLayer 
{ 
    if(! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink]) 
     [CCDirector setDirectorType:kCCDirectorTypeDefault]; 


    CCDirector *director = [CCDirector sharedDirector]; 

    EAGLView *glView = [EAGLView viewWithFrame:[self.view frame] 
            pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8 
            depthFormat:0      // GL_DEPTH_COMPONENT16_OES 
         ]; 

    [director setOpenGLView:glView]; 
    [director openGLView].opaque = NO; 
    [director openGLView].backgroundColor = [UIColor clearColor]; 

    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)[director openGLView].layer; 
    eaglLayer.opaque = NO; 
    eaglLayer.frame = self.view.bounds; 

    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 
    const CGFloat myColor[] = {0.0, 0.0, 0.0, 0.0}; 
    eaglLayer.backgroundColor = CGColorCreate(rgb, myColor); 
    CGColorSpaceRelease(rgb); 

    [director openGLView].opaque = NO; 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 

#if GAME_AUTOROTATION == kGameAutorotationUIViewController 
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft]; 
#else 
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft]; 
#endif 

    [director setAnimationInterval:1.0/60]; 
    [director setDisplayFPS:NO]; 

    [self setView:glView]; 

    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; 

    [self removeStartupFlicker]; 

    [[CCDirector sharedDirector] runWithScene: [OTAnimationCC2d scene]]; 

} 


// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    // 
    // There are 2 ways to support auto-rotation: 
    // - The OpenGL/cocos2d way 
    //  - Faster, but doesn't rotate the UIKit objects 
    // - The ViewController way 
    // - A bit slower, but the UiKit objects are placed in the right place 
    // 

#if GAME_AUTOROTATION==kGameAutorotationNone 
    // 
    // EAGLView won't be autorotated. 
    // Since this method should return YES in at least 1 orientation, 
    // we return YES only in the Portrait orientation 
    // 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

#elif GAME_AUTOROTATION==kGameAutorotationCCDirector 
    // 
    // EAGLView will be rotated by cocos2d 
    // 
    // Sample: Autorotate only in landscape mode 
    // 
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { 
     [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight]; 
    } else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft]; 
    } 

    // Since this method should return YES in at least 1 orientation, 
    // we return YES only in the Portrait orientation 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

#elif GAME_AUTOROTATION == kGameAutorotationUIViewController 
    // 
    // EAGLView will be rotated by the UIViewController 
    // 
    // Sample: Autorotate only in landscpe mode 
    // 
    // return YES for the supported orientations 

    return (UIInterfaceOrientationIsLandscape(interfaceOrientation)); 

#else 
#error Unknown value in GAME_AUTOROTATION 

#endif // GAME_AUTOROTATION 


    // Shold not happen 
    return NO; 
} 

// 
// This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController 
// 
#if GAME_AUTOROTATION == kGameAutorotationUIViewController 
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    // 
    // Assuming that the main window has the size of the screen 
    // BUG: This won't work if the EAGLView is not fullscreen 
    /// 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGRect rect = CGRectZero; 


    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)  
     rect = screenRect; 

    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
     rect.size = CGSizeMake(screenRect.size.height, screenRect.size.width); 

    CCDirector *director = [CCDirector sharedDirector]; 
    EAGLView *glView = [director openGLView]; 
    float contentScaleFactor = [director contentScaleFactor]; 

    if(contentScaleFactor != 1) { 
     rect.size.width *= contentScaleFactor; 
     rect.size.height *= contentScaleFactor; 
    } 
    glView.frame = rect; 
} 
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

使用方法。私たちがアニメーションを配置したい場所からCC2d:

CCScoreBoardLG *LG = [[CCScoreBoardLG alloc] init]; 
LG.view.frame = CGRectMake(10, 300, 1004, 300); 
[LG.view setBackgroundColor:[UIColor clearColor]]; 
[self.view addSubview:LG.view]; 
[LG startCCLayer]; 

私はCC2dとGLのテクニックに絶対にnoobです。 CC2dの教師が誤りや正しいものが見つからないのは残念です。これは私のために働く。

+0

+1あなたのソリューションは、私にとって同じ問題を解決するのに役立ちました。ありがとう!! – Myxtic

答えて

0

いいえ、直接ではありません。 cocos2dノードのアニメーション化されたすべてのプロパティを手動で更新し、その変更を対応するUIViewプロパティにフレームごとに適用する必要があります。

少なくとも、あなたのニーズに合わせて変更できる素敵なテンプレートのように見えるかもしれませんが、CCUIViewWrapperクラスがあります。

+1

こんにちは、応答のおかげで、これは私がやっているのと反対です。このラッパーは、UIViewをCocos2dシーンに挿入するようです。私の場合は逆、UIViewにcocos2dシーンを挿入する方法です。私は、アニメーションを作成するcocos2dクラスを開発しました。私はこのココスのオブジェクトを表示し、[myView addSubview:mycocos.view]のようなビューに追加するにはどうすればいいですか? – NemeSys

0

Cocos2D 2.xのCCDirectorは、UIViewControllerに基づいています。

他のUIViewと同じようにビューを追加できます。

関連する問題