【问题标题】:Run an animation ONLY when click is performed (cocos2d iPhone)仅在执行单击时运行动画(cocos2d iPhone)
【发布时间】:2012-03-02 23:19:00
【问题描述】:

我希望仅在执行触摸时运行动画,然后将动画恢复到原始帧。

当我从 .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

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

但当我触摸屏幕时,动画会一直运行..

请帮帮我!

【问题讨论】:

  • 那么,你想在触摸精灵时开始动画,再次触摸时停止动画??

标签: iphone animation cocos2d-iphone touch


【解决方案1】:
[_bear stopAction:_moveAction];
[_bear stopAction:_walkAction];

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

试试这个

【讨论】:

    【解决方案2】:

    我做了其他方式:

    1. init中运行您的操作
    2. 如果您有更新方法,则创建标志以确定它是否正在移动 - 如果为真 -> 恢复操作;否则暂停

      如果 (player.isMarching) { [玩家简历SchedulerAndActions]; }别的 { [播放器 pauseSchedulerAndActions]; }

    它对我有用:)

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2022-01-05
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多