我希望我的堕落精灵在屏幕中间消失

时间:2012-02-09 18:39:56

标签: iphone objective-c ios5 cocos2d-iphone

我刚接触编程,我一直在尝试cocos2d,继承人的问题,我已经做了一个简单的游戏,设备在纵向,它有下降的精灵,我希望精灵消失时,顶部的位置精灵< screen.height / 2怎么办呢?

下面是您可能感兴趣的一些代码:

这是落下的精灵,它从屏幕顶部落到底部

-(void)addRock {

CCSprite *rock = [CCSprite spriteWithFile:@"rock.png" 
                                     rect:CGRectMake(0, 0, 27, 40)]; 

// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = rock.contentSize.width/2;
int maxX = winSize.width - rock.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;

// Create the target slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
rock.position = ccp(actualX, 500);
[self addChild:rock];

// Determine speed of the target
int actualDuration = spriteDuration;//speed of sprite

// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(actualX,-winSize.height+ rock.contentSize.height)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
[rock runAction:[CCSequence actions:actionMove, nil]];

}

精灵移动完成后

-(void)spriteMoveFinished:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];

}

2 个答案:

答案 0 :(得分:0)

也许是这样的?

CGSize size = [[CCDirector sharedDirector] winSize];

CCSprite* img = [[CCSprite alloc] initWithFile:@"yourSprite.png"];
img.position = ccp(size.width/2,size.height + img.boundingBox.size.height/2);
[img runAction:[CCSequence actions:
                    [CCMoveTo actionWithDuration:3 position:ccp(img.position.x,size.height/2 - img.boundingBox.size.height/2)],
                    [CCFadeOut actionWithDuration:1],
                    [CCCallFuncN actionWithTarget:nil selector:@selector(spriteMoveFinished:)],
                    nil]];

答案 1 :(得分:0)

要将精灵淡出动画的一半,您可以使用CCSpawn将下降动画与 fadeOut 结合起来。但是,如果您希望它在给定时间后淡出,则需要创建CCDelayTimeCCFadeOut的序列。

double fadeStart = 0.5;
id wait    = [CCDelayTime actionWithDuration: actualDuration * fadeStart];
id fadeOut = [CCFadeOut actionWithDuration: actualDuration * (1.0 - fadeStart)];
id waitThenFade = [CCSequence actions: wait, fadeOut, nil];

id actionMove = ...;
id fullAnimation = [CCSpawn actions:actionMove, waitThenFade, nil];

[rock runAction:[CCSequence actions:fullAnimation, actionMoveDone, nil]];

如果您希望淡入淡出更早或更晚,您只需移动fadeStart值即可。 0会立即开始,1实际上不会褪色,所以最好在中间选择一个值。