Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:15:36+00:00 2026-05-31T04:15:36+00:00

I would like the run an animation only when a touch is performed, and

  • 0

I would like the run an animation only when a touch is performed, and restore the animation to the original frame afterwards.

I want to run this animation when I do click from a .plist.

I had to run the animation only when you perform a click, and not when the app starts. My animation has 25 frames.

This is my code:

    // 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

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

But the animation runs for ever and ever when I touch the screen..

please help me with this!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:15:38+00:00Added an answer on May 31, 2026 at 4:15 am
    [_bear stopAction:_moveAction];
    [_bear stopAction:_walkAction];
    
    if (_moving == TRUE) {
    [_bear runAction:_walkAction];
    }
    

    Try This on

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a process from Python (2.4/2.5/2.6) using Popen , and
I would like to run something like: select * from table where field in
I would like to run a Dos program from a web server. The Dos
I would like to run multiple copies of my application and force the first
I would like to run a job through cron that will be executed every
I would like to run a task if any file in an item list
I would like to run the following SQL select: SELECT ID, NUMERATOR, (SELECT m.COLUMNNAME
I would like to run APIDemos under platforms\android-1.5\samples directory on Android emulator but I
I would like to run a (extensive) query that produces one line of XML.
I would like to run a timer for every 5 hours and delete the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.