如何停止然后在cocos2D中的节点上重新启动操作

时间:2011-12-06 01:49:19

标签: iphone ios cocos2d-iphone

我正在为iOS创建一个符文转换应用程序,我正试图让当前施法位置的动作从符文传播中的第一个位置切换到第二个位置。

在放置第一个符文之后,当前位置基本上会移出屏幕而不会执行动作我正试图将其移动到。

我正在尝试使用的符号的代码:

    #import "CastingLayer.h"
    #import "RuneScene.h"


    @implementation CastingLayer

    @synthesize rotateSymbol;

-(id) init
{
    if ((self = [super init]))
    {
        self.isTouchEnabled = YES;

        castingLayerPosition = self.position;

        //CGSize screenSize = [[CCDirector sharedDirector] winSize];


        //______________________________________
        //RotateSymbol code for when it's needed
        //--------------------------------------

        CGSize screenSize = [[CCDirector sharedDirector] winSize];

        rotateSymbol = [CCSprite spriteWithFile:@"rotatedSymbol.png"];
        [self addChild:rotateSymbol z:0 tag:1];
        first = CGPointMake(screenSize.width * (5.0f/12.0f), screenSize.height * 0.5f);
        second = CGPointMake(screenSize.width * 0.5f, screenSize.height * 0.5f);
        third = CGPointMake(screenSize.width * 0.20f, screenSize.height * 0.20f);
        fourth = CGPointMake(screenSize.width * 0.20f, screenSize.height * 0.10f);
        fifth = CGPointMake(screenSize.width * 0.20f, screenSize.height * 0.40f);
        sixth = CGPointMake(screenSize.width * 0.20f, screenSize.height * 0.60f);

        //rotateSymbol.position = CGPointMake(screenSize.width * (5.0f/12.0f), screenSize.height * 0.5f);


        [self symbolSpawnWithPosition:first];

        //runeArray = [NSMutableArray arrayWithCapacity:8];

       //rune test
        for (int i = 0; i < 8; i++) {
            //rune = [Runes runeWithParentNode:self andTexture:[NSString stringWithFormat:@"amathyst%i.png",i]];
            //[runeArray insertObject:rune atIndex:i];
        }
        rune0 = [Runes runeWithParentNode:self withTexture:@"amathyst0.png" withRuneNum:Rune0];
        rune1 = [Runes runeWithParentNode:self withTexture:@"amathyst1.png" withRuneNum:Rune1];
        rune2 = [Runes runeWithParentNode:self withTexture:@"amathyst2.png" withRuneNum:Rune2];
        rune3 = [Runes runeWithParentNode:self withTexture:@"amathyst3.png" withRuneNum:Rune3];
        rune4 = [Runes runeWithParentNode:self withTexture:@"amathyst4.png" withRuneNum:Rune4];
        rune5 = [Runes runeWithParentNode:self withTexture:@"amathyst5.png" withRuneNum:Rune5];
        rune6 = [Runes runeWithParentNode:self withTexture:@"amathyst6.png" withRuneNum:Rune6];
        rune7 = [Runes runeWithParentNode:self withTexture:@"amathyst7.png" withRuneNum:Rune7];
        [self symbolSpawnWithPosition:first];
        [self schedule:@selector(updateSymbol:) interval:0.1f];
    }
    return self;
}

-(void) symbolSpawnWithPosition:(CGPoint)point{
    //CGSize screenSize = [[CCDirector sharedDirector] winSize];
    [rotateSymbol stopAllActions];
    rotateSymbol.position = point;


    CCRotateBy* rotate = [CCRotateBy actionWithDuration:2 angle:-360];
    CCFadeIn* fade = [CCFadeIn actionWithDuration:1];
    CCSpawn* spawn = [CCSpawn actionOne:fade two:rotate];
    CCRepeatForever* repeat = [CCRepeatForever actionWithAction:rotate];

    [rotateSymbol runAction:spawn];
    [rotateSymbol runAction:repeat];
}
-(void) updateSymbol:(ccTime)delta{
    //static int currentRune = 0;

    Runes* currentRune = (Runes*)[self getChildByTag:current];


    if ([currentRune runeIsPlaced]) {
        CCFadeOut* fadeOut = [CCFadeOut actionWithDuration:1];
        [rotateSymbol runAction:fadeOut];
        [self symbolSpawnWithPosition:second];

        switch (current) {
            case Rune0:
                current = Rune1;
                break;
            case Rune1:
                current = Rune2;
                break;
            case Rune2:
                current = Rune3;
                break;
            case Rune3:
                current = Rune4;
                break;
            case Rune4:
                current = Rune5;
                break;
            case Rune5:
                current = Rune6;
                break;
            case Rune6:
                current = Rune7;
                break;
            case Rune7:
                current = none;
                break;

            default:
                break;
        }
    }
}

这是我自己尝试制作的第一个应用程序,所以请原谅,如果它不是最好的代码。

但是到目前为止我所做的是使用我设想的标签创建符文,这样我就可以在需要时获取对象。我想要反复使用spawn动作,我把它们放在自己的方法中,这个方法在初始化rotateSymbol之后就被调用了。

它适用于启动,所有操作都能正常工作,但是当我放置第一个符文并尝试转换到第二个位置时,符号会突然出现并且不能满足我的需要。

感谢您提供的任何帮助:)

1 个答案:

答案 0 :(得分:0)

我不确定你要做什么,但从描述来看,你似乎在滥用行动。使用动作最简单的方法就是一次性,忘记 - 它们很轻巧,可以轻松使用和丢弃。而是重新使用它们只是重新创建它们。

另外看看使用CCSequence它会让你排队几个动作一个接一个地发生。