1

我有一个包含飞龙姿势的精灵表,我已经成功地将它集成到我的项目中,它在我的屏幕上飞得很好。我编写了通过 x 和 y 轴随机移动龙的代码,但是当龙随机将位置改变为向后时,它并没有向后翻转,这意味着当龙向后移动时,它的脸仍然在后面。当它转动屏幕时,我需要翻转龙。

此代码是随机移动到 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;
}
4

1 回答 1

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...
}
于 2012-07-09T10:24:49.827 回答