将动画精灵面旋转到对象

时间:2012-11-04 13:35:54

标签: iphone ios cocos2d-iphone sprite

我有我的动画精灵:

   #import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Raider : CCSprite
{
    NSUInteger baseX;
    NSUInteger baseY;
    BOOL takeItem;
    CCAction *_walkAction;
    CCAction *_moveAction;
    CCAnimation *walkAnim;
}

@property (nonatomic) NSUInteger baseX;
@property (nonatomic) NSUInteger baseY;
@property (nonatomic) BOOL takeItem;
- (id) initRaider:(NSUInteger)x andY: (NSUInteger)y;
- (void) setTaken;
@property (nonatomic, retain) CCAction *walkAction;
@property (nonatomic, retain) CCAction *moveAction;
@property (nonatomic, retain) CCAnimation *animation;

-(void)stopAnim;
-(void)startAnim;

@end

和Raider.m

#import "Raider.h"

@implementation Raider
@synthesize baseX;
@synthesize baseY;
@synthesize takeItem;
@synthesize moveAction = _moveAction;
@synthesize walkAction = _walkAction;
@synthesize animation = walkAnim;

- (id) initRaider:(NSUInteger)x andY: (NSUInteger)y
{
    [self initWithFile:@"10001.png"];
    self.walkAction = nil;
    self.animation = nil;
    self.position = ccp(x,y);
    baseX = x;
    baseY = y;
    takeItem = NO;

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"pirate.plist"];
    NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 9; ++i)
    {
        [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
                                   [NSString stringWithFormat:@"1000%d.png", i]]];
    }
    self.animation = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
    self.walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
    [self runAction:_walkAction];
    return self;
}

- (void) setTaken
{
    [self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"raiderItem.png"]];
}

-(void)stopAnim
{
    [self pauseSchedulerAndActions];
}

-(void)startAnim
{
    [self resumeSchedulerAndActions];

}

@end

在我的场景中,我在中心有一个物体。我需要将所有物体面向中心旋转,然后我们将它们到达中心旋转它们返回到生成点。那么,我如何将我的动画精灵旋转到任何其他精灵的中心?  您可以在屏幕截图中看到它现在的样子: enter image description here

1 个答案:

答案 0 :(得分:0)

this so thread

上尝试Nikhil的回答

我试了一下它完美无缺。只需用函数或方法包装它。

相关问题