2012-09-05 50 views
2

我有一個球精靈,從頂部和一個男孩在它之下下降。當球擊中男孩的頭部時,我希望它遵循拋物線路徑。我嘗試使用CCJumpTo如下,但我無法得到它的工作。我在更新循環中調用動作。我不允許這樣做嗎?我不能在更新循環中調用CCJumpTo嗎?如何使更新循環內一個精靈跳躍在cocos2d

- (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; 
} 
+0

您可以撥打CCJumpTo更新循環中。我嘗試了相同的代碼。它似乎在工作。可能是其他一些問題。你確定條件檢查中的代碼行被執行了嗎? – Rakesh

+0

是的,我確實檢查過。正在執行中。直到現在,我通過增加_ball.position.y來反彈。這工作正常。但我想要帶來一條拋物線路徑。這就是爲什麼我嘗試上述。但似乎沒有發生。 – Anil

+0

你能後你是如何使球移動,添加精靈的代碼。我嘗試了一個簡單的moveTo,並且更新了你發佈的代碼並且工作正常。所以它必須在其他地方。 – Rakesh

回答

1

保持在方法內部的操作代碼,並使用一個布爾變量,因此,這將是一次只叫從更新的方法調用它。事情是這樣的:

採取.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"); 
    } 
} 

希望這會有所幫助。

+0

嘗試使用該方法再次實現。依然沒有。 當我使用 [self schedule:@selector(update :) interval:0.1]致電更新時; 我可以看到拋球出現的速度比球上升的更快 – Anil

+0

我終於在沒有更新方法的幫助下運行了。這是導致問題的原因。 – Anil