【问题标题】:Cocos2D V3 does not have CCCallFuncND How pass structure address to selector?Cocos2D V3 没有 CCCallFuncND 如何将结构体地址传递给选择器?
【发布时间】:2014-03-24 16:17:22
【问题描述】:

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

【问题讨论】:

    标签: cocos2d-iphone ccaction


    【解决方案1】:

    您可以使用CCActionCallBlockCCActionCallFunc。无论哪个适合您的用例。

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

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

    【讨论】:

    • 您的示例中的 CCActionCallBlock 在移动操作完成后不会调用选择器。您能否更新您的示例以显示这一点。
    • 我也为此添加了一个示例。
    • CCActionCallFunc V3 不允许将数据传递给选择器。原来的问题没有回答。移动操作完成后,我需要调用一个选择器并将一个数据值(即数据结构的地址)传递给该选择器。你能举个例子吗?
    • 为什么不直接调用块中的方法呢?
    【解决方案2】:

    虽然我建议继续使用积木, 这是我的 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
    

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2015-03-31
      • 2022-01-18
      相关资源
      最近更新 更多