如何在基于UIKit的应用程序中在UIViewController中加载Cocos2d动画



可以在UIViewController中混合iOS的普通xcode项目中的一个基于cocos2d的动画?

我正在尝试使用UIImageView制作的动画,并尝试使用cocos2d。我有两个放置动画的视图。

是否可以创建CC2dAnimationObject以便在UIViewController中使用?

谢谢。

已解决-

大家好,我是个快乐的人。我明白了。

我已经创建了两个类,一个是CCScoreBoardLG(名称不是决定性的),这个类是UIViewController的子类,并使用对象OTAnimationCC2d加载EAGLView,该对象是动画的容器。

CCScoreBoardLG可以放置在UIKit视图控制器中的任何位置,背景是透明的,以便放置动画并查看其背后的内容。

有些零件是硬编码的,因为测试的目的,但很容易适应加载任何类型的动画。动画被放置在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技术。如果任何CC2d大师发现错误或不正确的事情,我很抱歉。这对我有用。

否,不直接。您必须手动更新cocos2d节点的所有动画属性,并在每帧将更改应用于相应的UIView属性。

有一个CCIViewWrapper类可以帮助您做到这一点,至少它看起来是一个不错的模板,您可以根据需要进行修改。

Cocos2D 2.x中的CCDirector是基于UIViewController的。

因此,您可以像添加任何其他UIView一样添加它的视图。

最新更新