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



我正在为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;
        }
    }
}

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

但是到目前为止,我所做的是创建带有我枚举的标记的符文,以便我可以在需要时获得对象。因为我想一遍又一遍地使用刷出动作,我把它们放在它们自己的方法中,这个方法在rotatesymsymbol初始化后被调用。

它在启动时工作,所有的动作都工作得很好,但是当我放置第一个符文并试图过渡到第二个位置时,符号就会崩溃并且不做我需要它做的事情。

谢谢你的帮助:)

我不确定你想做什么,但从描述来看,你似乎在滥用行动。使用动作最简单的方法是一次性使用,即弃即弃——它们是轻量级的,可以很容易地使用和丢弃。而不是重新使用它们,只是重新创建它们。

也看看使用CCSequence,它会让你排队几个动作发生一个接一个。

最新更新