2010-08-27 107 views
2

我将ParticleDemo实现中的代码复制到我的项目中,并试图使其工作。我希望当我点击图像时,它应该改变/替换场景。出于某种原因,现场并未被取代。下面是我的实现:iPhone Cocos2d加载场景

// 
// HelloWorldLayer.h 
// FirstCocoaApplication 
// 
// Created by Mohammad Azam on 8/27/10. 
// Copyright HighOnCoding 2010. All rights reserved. 
// 

// When you import this file, you import all the cocos2d classes 
#import "cocos2d.h" 

// HelloWorld Layer 
@interface HelloWorld : CCLayer 
{ 
} 

// returns a Scene that contains the HelloWorld as the only child 
+(id) scene; 

@end 

@class Emitter; 

@interface ParticleDemo : CCColorLayer 
{ 
CCParticleSystem *emitter; 
CCSprite *background; 
} 

@property (readwrite, retain) CCParticleSystem *emitter; 

@end 

@interface DemoFire : ParticleDemo 
{} 
@end 

这里是.m文件:

// 
// HelloWorldLayer.m 
// FirstCocoaApplication 
// 
// Created by Mohammad Azam on 8/27/10. 
// Copyright HighOnCoding 2010. All rights reserved. 
// 

// Import the interfaces 
#import "HelloWorldScene.h" 

Class nextAction() 
{ 
    NSLog(@"next action fired"); 

    NSString *r = @"DemoFire"; 
    Class c = NSClassFromString(r); 

    return c; 
} 

@implementation ParticleDemo 

@synthesize emitter; 

-(id) init 
{ 
    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if((self=[super init])) { 

     // create and initialize a Label 
     //CCLabel* label = [CCLabel labelWithString:@"Particle Demo" fontName:@"Marker Felt" fontSize:64]; 

     //CCScene *s = [CCScene node]; 

CCMenuItemImage *item1 = [CCMenuItemImage itemFromNormalImage:@"f1.png" selectedImage:@"f1.png" target:self selector:@selector(nextCallback:)]; 

     CGSize s = [[CCDirector sharedDirector] winSize]; 

     CCMenu *menu = [CCMenu menuWithItems:item1,nil]; 
     menu.position = CGPointZero; 
     item1.position = ccp(s.width/2 - 100,30); 

     [self addChild:menu z:100]; 

     //[s addChild: [nextAction() node]]; 
     //[[CCDirector sharedDirector] replaceScene: s]; 

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

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

-(void) nextCallback: (id) sender 
{ 
    NSLog(@"nextCallback fired!"); 

    CCScene *s = [CCScene node]; 
    [s addChild: [nextAction() node]]; 
    [[CCDirector sharedDirector] replaceScene: s]; 
} 

-(void) setEmitterPosition 
{ 
    if(CGPointEqualToPoint(emitter.centerOfGravity, CGPointZero)) 
     emitter.position = ccp(200,70); 
} 

@end 

@implementation DemoFire 

-(void) onEnter 
{ 
    NSLog(@"demo fire onenter fred"); 

    [super onEnter]; 
    self.emitter = [CCParticleFire node]; 
    [background addChild:emitter z:10]; 

    emitter.texture = [[CCTextureCache sharedTextureCache] addImage:@"fire.pvr"]; 
    CGPoint p = emitter.position; 
    emitter.position = ccp(p.x, 100); 

    [self setEmitterPosition]; 
} 

@end 

// HelloWorld implementation 
@implementation HelloWorld 

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

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

    // 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 
{ 
    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if((self=[super init])) { 

     // create and initialize a Label 
     CCLabel* label = [CCLabel 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 

// CLASS IMPLEMENTATIONS 
@implementation AppController 

- (void) applicationDidFinishLaunching:(UIApplication*)application 
{ 
    // CC_DIRECTOR_INIT() 
    // 
    // 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer 
    // 2. EAGLView multiple touches: disabled 
    // 3. creates a UIWindow, and assign it to the "window" var (it must already be declared) 
    // 4. Parents EAGLView to the newly created window 
    // 5. Creates Display Link Director 
    // 5a. If it fails, it will use an NSTimer director 
    // 6. It will try to run at 60 FPS 
    // 7. Display FPS: NO 
    // 8. Device orientation: Portrait 
    // 9. Connects the director to the EAGLView 
    // 
// CC_DIRECTOR_INIT(); 

    // Obtain the shared director in order to... 
    CCDirector *director = [CCDirector sharedDirector]; 

    // Turn on display FPS 
    [director setDisplayFPS:YES]; 

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

    CCScene *scene = [CCScene node]; 
    [scene addChild: [nextAction() node]]; 

    [director runWithScene: scene]; 
} 

// getting a call, pause the game 
-(void) applicationWillResignActive:(UIApplication *)application 
{ 
    [[CCDirector sharedDirector] pause]; 
} 

// call got rejected 
-(void) applicationDidBecomeActive:(UIApplication *)application 
{ 
    [[CCDirector sharedDirector] resume]; 
} 

-(void) applicationDidEnterBackground:(UIApplication*)application 
{ 
    [[CCDirector sharedDirector] stopAnimation]; 
} 

-(void) applicationWillEnterForeground:(UIApplication*)application 
{ 
    [[CCDirector sharedDirector] startAnimation]; 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    [[CCDirector sharedDirector] end]; 
} 

// purge memory 
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application 
{ 
    [[CCDirector sharedDirector] purgeCachedData]; 
} 

// next delta time will be zero 
-(void) applicationSignificantTimeChange:(UIApplication *)application 
{ 
    [[CCDirector sharedDirector] setNextDeltaTimeZero:YES]; 
} 

@end 

在AppDelegate的文件我已经安装加载ParticleDemo为当前场景!

回答

2

cocos2d测试项目是非常糟糕的例子来启动一个项目。他们的目的是测试代码的某些方面,以及您应该查看并可能复制的那些方面。尽管如此,设置cocos2d测试用例的方式并不是一个好的,干净的项目。有很多捷径,比如把所有东西放到一个文件中,并依靠左/右按钮来改变测试用例(我相信这是nextAction类的起作用)。

我建议你安装的cocos2d Xcode项目模板,并与cocos2d的应用程序项目启动: http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/15838-how-to-install-the-cocos2d-xcode-project-templates/

我从你的代码中所看到的是,该层没有

self.isTouchEnabled = YES 

,它也没有将常规触摸输入事件方法(ccTouchesBegan等)添加到它。所以它从来不会对触摸输入做出反应,更不用说调用任何调用replaceScene的方法。

+0

感谢您的回答!其实我从空白板开始,并添加了一些Cocos2d演示代码。我捕获MenuItem点击事件,它会触发: CCMenuItemImage * item1 = [CCMenuItemImage itemFromNormalImage:@“f1.png”selectedImage:@“f1.png”target:self selector:@selector(nextCallback :)]; – azamsharp 2010-08-28 16:40:21