Cocos2D V3没有CCCallFuncND如何将结构地址传递给选择器?

时间:2014-03-24 16:17:22

标签: cocos2d-iphone ccaction

使用Cocos2D V3在精灵上完成操作后,我需要更新结构中包含的数据。如何将数据结构地址传递给在精灵操作完成后执行的选择器? 任何帮助非常感谢。

2 个答案:

答案 0 :(得分:4)

您可以使用CCActionCallBlockCCActionCallFunc。适用于您的用例。

以下是在采取行动后调用的的代码示例。

CCActionMoveBy* moveToSomeAwesomePlace = [CCActionMoveBy actionWithDuration:0.1f position:CGPointZero];
CCActionCallBlock *actionAfterMoving = [CCActionCallBlock actionWithBlock:^{
    self.someProperty = 42;
}];
CCActionSequence *movingSequeceAndOtherStuffAfter = [CCActionSequence actionWithArray:@[moveToSomeAwesomePlace, actionAfterMoving]];
[self runAction:movingSequeceAndOtherStuffAfter];

以下是使用CCActionCallFunc执行移动操作后执行选择器的示例。

CCActionMoveBy* moveToSomeAwesomePlace = [CCActionMoveBy actionWithDuration:0.1f position:CGPointZero];
CCActionCallFunc *callAfterMoving = [CCActionCallFunc actionWithTarget:self selector:@selector(someMethod)];
CCActionSequence *movingSequeceAndOtherStuffAfter = [CCActionSequence actionWithArray:@[moveToSomeAwesomePlace, actionAfterMoving]];
[self runAction:movingSequeceAndOtherStuffAfter];

答案 1 :(得分:1)

虽然我建议继续使用积木, 这是我对ARC友好版本的CCActionCallFuncND。适用于多种数据,包括原始数据,以及任何客观的C类,至多NSArray等等......任何改进它的评论都会更好!

//  CCActionCallFuncND.h

#import "CCActionInstant.h"

typedef void (*CC_CALLBACK_ND)(id, SEL, id, void *);

@interface CCActionCallFuncND : CCActionCallFunc<NSCopying>


+(id) actionWithTarget:(id)t selector:(SEL)s data:(void*)d;
-(id) initWithTarget:(id)t selector:(SEL)s data:(void*)d;

@end


//  CCActionCallFuncND.m


#import <malloc/malloc.h>
#import "CCActionCallFuncND.h"
#import <objc/runtime.h>

@interface CCActionCallFuncND ()

@property(strong, nonatomic, readwrite)id myObject;
@property(strong, nonatomic, readwrite)NSValue *myValue;
@property(nonatomic, readwrite)CC_CALLBACK_ND callbackND;

@end

@implementation CCActionCallFuncND


+(id)actionWithTarget:(id)t selector:(SEL)s data:(void*)d
{
    return [[self alloc] initWithTarget:t selector:s data:d];
}

-(id)initWithTarget:(id)t selector:(SEL)s data:(void*)d
{
    if((self=[super initWithTarget:t selector:s]))
    {

        self.myValue=[NSValue valueWithPointer:d];
        self.myObject=nil;


        bool bIsOfClass=false;

        int classesNumber=objc_getClassList(NULL, 0);
        Class *classesList = (Class*)malloc(classesNumber*sizeof(Class));

        classesNumber = objc_getClassList(classesList, classesNumber);
        for(int i=0; i<classesNumber; i++)
        {
            if(classesList[i]==*((Class *)d))
            {
                bIsOfClass=true;
                break;
            }
        }

        free(classesList);

        if(bIsOfClass)
        {
            self.myValue=nil;
            self.myObject=(__bridge id)d;
        }

        self.callbackND=(CC_CALLBACK_ND)[t methodForSelector:s];
    }


    return self;
}



-(id)copyWithZone:(NSZone*)zone
{
    CCActionInstant *copy = [[[self class] allocWithZone: zone]
        initWithTarget:_targetCallback selector:_selector data:[self.myValue pointerValue]];
    return copy;
}

-(void)execute
{

    void *dat=self.myValue?(void*)[self.myValue pointerValue]:(__bridge void*)self.myObject;
    self.callbackND(_targetCallback, _selector, _target, dat);

}

@end