0

我正在尝试在发生场景转换时检索在场景中作为子级添加的图层的当前位置。过渡是经过编辑的 Cocos2D 过渡,它会在出现新图层时将图层滑出屏幕。我使用更新方法在 CCActionEase 中创建了自己的实现:

#import "JoinedMapsScene.h"
#import "JoinedMapsLayer.h"

    @implementation CCEaseInWithPercentMult
    -(void) update: (ccTime) t
    {

        [other update: powf(t,rate)];

        CCScene * scene = [[CCDirector sharedDirector] runningScene];

        CCNode* layer = [scene getChildByTag:0];

        NSLog(@"% .2f",layer.position.x); //returns 0 
        NSLog(@"% .2f",layer.position.y); //returns 0 
    }

但是,当转换发生时,这些返回 0。大概是因为我得到了相对于自身的位置?

编辑:

我发现这行不通。通过这样做,我已经通过指向正确的类正确地访问了我当前的场景:

JoinedMapsScene * 场景 = (JoinedMapsScene *)[[CCDirector sharedDirector] runningScene];

并通过这样做来调用我假设的方法:

[场景获取JoinedMapsLayerPosition];

令我惊讶的是,当转换发生时,当前的 runningScene 是我的转换类!

它给了我这个例外:-[ExitLTransition getJoinedMapsLayerPosition]: unrecognized selector sent to instance 0x5e4e20

我需要找出另一种方法来做到这一点。

4

1 回答 1

0

Add this in AppDelegate.h :

@class CCLayer;

@interface AppController : NSObject <UIApplicationDelegate, CCDirectorDelegate,UIGestureRecognizerDelegate>
{
    CCLayer             *mCurrentLayer;

}

@property (nonatomic, retain) CCLayer *currentLayer;

Add this in AppDelegate.mm :

@implementation AppController
@synthesize currentLayer = mCurrentLayer;

In your Layer init class use this. In all scene method.

@implementation MyMainMenu

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

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

    AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
    app.currentLayer = layer;

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

    // return the scene
    return scene;
}

You can check anywhere in project..

 AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

 if([app.currentLayer  isKindOfClass:[MyMainMenu class]])
 {
     MyMainMenu *mm = (MyMainMenu*) app.currentLayer;
     [mm calFunction]; 
 }
于 2012-10-08T18:47:28.320 回答