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

  • Home
  • SEARCH
  • 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 6340297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:48:33+00:00 2026-05-24T19:48:33+00:00

I am developing my first game with cocos2d and i have run into a

  • 0

I am developing my first game with cocos2d and i have run into a problem of which i cant seem to find a right solution. i am adding and animating a CCSprite every second from top of the screen to the bottom and need to hide these sprites when the player touches on any one of them. So i thought of giving tags to every sprite i add and later on access that sprite with that particular tag. Do i need to put the tag number in some array as they get incremented every second even before i access them in the touch method??

- (void)addStraightBugs 
{
currentAntTag++;

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

spriteSheetmedAnt = [CCSpriteBatchNode batchNodeWithFile:@"smallAunt.png"];
[self addChild:spriteSheetmedAnt z:0 tag:kSpriteManager];

CCSprite *ant= [CCSprite spriteWithSpriteFrameName:@"small-aunt1.png"];
[spriteSheetmedAnt addChild:ant z:1 tag:currentAntTag];

NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 2; ++i) {
    [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"small-ant%d.png", i]]];
}

CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.15f];
CCAction *action=[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];


ant.position = ccp(100,500);
[ant runAction:action];

CGPoint realDest = ccp(60,140);

int minDuration = 2.0;
int maxDuration = 5.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

[ant runAction:[CCSequence actions:
                       [CCMoveTo actionWithDuration:actualDuration position:realDest],
                       [CCCallFuncN actionWithTarget:self selector:@selector(moveFinished:)],
                       nil]];

}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event  
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

CCSpriteBatchNode *spriteManager;
spriteManager = (CCSpriteBatchNode*)[self getChildByTag:kSpriteManager];
CCSprite *ant = (CCSprite*)[spriteManager getChildByTag:currentAntTag];

CGRect abc= CGRectInset([ant boundingBox],30, 85);

if(CGRectContainsPoint(abc,touchLocation))  
{

    ant.visible=NO;
}
}

also i have 3 methods which are called every few seconds in which i create these CCSpriteFrameCache and CCSpriteBatchNode object to make my character run while it animates. will it be too heavey to crate caches every second like this or should i create them in init method and just run the action on CCSprite here??

  • 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-24T19:48:33+00:00Added an answer on May 24, 2026 at 7:48 pm

    Subclass CCSprite. Then in it’s init:

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
    

    Implement the ccTouch methods:

    - (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
    - (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
    - (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
    - (void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
    

    Now you have two options. You can have a delegate variable to callback or use NSNotifications. Go with the callback here, it’s faster.

    // in your @interface
    id touchDelegate; // between the {}'s
    
    @property (nonatomic, assign) id touchDelegate;
    

    Inside your game class, when you’re creating your bad guys:

    NewCCSprite = newSprite = [NewCCSprite init];
    newSprite.touchDelegate = self;
    

    And when you touch one:

    - (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
        if (self.touchDelegate == nil) return;
        [self.touchDelegate performSelector:@selector(touched:) withDelay:0.0f];
    }
    

    Finally, your touch: method:

    - (void) touch:(id)sender {
        NewCCSprite* sprite = (NewCCSprite*)sender;
        // hide/kill off/etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently developing a PHP framework and have ran into my first problem. I need
I am developing an iOS game and I have two questions. First, I am
I am new to cocos2d and im developing my first iphone game. The requirement
I've recently started developing my first serious application which uses a SQL database, and
I'm currently developing my first android app, and my first game. I've been developing
I'm developing a game for android and this is my first experience with OpenGL.
I am developing a game, the game have different mode. Easy, Normal, and Difficult.
I have been developing a new online game for the past year now. The
I'm developing my first Mac App have some issues with shell commands... I'm trying
We are developing our first iPhone game, and all is going OK for now.

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.