0

在 cocos2d v3 中,我找不到像 CCTargetedAction 这样的东西。在我的项目中是必需的,所以我从 cocos2d v2 复制了代码。

@interface CCTargetedAction : CCActionInterval

/** This is the target that the action will be forced to run with */
@property(readwrite,nonatomic,retain) id forcedTarget;
@property(readwrite,nonatomic,retain) CCActionFiniteTime* action;

/** Create an action with the specified action and forced target */
+(id)actionWithTarget:(id)target
               action:(CCActionFiniteTime*)action;

/** Init an action with the specified action and forced target */
-(id)initWithTarget:(id)target
             action:(CCActionFiniteTime*)action;
@end
@implementation CCTargetedAction

+(id)actionWithTarget:(id)target
               action:(CCActionFiniteTime*)action
{
    return [(CCTargetedAction*)[self alloc] initWithTarget:target
                                                    action:action];
}
-(id)initWithTarget:(id)target
             action:(CCActionFiniteTime*)action
{
    self = [super initWithDuration:action.duration];
    if(self)
    {
        self.forcedTarget = target;
        self.action = action;
    }
    return self;
}

-(id)copyWithZone:(NSZone*)zone
{
    CCAction *copy = [(CCTargetedAction*) [[self class] allocWithZone: zone]
                      initWithTarget:_forcedTarget
                      action:[_action copy]];
    return copy;
}

- (void) startWithTarget:(id)aTarget
{
    [super startWithTarget:aTarget];
    [_action startWithTarget:_forcedTarget];
}

- (void) stop
{
    [_action stop];
}

- (void) update:(CCTime) time
{
    [_action update:time];
}
@end

但是我的 CCTargetedAction 运行了两次操作。

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CCActionCallBlock* block = [CCActionCallBlock actionWithBlock:^{
        CCLOG(@"call block");
    }];
    CCTargetedAction* action = [CCTargetedAction actionWithTarget:self
                                                           action:block];
    [self runAction:action];
}

如果我触摸屏幕一次,则会输出两次日志消息。

2014-04-07 22:09:57.439 TargetedActionTest[3924:60b] call block
2014-04-07 22:09:57.455 TargetedActionTest[3924:60b] call block

为什么这段代码运行两次?

谢谢你。

4

1 回答 1

1

这个问题通过覆盖 -(BOOL)isDone 方法来解决。

-(BOOL)isDone
{
    return [_action isDone];
}

我指的是这个帖子。 http://cocos2d-x.org/forums/6/topics/39546

于 2014-04-23T11:05:41.437 回答