CC Follow和Parallax Node工作很奇怪

时间:2012-12-11 02:17:52

标签: objective-c ios cocos2d-iphone

我是新手,试图整合CCsprite以跟随一层视差节点。 它假设遵循bg04精灵。 事实证明.....,是的,它遵循,但在相反的方向。

这是代码,请帮助,谢谢!

-(void) addParaBG
{


CCSprite* bg01 = [CCSprite spriteWithFile:@"Page001bg01.png"];
CCSprite* bg02 = [CCSprite spriteWithFile:@"Page001bg02.png"];
CCSprite* bg03 = [CCSprite spriteWithFile:@"Page001bg03.png"];
CCSprite* bg04 = [CCSprite spriteWithFile:@"Page001bg04.png"];

bg01.anchorPoint = CGPointMake(0, 0);
bg02.anchorPoint = CGPointMake(0, 0);
bg03.anchorPoint = CGPointMake(0, 0);
bg04.anchorPoint = CGPointMake(0, 0);

bg01.tag = 01;
bg02.tag = 02;
bg03.tag = 03;
bg04.tag = 04;

CCParallaxNode* paraBG = [CCParallaxNode node];
[paraBG addChild:bg01 z:1 parallaxRatio:CGPointMake(1, 0) positionOffset:CGPointMake(0, 0)];
[paraBG addChild:bg02 z:2 parallaxRatio:CGPointMake(2, 0) positionOffset:CGPointMake(0, 0)];
[paraBG addChild:bg03 z:3 parallaxRatio:CGPointMake(3, 0) positionOffset:CGPointMake(0, 0)];
[paraBG addChild:bg04 z:4 parallaxRatio:CGPointMake(4, 0) positionOffset:CGPointMake(0, 0)];

[self addChild:paraBG z:0 tag:paraBGtag];
}

-(void) moveParaBG
{
CCMoveBy* move1 = [CCMoveBy actionWithDuration:15 position:CGPointMake(-160, 0)];
CCMoveBy* move2 = [CCMoveBy actionWithDuration:15 position:CGPointMake(160, 0)];
CCSequence* sequence = [CCSequence actions:move1, move2, nil];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence];

[[self getChildByTag:paraBGtag] runAction:repeat];
}

-(void) addAnimationElement
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CCSprite* testship = [CCSprite spriteWithFile:@"ship.png"];
testship.position = CGPointMake(screenSize.width /2, screenSize.height /2);
[self addChild:testship];

CCAnimation* anim = [CCAnimation animationWithFile:@"ship-anim" frameCount:5 delay:0.08f];
CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];

[testship runAction:repeat];
[testship runAction:[CCFollow actionWithTarget:[[self getChildByTag:paraBGtag] getChildByTag:04]]];

}

1 个答案:

答案 0 :(得分:0)

您正在向后使用CCFollow。背景层在精灵上运行跟随操作,而不是相反。

所以:

[testship runAction:[CCFollow actionWithTarget:[[self getChildByTag:paraBGtag] getChildByTag:04]]];

应改为:

CCSprite *bg = (CCSprite*)[[self getChildByTag:paraBGtag] getChildByTag:04]];
[bg runAction:[CCFollow actionWithTarget:testship]];