cocos2d - 翻转两个Sprite

用两个图片初始化两个CCSprite,一样的大小,重叠在一起,分别叫做 foregroundNode 和 backgroundNode 。

- (void)flipover
{
    if (isFliping) {
        return;
    }
    [self chooseCard];
}

-(void) chooseCard
{
    isFliping = YES;
    CCSprite *fore = nil;
    CCSprite *back = nil;
    if (flipovered) {
        fore = self.foregroundNode;
        back = self.backgroundNode;
    }else{
        fore = self.backgroundNode;
        back = self.foregroundNode;
    }

    //始终保证前面的图像是未横向翻转过的,后面的图像是翻转过的
    if ([back flipX] == NO) {
        [back setFlipX:YES];
    }
    if ([fore flipX] == YES) {
        [fore setFlipX:NO];
    }
    
    //执行动画的时间
    float orbitTime = 0.3;
    
    //前景图像转动90度不可见
    [fore runAction:[CCSequence actions:[CCOrbitCamera actionWithDuration:orbitTime radius: 1 deltaRadius:0 angleZ:0 deltaAngleZ:90 angleX:0 deltaAngleX:0],[CCCallFunc actionWithTarget:self selector:@selector(showCard:)], nil]];
    
    //背景图像同步旋转,当前景图像停止动画时,背景图像继续旋转90度,此时图像左右是翻转的,为了保证看着的图像是正常的,所以在执行动画前要翻转一次
    [back runAction:[CCSequence actions:[CCOrbitCamera actionWithDuration:orbitTime*2 radius:1 deltaRadius:0 angleZ:0 deltaAngleZ:180 angleX:0 deltaAngleX:0], [CCCallFunc actionWithTarget:self selector:@selector(flipFinished)], nil]];
}

// 翻转后,调整前后景图像的层次
-(void)showCard:(id)sender
{
    if (flipovered) {
        [self.foregroundNode setZOrder:0];
        [self.backgroundNode setZOrder:1];
    }else{
        [self.foregroundNode setZOrder:1];
        [self.backgroundNode setZOrder:0];
    }
    flipovered = !flipovered;
}

- (void)flipFinished
{
    isFliping = NO;
}

 

posted @ 2013-12-05 16:52  有妄想症的猫zz  阅读(3239)  评论(0编辑  收藏  举报