我的CCTargetedAction运行两次

时间:2014-04-07 13:37:29

标签: cocos2d-iphone ccaction

在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

为什么这段代码会执行两次操作?

谢谢。

1 个答案:

答案 0 :(得分:1)

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

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

我指的是这篇文章。 http://cocos2d-x.org/forums/6/topics/39546