0

我试图在 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

4

2 回答 2

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 的期望。

于 2013-10-27T22:09:11.180 回答
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]];
}
于 2013-10-31T11:45:03.703 回答