[英]Prevent Cocos2D touches during animation?
In this animation here: 在此动画中:
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
self.isTouchEnabled = NO;
if (scoreLabel.opacity == 225) {
NSLog(@"fadeOut");
CCSequence *fadeOut = [CCSequence actions:[CCFadeOut actionWithDuration:0.5], [CCCallFuncN actionWithTarget:self
selector:@selector(enableTouches)], nil];
[scoreLabel runAction:fadeOut];
[livesLabel runAction:[[fadeOut copy] autorelease]];
}
else {
NSLog(@"fadeIn");
CCSequence *fadeIn = [CCSequence actions:[CCFadeIn actionWithDuration:0.5], [CCCallFuncN actionWithTarget:self
selector:@selector(enableTouches)], nil];
[scoreLabel runAction:fadeIn];
[livesLabel runAction:[[fadeIn copy] autorelease]];
}
}
I am trying to simply fade out/in labels. 我试图淡出标签。 The thing is though, I want to make sure that this method won't get called while the labels are animating.
事实是,我想确保标签动画时不会调用此方法。
If you look in the code, I attempt to do that by calling this method: 如果您在代码中查找,我会尝试通过调用以下方法来做到这一点:
- (void)enableTouches {
NSLog(@"ET");
self.isTouchEnabled = YES;
}
But it does not seem to work. 但这似乎不起作用。 If I touch the screen while the labels are animating, it messes up the animation midway through and doesn't do what I want.
如果在给标签添加动画效果时触摸屏幕,则会使动画中途混乱,并且无法执行我想要的操作。
Any ideas? 有任何想法吗?
Thanks! 谢谢!
I ended up doing this in case anyone has the same issue: 如果有人遇到相同的问题,我最终会这样做:
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
self.isTouchEnabled = NO;
if(label1.opacity == 0 )
{
CCFadeIn* fadeIn = [CCFadeIn actionWithDuration:0.5];
CCCallBlock* fadeCompleted = [CCCallBlock actionWithBlock:^{ self.isTouchEnabled = YES; }];
[label1 runAction:[CCSequence actions:fadeIn, fadeCompleted, nil]];
[label2 runAction:[[fadeIn copy] autorelease]];
}
else
{
CCFadeOut* fadeOut = [CCFadeOut actionWithDuration:0.5];
CCCallBlock* fadeCompleted = [CCCallBlock actionWithBlock:^{ self.isTouchEnabled = YES; }];
[label1 runAction:[CCSequence actions:fadeOut, fadeCompleted, nil]];
[label2 runAction:[[fadeOut copy] autorelease]];
}
}
In case you didn't know, you can assign tags to actions the same way you can assign tags to nodes. 如果您不知道,则可以将标签分配给操作,就像分配标签给节点一样。 Then you can call getActionByTag: on the object that is running the action to either get nil or a pointer to the action.
然后,您可以在运行操作的对象上调用getActionByTag:以获得nil或指向该操作的指针。 By checking if the return value is nil or an action you will know if the certain action/animation you are looking for is playing.
通过检查返回值是否为nil或某个动作,您将知道您正在寻找的某个动作/动画是否正在播放。 That way you might be able to do this without additional variables.
这样,您可能可以在没有其他变量的情况下执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.