Cocos2D:从另一个类获取场景中图层的位置 [英] Cocos2D: Get layer's position in scene from another class

查看:278
本文介绍了Cocos2D:从另一个类获取场景中图层的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to retrieve the current position of the layer added as a child in a scene while a scene transition is occurring. The transition is an edited Cocos2D transition that slides the layer off the screen while a new one appears. I created my own implementation inside CCActionEase with an update method:

#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.大概是因为我得到了相对于自己的位置?

However these return 0 when the transition occurs. Presumably because I'm getting the position relative to itself?

我发现这行不通.通过执行以下操作指向正确的班级,我已经正确访问了当前场景:

I found out this will not work. I've correctly accessed my current scene by pointing to the right class by doing this:

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

JoinedMapsScene * scene = (JoinedMapsScene *)[[CCDirector sharedDirector] runningScene];

并通过执行以下操作调用我的假定方法:

And calling my supposed method by doing this:

[场景getJoinedMapsLayerPosition];

[scene getJoinedMapsLayerPosition];

令我惊讶的是,在过渡期间,当前的runningScene是我的过渡类!

To my surprise, while the transition is happening, the current runningScene is my transition class!

它给了我这个例外: -[ExitLTransition getJoinedMapsLayerPosition]:无法识别的选择器已发送到实例0x5e4e20

It gives my this exception: -[ExitLTransition getJoinedMapsLayerPosition]: unrecognized selector sent to instance 0x5e4e20

我需要找出一种替代方法.

I need to figure out an alternate way of doing this.

推荐答案

将其添加到AppDelegate.h中:

Add this in AppDelegate.h :

@class CCLayer;

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

}

@property (nonatomic, retain) CCLayer *currentLayer;

在AppDelegate.mm中添加它:

Add this in AppDelegate.mm :

@implementation AppController
@synthesize currentLayer = mCurrentLayer;

在您的Layer初始化类中使用它.在所有场景中使用方法.

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

这篇关于Cocos2D:从另一个类获取场景中图层的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆