Cocos2d iphone Cocos2D V3没有CCCallFunnd如何将结构地址传递给选择器?
在精灵上完成操作后,使用CoCoS2DV3,我需要更新结构中包含的数据。如何将数据结构地址传递给在sprite操作完成后执行的选择器?Cocos2d iphone Cocos2D V3没有CCCallFunnd如何将结构地址传递给选择器?,cocos2d-iphone,ccaction,Cocos2d Iphone,Ccaction,在精灵上完成操作后,使用CoCoS2DV3,我需要更新结构中包含的数据。如何将数据结构地址传递给在sprite操作完成后执行的选择器? 非常感谢您的帮助。您可以使用cActionCallBlock或cActionCallFunc。以适合您的用例的为准 下面是一个代码示例,说明在移动到操作后调用的块 CCActionMoveBy* moveToSomeAwesomePlace = [CCActionMoveBy actionWithDuration:0.1f position:CGPointZer
非常感谢您的帮助。您可以使用
cActionCallBlock
或cActionCallFunc
。以适合您的用例的为准
下面是一个代码示例,说明在移动到操作后调用的块
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];
虽然我建议继续使用积木, 这是我的cActionCallFunnd的ARC友好版本。可以处理多种数据,包括原语和任何目标C类,最多是NSArray等。。。任何意见,以改善它,甚至更好是wellcome
// 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
//CCActionCallFuncND.h
#导入“CCActionInstant.h”
typedef void(*CC_CALLBACK_ND)(id、SEL、id、void*);
@接口CCActionCallFunnd:CCActionCallFunc
+(id)actionWithTarget:(id)t选择器:(SEL)s数据:(void*)d;
-(id)initWithTarget:(id)t选择器:(SEL)s数据:(void*)d;
@结束
//CCActionCallFuncND.m
#进口
#导入“CCActionCallFuncND.h”
#进口
@接口CCActionCallFunnd()
@属性(强、非原子、读写)id myObject;
@属性(强、非原子、读写)NSValue*myValue;
@属性(非原子,读写)CC\u CALLBACK和callbackND;
@结束
@实现CCActionCallFunnd
+(id)actionWithTarget:(id)t选择器:(SEL)s数据:(void*)d
{
return[[self alloc]initWithTarget:t选择器:s数据:d];
}
-(id)initWithTarget:(id)t选择器:(SEL)s数据:(void*)d
{
if((self=[super initWithTarget:t选择器: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);
对于(int i=0;您的示例中的iCCActionCallBlock在移动操作完成后不调用选择器。您能否更新您的示例以说明这一点。我也为此添加了一个示例。V3的CCActionCallFunc不允许将数据传递给选择器。原始问题尚未回答。移动操作完成后,我需要调用一个选择器,并将一个数据值(即数据结构的地址)传递给该选择器。您能给出一个例子吗?为什么不在块中调用该方法呢?