如何在Cocos2D中使用sprite跳转更新循环

时间:2012-09-05 06:34:15

标签: ios cocos2d-iphone

我有一个从顶部落下的球形精灵和一个位于它下方的男孩。当球击中男孩的头部时,我希望它遵循抛物线路径。我尝试使用CCJumpTo如下,但我不能让它工作。我正在调用更新循环中的操作。我不被允许这样做吗?我不能在更新循环中调用CCJumpTo吗?

- (void) jump
{
if(!method_called)
{ 
    method_called=TRUE; 
    CCActionInterval *jump1 = [CCJumpTo actionWithDuration:3 position:CGPointMake(400, 400) height:150 jumps:1];
    [_ball runAction:jump1];
    NSLog(@"something");
}
else 
{ 
    NSLog(@"do nothing"); 
}
}

- (void)update:(ccTime)dt
{
time ++;

if ( dpad.leftJoystick.velocity.x >  0 && x < 2000 ) {
    x = x + 10;
} else if ( dpad.leftJoystick.velocity.x < 0 && x > 0 ) {
    x = x - 10;
}
if (x > 10 && x < 2000)
    _boy.position = ccp(x, 100);


_ball.position = ccp(ball_x, ball_y);
ball_y = _ball.position.y - (speed * rebound);

_boy.anchorPoint = ccp(0, 0);    
CGRect boyBox = CGRectMake(_boy.position.x, _boy.position.y, [_boy boundingBox].size.width, [_boy boundingBox].size.height);
_ball.anchorPoint = ccp(0, 0);
CGRect ballBox = CGRectMake(_ball.position.x, _ball.position.y, [_ball boundingBox].size.width, [_ball boundingBox].size.height);



if (CGRectIntersectsRect(boyBox, ballBox) && flag == 0)
{
    rebound = -rebound;

    flag = 1;
    topFlag = 0;
    [_ball stopAllActions];
    [self jump];

}
if (_ball.position.y > 700 && topFlag == 0)
{
    rebound = -rebound;
    flag = 0;
    topFlag = 1;
}
}

提前致谢。

编辑:

这是我的init方法

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    self.ball = [CCSprite spriteWithFile:@"ball.png" 
                                           rect:CGRectMake(0, 0, 50, 50)];
    self.ball.position = ccp(275, 999);
    [self addChild:self.ball];  


    x = 0;
    ball_y = 650;
    ball_x = 300;
    rebound = 1;
    flag = 0;
    topFlag = 0;
    speed = 5;
    time = 0;
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     @"boy.plist"];
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"boy.png"];
    //spriteSheet.scaleX = 0.5;
    //spriteSheet.scaleY = 0.5;
    NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 8; ++i) {
        [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"boy%d.png", i]]];

        CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:.1f];
        self.boy = [CCSprite spriteWithSpriteFrameName:@"boy1.png"];    
        _boy.position = ccp(100000, 100000);
        self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim]];
        [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];
        [_boy runAction:_walkAction];
        _boy.flipX = YES;
        [spriteSheet addChild:_boy];

        dpad = [[MyJoystick alloc] init];
        [self addChild:dpad z:10];
        [self schedule:@selector(update:)];
    }

    [self addChild:spriteSheet];

}
return self;
}

1 个答案:

答案 0 :(得分:1)

将您的操作代码保存在方法中,并使用布尔变量从update方法调用它,以便仅调用一次。像这样:

在.h文件中取一个布尔变量method_called:

-(void)update:(ccTime)dt
{
    _boy.anchorPoint = ccp(0, 0); 
    CGRect boyBox = CGRectMake(_boy.position.x, _boy.position.y, [_boy boundingBox].size.width, [_boy boundingBox].size.height);
    _ball.anchorPoint = ccp(0, 0);
    CGRect ballBox = CGRectMake(_ball.position.x, _ball.position.y, [_ball boundingBox].size.width, [_ball boundingBox].size.height);

    if (CGRectIntersectsRect(boyBox, ballBox) && flag == 0) 
    { 
        flag = 1; topFlag = 0; 
        [self callJumpAction];
    } 
}

-(void)callJumpAction 
{
    if(!method_called)
    { 
        method_called=true; 
        CCActionInterval *jump1 = [CCJumpTo actionWithDuration:3 position:CGPointMake(400, 400) height:150 jumps:1];
        [_ball runAction:jump1];
    }
    else 
    { 
        NSLog(@"do nothing"); 
    }
}

希望这会有所帮助。