2012-12-11 31 views
1

我是新手,試圖整合一個CCsprite來跟隨一層parallaxnode。 它假設遵循bg04精靈。事實證明.....,是的,它遵循,但在相反的方向。CCFollow和ParallaxNode作品很奇怪

這是代碼,請大家幫忙,謝謝!

-(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]]]; 

} 

回答

0

您正在向後使用CCFollow。背景圖層對精靈運行後續操作,而不是相反。

所以:

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

應改爲:

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