如何在按下UIViewController时启动Cocos2D场景

时间:2013-01-23 15:26:02

标签: ios cocos2d-iphone draw

我正在开发iOS 5和6的项目,iPad 3。

我的问题是我需要在场景中用Cocos2D绘制一些东西,但它不起作用。

我开始在AppDelegate中编写代码 - 设置一个rootViewController(MainViewController)

在这个MainViewController中,我有一个按钮,它会触发一个名为-draw的方法:

-draw:方法非常简单。

DrawController * c = [DrawController alloc] init]; [self.navigationController pushViewController:c animated:YES];

现在,当App推送DrawController时, 我的应用程序应该使用CCDirector与Cocos2D建立一个场景。

但它非常令人沮丧,因为我的屏幕上总是有一个黑色的间隙。

DrawController

- (void)viewDidLoad
{
[super viewDidLoad];

glView = [CCGLView viewWithFrame:[self.view bounds]
                     pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8
                     depthFormat:0  //GL_DEPTH_COMPONENT24_OES
              preserveBackbuffer:NO
                      sharegroup:nil
                   multiSampling:NO
                 numberOfSamples:0];

director_ =  (CCDirectorIOS*) [CCDirector sharedDirector];

director_.wantsFullScreenLayout = YES;

// Display FSP and SPF
[director_ setDisplayStats:YES];

// set FPS at 60
[director_ setAnimationInterval:1.0/60];


// attach the openglView to the director
[director_ setView:glView];


// for rotation and other messages
[director_ setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];



UIButton * redColor = [UIButton buttonWithType:UIButtonTypeRoundedRect];
redColor.tag = 1;
redColor.frame = CGRectMake(658, 80, 100, 30);
[redColor setTitle:@"ROT" forState:UIControlStateNormal];
[redColor addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
[glView addSubview:redColor];

UIButton * blueColor = [UIButton buttonWithType:UIButtonTypeRoundedRect];
blueColor.tag = 2;
blueColor.frame = CGRectMake(658, 80+30+30, 100, 30);
[blueColor setTitle:@"BLAU" forState:UIControlStateNormal];
[blueColor addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
[glView addSubview:blueColor];

UIButton * undo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
undo.tag = 1;
undo.frame = CGRectMake(658, 80 +30+30 +30+30, 100, 30);
[undo setTitle:@"UNDO" forState:UIControlStateNormal];
[undo addTarget:self action:@selector(changeHistory:) forControlEvents:UIControlEventTouchUpInside];
[glView addSubview:undo];

UIButton * foto = [UIButton buttonWithType:UIButtonTypeRoundedRect];
foto.tag = 1;
foto.frame = CGRectMake(658, 80 +30+30 +30+30 +30+30, 100, 30);
[foto setTitle:@"FOTO" forState:UIControlStateNormal];
[foto addTarget:self action:@selector(takeScreenshot:) forControlEvents:UIControlEventTouchUpInside];
[glView addSubview:foto];

UIStepper * overdrawStepper = [[UIStepper alloc] initWithFrame:CGRectMake(658, 80 +30+30 +30+30 +30+30 +30+30, 100, 30)];
overdrawStepper.value = 1;
overdrawStepper.minimumValue = 1;
overdrawStepper.maximumValue = 18;
overdrawStepper.stepValue = 1;
[overdrawStepper addTarget:self action:@selector(overdraw:) forControlEvents:UIControlEventValueChanged];

[glView addSubview:overdrawStepper];


UIStepper * thicknessStepper = [[UIStepper alloc] initWithFrame:CGRectMake(658, 80 +30+30 +30+30 +30+30 +30+30 +30+30, 100, 30)];
thicknessStepper.value = 3;
thicknessStepper.minimumValue = 1;
thicknessStepper.maximumValue = 30;
thicknessStepper.stepValue = 1;
[thicknessStepper addTarget:self action:@selector(thickness:) forControlEvents:UIControlEventValueChanged];

[glView addSubview:thicknessStepper];


UIStepper * alphaStepper = [[UIStepper alloc] initWithFrame:CGRectMake(658, 80 +30+30 +30+30 +30+30 +30+30 +30+30 +30+30, 100, 30)];
alphaStepper.value = 1;
alphaStepper.minimumValue = 0.1;
alphaStepper.maximumValue = 1;
alphaStepper.stepValue = 0.1;
[alphaStepper addTarget:self action:@selector(alpha:) forControlEvents:UIControlEventValueChanged];

[glView addSubview:alphaStepper];

UIButton * eraser = [UIButton buttonWithType:UIButtonTypeRoundedRect];
eraser.tag = 1;
eraser.frame = CGRectMake(658, 80 +30+30 +30+30 +30+30 +30+30 +30+30 +30+30 +30+30, 100, 30);
[eraser setTitle:@"Eraser" forState:UIControlStateNormal];
[eraser addTarget:self action:@selector(erase:) forControlEvents:UIControlEventTouchUpInside];
[glView addSubview:eraser];



// 2D projection
[director_ setProjection:kCCDirectorProjection2D];
//  [director setProjection:kCCDirectorProjection3D];

// 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");


// set the Navigation Controller as the root view controller
// there is a iOS 6 Bug, so we need have this workaround


[self addChildViewController:director_];

// Add the director's OpenGL view as a subview so we can see it.
[self.view addSubview:director_.view];

// Finish up our view controller containment responsibilities.
[director_ didMoveToParentViewController:self];


//        director_.view.frame = CGRectMake(0, 0, 1024, 1024);

// 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];

// Assume that PVR images have premultiplied alpha
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];

// and add the scene to the stack. The director will run it when it automatically when the view is displayed.


self.view.backgroundColor = [UIColor orangeColor];

if ([self loadImage]) {
    lineDrawer = [[LineDrawer alloc] initWithImage:[self loadImage]];
} else {
    lineDrawer = [LineDrawer node];
}


CCScene *scene = [CCScene node];
//[scene setContentSize:glView.frame.size];
[scene addChild:lineDrawer];
[director_ pushScene: scene];

NSLog(@"self.view.frame %@", NSStringFromCGRect(self.view.frame));

// Do any additional setup after loading the view.



#import "cocos2d.h"

@interface LineDrawer : CCLayer

- (id)initWithImage:(UIImage*)imageName;

// change the color of line
- (void)changeLineColorToRed:(float)r_ green:(float)g_ blue:(float)b_ alpha:(float)a_;

// the undo function to call. When you undo the last line, the also will be an change in line color
- (void)undo;

// get the actual drawing from screen
- (UIImage*)getScreenShot;

// property change methods
- (void)changeOverdraw:(float)overdraw_;
- (void)changeThikness:(int)thikness_;
- (void)changeAlpha:(float)alpha_;

@end

- (id)initWithImage:(UIImage *)image
{
self = [super init];
if (self) {
    points = [NSMutableArray array];
    velocities = [NSMutableArray array];
    circlesPoints = [NSMutableArray array];

    _undoManager = [[RMUndoManager alloc] init];


    shaderProgram_ = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor];
    overdraw = 1.0f;

    CCTexture2D * imageTexture = [[CCTexture2D alloc] initWithCGImage:image.CGImage
                                                       resolutionType:kCCResolutioniPadRetinaDisplay];

    CCSprite *backGroundSprite = [[CCSprite alloc] initWithTexture: imageTexture];
    [backGroundSprite setPosition:CGPointMake(768/2, 1024/2)];

    renderTexture = [[CCRenderTexture alloc] initWithWidth:(int)self.contentSize.width
                                                    height:(int)self.contentSize.height
                                               pixelFormat:kCCTexture2DPixelFormat_RGBA8888];

    renderTexture.anchorPoint = ccp(0,0);

    renderTexture.position = ccp(1024 * 0.5f, 768 * 0.5f);

    renderTexture.sprite = backGroundSprite;


    [[renderTexture sprite] setBlendFunc:(ccBlendFunc){GL_ONE, GL_ONE_MINUS_SRC_ALPHA}];

    [renderTexture clear:1.0f g:1.0f b:1.0f a:0];


    [renderTexture begin];
    [backGroundSprite visit];
    [renderTexture end];

    [self addChild:renderTexture];



    self.isTouchEnabled = YES;

    ccColor4F c = {0, 0, 190.0f/255.0f, 1};
    drawColor = c;

    thikness = 3;

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                           action:@selector(handlePanGesture:)];
    panGestureRecognizer.maximumNumberOfTouches = 1;
    [self addGestureRecognizer:panGestureRecognizer];

    UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                                                                             action:@selector(handleLongPress:)];
    [self addGestureRecognizer:longPressGestureRecognizer];

    UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                            action:@selector(handleTap:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tapGestureRecognizer];
}
return self;
}

有人可以帮我这个吗?

https://dl.dropbox.com/u/15710714/iOS%20Simulator%20Bildschirmfoto%2023.01.2013%2016.17.57.png

1 个答案:

答案 0 :(得分:0)

只需添加[director_ startAnimation]; 低于[director_ pushScene: scene];