3

私はcocos2dが初めてです。私は最初のアプリを書いています。しかし、私には解決できない問題があります。問題は、複数のスプライトを移動すると、最後のスプライト以外は問題ないことです。スプライトが同時にアクションを実行するようにします。しかし、私はそれを修正する方法がわかりません。私のコードの宣伝に関するアイデアやアドバイス。ありがとう。

これが私のコードです。

-(void) setHeadFace:(Face *)headFace moveFrom:(CGPoint)startPosition moveTo:(CGPoint)endPosition`

{

        CCMoveTo *moveOut = [CCMoveTo actionWithDuration:0.2f position:startPosition];
        CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.2f scale:0.0f];
        CCSpawn *moveFrom = [CCSpawn actions:moveOut,scaleTo,nil];

        CCMoveTo *moveTo = [CCMoveTo actionWithDuration:0.0f position:endPosition];

        CCSequence *headAction = [CCSequence actions:moveFrom,moveTo,nil];
        [headFace.faceSprite runAction:headAction];
}

-(void) setMidlFace:(Face *)curtFace moveTo:(CGPoint)movePosition nextFace:(Face *)nextFace

{

        CCMoveTo *moveTo = [CCMoveTo actionWithDuration:0.2f position:movePosition];

        CCSequence *moveAction = [CCSequence actions:moveTo,nil];
        [curtFace.faceSprite runAction:moveAction];

        nextFace.faceSprite = curtFace.faceSprite;
        nextFace.faceType = curtFace.faceType;
        [nextFace.faceSprite setPosition:movePosition];
}

-(void) setTailFace:(Face *)tailFace moveTo:(CGPoint)movePosition 
                            byGuardSprite:(CCSprite *)guardSprite`
{

        CCDelayTime *delay = [CCDelayTime actionWithDuration:0.2f];
        CCMoveTo *moveIn = [CCMoveTo actionWithDuration:0.2f position:movePosition];
        CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.2f scale:1.0f];
        CCSpawn *moveTo = [CCSpawn actions:moveIn,scaleTo,nil];

        CCCallBlock *touchBlock =[CCCallBlock actionWithBlock:^{
                tailFace.faceSprite = guardSprite;
                tailFace.faceType = guardSprite.tag;
                tailFace.faceSprite.tag = -1;

                isTouch = YES;
        }];

        CCSequence *tailAction=[CCSequence actions:delay,moveTo,touchBlock,nil];
        [guardSprite runAction:tailAction];
}

-(void) moveFaces:(Face *)face direction:(int)direction`

{

        CCSprite *guardSprite = nil;

        CGPoint movePosition;
        CGPoint curtPosition;
        CGPoint startPosition;
        CGPoint endPosition;

        int x = face.cdX;
        int y = face.cdY;

        CGFloat width = faceGrid[x][0].position.x;
        CGFloat height = faceGrid[0][y].position.y;

        switch (direction) {
        case MOVE_DOWN:            
            CCLOG(@"move down:direction %d", direction);

            movePosition = CGPointMake(width,faceGrid[x][0].position.y);

            //move the first face out of the grid
            startPosition = CGPointMake(width,faceGrid[x][0].position.y-GRID_OFFSET.y/2);
            endPosition = CGPointMake(width,faceGrid[x][GRID_HEIGHT-1].position.y+GRID_OFFSET.y/2);

            [self setHeadFace:faceGrid[x][0] moveFrom:startPosition moveTo:endPosition];

            guardSprite = faceGrid[x][0].faceSprite;
            guardSprite.tag = faceGrid[x][0].faceType;

            for (int j=1; j<GRID_HEIGHT; j++)
            {
                curtPosition=CGPointMake(width,faceGrid[x][j].position.y);

                //move the middle face to the next face
                [self setMidlFace:faceGrid[x][j] moveTo:movePosition nextFace:faceGrid[x][j-1]];

                movePosition = curtPosition;
            }

            //use the guard face to set the last face's move action
            [self setTailFace:faceGrid[x][GRID_HEIGHT-1] moveTo:movePosition byGuardSprite:guardSprite];

            break;
            ......
}
4

1 に答える 1

0

同じ目的で tick 関数を使用しました。

-(void)onEnter
{
    [super onEnter];

    self.isTouchEnabled = true;

    [self schedule: @selector(tick:)];
}


-(void) tick: (ccTime) dt
{    
    CGPoint pos = head.position ; 

    head.position = ccp(pos.x+gameSpeed, pos.y);

    pos = tail.position ;
    tail.position = ccp(pos.x+gameSpeed, pos.y);

}
于 2012-11-04T16:05:02.700 に答える