在2个精灵与不同父母之间交换位置

时间:2013-10-27 20:45:28

标签: ios cocos2d-iphone

我试图在spriteA和SpriteB之间交换位置。他们有父母:parentA和parentB。我也在改变父母。 所以,如果我只是交换没有动画,它的工作正常。 但是如果我运行CCMoveTo动作,精灵会在动作开始时处理,然后移动到指定的位置。我需要的是所以他们不要眨眼,从他们的位置移动到指定的位置。父进程正在旋转,并且子进程没有旋转,因为我使用child.rotation = -child.parent.protation。 这是我的代码

-(void)makeSwapBetweenSelectedShape:(ShapeSprite*)firstShape secondShape:(ShapeSprite*)secondShape{
CGPoint positionOfFirst=[firstShape.parent convertToNodeSpace:firstShape.position];
CGPoint positionOfSecond=secondShape.position;

NSLog(@"POSITION BEFORE 1st %f %f",[firstShape.parent convertToWorldSpace:firstShape.position].x,[firstShape.parent convertToWorldSpace:firstShape.position].y);
NSLog(@"POSITION BEFORE 2nd %f %f",[secondShape.parent convertToWorldSpace:secondShape.position].x,[secondShape.parent convertToWorldSpace:secondShape.position].y);   

//swapping parents
CCNode* tempParent=firstShape.parent;
[firstShape removeFromParentAndCleanup:NO];
[secondShape.parent addChild:firstShape];
[firstShape runAction:[CCSequence actions:[CCMoveTo actionWithDuration:1.1 position:positionOfSecond],[CCCallBlockN actionWithBlock:^(CCNode* node){
 NSLog(@"POSITION AFTER ANIMATION 1st %f %f ",[node.parent convertToWorldSpace:node.position].x,[node.parent convertToWorldSpace:node.position].y);
}],nil]];
NSLog(@"POSITION ON START 1st %f %f ",[firstShape.parent convertToWorldSpace:firstShape.position].x,[firstShape.parent convertToWorldSpace:firstShape.position].y);


[secondShape removeFromParentAndCleanup:NO];
[tempParent addChild:secondShape];

[secondShape runAction:[CCSequence actions:[CCMoveTo actionWithDuration:1.1 position:selectedShapeStartingPosition],[CCCallBlockN actionWithBlock:^(CCNode* node){
    NSLog(@"POSITION AFTER ANIMATION 2st %f %f ",[node.parent convertToWorldSpace:node.position].x,[node.parent convertToWorldSpace:node.position].y);

}],nil]];

NSLog(@"POSITION ON START 2nd %f %f",[secondShape.parent convertToWorldSpace:secondShape.position].x,[secondShape.parent convertToWorldSpace:secondShape.position].y);


}

给我这个日志:

2013-10-30 19:36:54.397 Wheel[1254:a0b] POSITION BEFORE 1st 160.000000 248.000000
2013-10-30 19:36:54.397 Wheel[1254:a0b] POSITION BEFORE 2nd 160.000000 204.000000
2013-10-30 19:36:54.398 Wheel[1254:a0b] POSITION ON START 1st 116.000000 204.000000 
2013-10-30 19:36:54.398 Wheel[1254:a0b] POSITION ON START 2nd 204.000000 248.000000
2013-10-30 19:36:55.533 Wheel[1254:a0b] POSITION AFTER ANIMATION 1st 160.000000 204.000000 
2013-10-30 19:36:55.534 Wheel[1254:a0b] POSITION AFTER ANIMATION 2st 160.000000 248.000000 

编辑:添加了NSLog

2 个答案:

答案 0 :(得分:0)

在cocos中,子位置是在父坐标中指定的,因此,除非parentAparentB具有相同的大小和位置,否则您需要将其子坐标转换为新的父坐标。所以你的代码应该是这样的:

-(void)makeSwapBetweenSelectedShape:(ShapeSprite*)firstShape secondShape(ShapeSprite*)secondShape{
    CGPoint positionOfFirst=firstShape.position;
    CGPoint positionOfSecond=secondShape.position;

    // this is to make firstShape position be represented in secondShape parents coordinates
    CGPoint positionOfFirstTranslated = [secondShape.parent convertToNodeSpace:positionOfFirst];

    //analogically
    CGPoint positionOfSecondTranslated = [firstShape.parent convertToNodeSpace:positionOfSecond];

    //swapping parents
    CCNode* tempParent=firstShape.parent;
    [firstShape removeFromParentAndCleanup:NO];
    [firstShape setPosition:positionOfFirstTranslated];
    [secondShape.parent addChild:firstShape];
    [firstShape runAction:[CCMoveTo actionWithDuration:1.1 position:positionOfSecond]];
    [secondShape removeFromParentAndCleanup:NO];
    [secondShape setPosition:positionOfSecondTranslated];
    [tempParent addChild:secondShape];
    [secondShape runAction:[CCMoveTo actionWithDuration:1.1 position:positionOfFirst]];
}

编辑:根据评论更改代码以符合OP的期望。

答案 1 :(得分:0)

我终于做到了。 正确的代码是:

-(void)makeSwapBetweenSelectedShape:(ShapeSprite*)firstShape secondShape:(ShapeSprite*)secondShape{

    CGPoint positionOfFirst=firstShape.position;
    CGPoint positionOfSecond=secondShape.position;

    CGPoint worldPositionOfFirst=[firstShape.parent convertToWorldSpace:firstShape.position];
    CGPoint worldPositionOfSecond=[secondShape.parent convertToWorldSpace:secondShape.position];

    //swapping parents
    CCNode* tempParent=firstShape.parent;
    [firstShape removeFromParentAndCleanup:NO];
    [secondShape.parent addChild:firstShape];
    firstShape.position=[firstShape.parent convertToNodeSpace:worldPositionOfFirst];
    [firstShape runAction:[CCMoveTo actionWithDuration:1.5 position:positionOfSecond]];


    [secondShape removeFromParentAndCleanup:NO];
    [tempParent addChild:secondShape];
    secondShape.position=[secondShape.parent convertToNodeSpace:worldPositionOfSecond];
    [secondShape runAction:[CCMoveTo actionWithDuration:1.5 position:positionOfFirst]];
}