2012-07-09 118 views
1

我有一個精靈表,其中包含龍飛行的姿勢,我已經成功地將它集成到我的項目中,並且它在我的屏幕上飛得很好。我編寫了通過x軸和y軸隨機移動龍的代碼,但是當龍隨機將位置改變爲向後時,它不會向後翻轉,這意味着當龍向後移動時,它的表面仍然位於後部。當它轉動屏幕時,我需要翻轉龍。cocos2d中的Sprite Sheet動畫iOS

此代碼是隨機移動到x和y軸的代碼。

-(void)moveRandom:(CCSprite*)s 
{ 
    CGPoint randomPoint = ccp(arc4random()%2000, arc4random()%500); 
    NSLog(@"%@", NSStringFromCGPoint(randomPoint)); 

    [s runAction: 
    [CCSequence actions: 
     [CCMoveTo actionWithDuration:arc4random()%5+1 position: randomPoint], 
     [CCCallBlock actionWithBlock:^{ 
     [self performSelector:@selector(moveRandom:) withObject:s afterDelay:0.5]; 
    }], 
     nil] 
    ]; 
} 

以下代碼是在init方法

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"dr.plist"]; 
    CCSpriteBatchNode *parrotSheet = [CCSpriteBatchNode batchNodeWithFile:@"dr.png"]; 
    [self addChild:parrotSheet]; 
    NSMutableArray *flyAnimFrames = [NSMutableArray array]; 

    for(int i = 1; i <= 6; ++i) { 

     [flyAnimFrames addObject: 

     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 

      [NSString stringWithFormat:@"dragon%d.png", i]]]; 

    } 

[self moveRandom:theParrot]; 

    CCAction *flyAction = [CCRepeatForever actionWithAction: 

          [CCAnimate actionWithAnimation:flyAnim restoreOriginalFrame:NO]]; 

    //start the action 

    [theParrot runAction:flyAction]; 

    //add the sprite to the CCSpriteBatchNode object 

    [parrotSheet addChild:theParrot]; 

如何解決此問題。? ...

我有另一個示例應用程序,它會翻轉圖像加入屏幕中的觸摸。當我們觸摸屏幕左側時,圖像會左轉並移動該方向。 我需要這個,但沒有聯繫,自動想要轉動圖像。

這個代碼是

-(id) init { 
    if((self = [super init])) { 

     // This loads an image of the same name (but ending in png), and goes through the 
     // plist to add definitions of each frame to the cache. 
     [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimBear.plist"];   

     // Create a sprite sheet with the Happy Bear images 
     CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimBear.png"]; 
     [self addChild:spriteSheet]; 

     // Load up the frames of our animation 
     NSMutableArray *walkAnimFrames = [NSMutableArray array]; 
     for(int i = 1; i <= 8; ++i) { 
      [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"bear%d.png", i]]]; 
     } 
     CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f]; 

     // Create a sprite for our bear 
     CGSize winSize = [CCDirector sharedDirector].winSize; 
     self.bear = [CCSprite spriteWithSpriteFrameName:@"bear1.png"];   
     _bear.position = ccp(winSize.width/2, winSize.height/2); 
     self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]; 
     //[_bear runAction:_walkAction]; 
     [spriteSheet addChild:_bear]; 

     self.isTouchEnabled = YES; 

    } 
    return self; 
} 

-(void) registerWithTouchDispatcher 
{ 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; 
} 

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    return YES; 
} 

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {  
    CGPoint touchLocation = [touch locationInView: [touch view]];  
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; 
    touchLocation = [self convertToNodeSpace:touchLocation]; 

    float bearVelocity = 480.0/3.0; 
    CGPoint moveDifference = ccpSub(touchLocation, _bear.position); 
    float distanceToMove = ccpLength(moveDifference); 
    float moveDuration = distanceToMove/bearVelocity; 

    if (moveDifference.x < 0) { 
     _bear.flipX = NO; 
    } else { 
     _bear.flipX = YES; 
    }  

    [_bear stopAction:_moveAction]; 

    if (!_moving) { 
     [_bear runAction:_walkAction]; 
    } 

    self.moveAction = [CCSequence actions:       
        [CCMoveTo actionWithDuration:moveDuration position:touchLocation], 
        [CCCallFunc actionWithTarget:self selector:@selector(bearMoveEnded)], 
        nil 
        ]; 


    [_bear runAction:_moveAction]; 
    _moving = TRUE; 
} 

-(void)bearMoveEnded { 
    [_bear stopAction:_walkAction]; 
    _moving = FALSE; 
} 

回答

1

這是完全一樣的想法,你表現出的示例代碼。

每當你爲你的龍生成一個新的隨機位置時,你需要檢查它是否在你當前的龍位置的左邊或右邊,然後相應地翻轉龍精靈以面對那個方向,通過設置flipX屬性:答案

-(void)moveRandom:(CCSprite*)s 
{ 
    CGPoint randomPoint = ccp(arc4random()%2000, arc4random()%500); 
    NSLog(@"%@", NSStringFromCGPoint(randomPoint)); 
    CGPoint moveDifference = ccpSub(randomPoint, s.position); 
    if (moveDifference.x < 0) 
    {    
     s.flipX = NO;   
    } 
    else 
    {   
     s.flipX = YES;   
    } 

    // the rest of your code... 
} 
+0

,,喔,謝謝,它的工作原理fine..thanks非常 – stackiphone 2012-07-09 11:15:11