2012-07-09 96 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