Запускать анимацию ТОЛЬКО при нажатии (cocos2d iPhone)

Я хотел бы запускать анимацию только при выполнении прикосновения и впоследствии восстанавливать анимацию до исходного кадра.

Я хочу запускать эту анимацию, когда я щелкаю из .plist.

Мне приходилось запускать анимацию только при выполнении клика, а не при запуске приложения. Моя анимация имеет 25 кадров.

Это мой код:

    // HelloWorldLayer.h

    #import "cocos2d.h"

    // HelloWorldLayer
    @interface HelloWorldLayer : CCLayer
    {
    CCSprite *_bear;
    CCAction *_walkAction;
    CCAction *_moveAction;
    BOOL _moving;

    }

    +(CCScene *) scene;

    @property (nonatomic, retain) CCSprite *bear;
    @property (nonatomic, retain) CCAction *walkAction;
    @property (nonatomic, retain) CCAction *moveAction;

    @end

    ///////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////

    // HelloWorldLayer.m

    #import "HelloWorldLayer.h"

    // HelloWorldLayer implementation
    @implementation HelloWorldLayer

    @synthesize bear = _bear;
    @synthesize moveAction = _moveAction;
    @synthesize walkAction = _walkAction;

    +(CCScene *) scene
    {
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
    }

    // on "init" you need to initialize your instance
    -(id) init
    {
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"llamanim24fps.plist"];

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

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

    // Create a sprite for our bear
    //CGSize winSize = [CCDirector sharedDirector].winSize;
    self.bear = [CCSprite spriteWithSpriteFrameName:@"llamasinfondo0000.png"];
    _bear.position =ccp(391, 300);
    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];

    [_bear stopAction:_moveAction];

    if (!_moving) {
    [_bear runAction:_walkAction];
    }
    // [_bear runAction:_walkAction];
    self.moveAction = [CCSequence actions:

    [CCCallFunc actionWithTarget:self selector:@selector(bearMoveEnded)],
    nil];

    _moving = TRUE;

    }

    -(void)bearMoveEnded {
    [_bear stopAction:_walkAction];
    _moving = FALSE;
    }
    // on "dealloc" you need to release all your retained objects
    - (void) dealloc
    {
    // don't forget to call "super dealloc"
    [super dealloc];
    }
    @end

    //////////////////////////

Но анимация работает вечно, когда я касаюсь экрана..

пожалуйста, помогите мне с этим!


person galenzo17    schedule 02.03.2012    source источник
comment
Итак, вы хотели бы запускать анимацию при касании спрайта и прекращать анимацию при повторном касании??   -  person Mazyod    schedule 03.03.2012


Ответы (2)


[_bear stopAction:_moveAction];
[_bear stopAction:_walkAction];

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

Попробуйте это на

person Ayaz    schedule 06.03.2012

Я поступил иначе:

  1. запустите действие в init
  2. если у вас есть метод обновления, то создайте флаг, чтобы определить, движется ли он - если true -> возобновить действие; иначе пауза

    if (player.isMarching) { [player резюмеSchedulerAndActions]; }else { [player pauseSchedulerAndActions]; }

у меня работает :)

person raistlin    schedule 07.07.2013