2

With Cocos2D V3 after action has completed on a sprite I need to update data contained in a structure. How can I pass a data structure address to a selector that executes after my sprite action has completed? Any help much appreciated.

4

2 回答 2

4

你会使用 aCCActionCallBlock或 aCCActionCallFunc。无论哪个适合您的用例。

这是一个在移动到操作后调用块的代码示例。

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

这是一个使用 a在移动操作之后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];
于 2014-03-24T18:59:24.683 回答
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
于 2014-04-20T10:05:52.193 回答