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 8016129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:27:29+00:00 2026-06-04T20:27:29+00:00

I have just started learning Cocos2d from one tutorial. I want to fly ‘Bats’

  • 0

I have just started learning Cocos2d from one tutorial. I want to fly ‘Bats’ randomly and when i touch anywhere on screen all Bats gathered at my touch position and them again start fly randomly.
Here is my code :

-(id) init
{
if( (self=[super init])) {

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

    //Bats Array Initialization
    bats = [[NSMutableArray alloc] init];

    //Add bats using a batch node.
    CCSpriteBatchNode *batch1 = [CCSpriteBatchNode batchNodeWithFile:@"Bat.png" capacity:10];
    [self addChild:batch1 z:2 tag:TAG_BATS];

    //Make them start flying up.
    for(int x=0; x<5; x++){
        //Create SimpleAnimObject of bat
        bat1 = [SimpleAnimObj spriteWithBatchNode:batch1 rect:CGRectMake(0,0,48,48)];
        [batch1 addChild:bat1];
        [bat1 setPosition:ccp(arc4random()%900+40, arc4random()%600+50)];
        [bat1 setScale:0.6f];

        float flappingSpeed = [self makeBatFlyUp:bat1];
        bat1.velocity = ccp((arc4random()%1000)/500 + 0.2f, 0.1f/flappingSpeed);

        [bats addObject:[NSValue valueWithPointer:bat1]];
        [bat1 retain];

        //Set the bat's direction based on x velocity.
        if(bat1.velocity.x > 0){
            bat1.flipX = YES;
        }
    }

    self.isTouchEnabled = YES;
    [self schedule:@selector(step:)];


}
return self;
}
-(float)makeBatFlyUp:(SimpleAnimObj*)bat {
CCSpriteFrameCache * cache = [CCSpriteFrameCache sharedSpriteFrameCache];

float delay = (float)(arc4random()%5+5)/150;
CCAnimation *animation = [[CCAnimation alloc] initWithName:@"simply_bat_fly" delay:delay];

int num = arc4random()%9+1;
for(int i=1; i<=9; i+=1){
    [animation addFrame:[cache spriteFrameByName:[NSString stringWithFormat:@"b%i.tiff",num]]];
    num++;
    if(num > 9){ num = 1; }
}       

[bat stopAllActions];
[bat runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation]]];

bat.animationType = BAT_FLYING_UP;

return delay;
}  

In touch event :

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];

float velocityBat =  1024/3.0 ;

CGPoint moveDifference = ccpSub(touchLocation,bat1.position );
float distanceMove = ccpLength(moveDifference);
float moveduration = distanceMove /   velocityBat;

// here i have to use Array but i don't know how to use array to access all same sprites.

self.moveAction = [CCSequence actions:                          
                   [CCMoveTo actionWithDuration:moveduration position:touchLocation],
                   [CCCallFunc actionWithTarget:self selector:@selector(bearMoveEnded)],
                   nil
                   ];
[bat1 runAction:moveAction];

return YES;
}  

Bats are flying properly but when i touch on screen only one Bat follow my touch event others are keep moving randomly.
Can any one help me here? If i miss any thing or where i am wrong ?

Thanks!!

  • 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-06-04T20:27:31+00:00Added an answer on June 4, 2026 at 8:27 pm

    You have only one reference to a bat that you’re using: the bat1 variable. You need to use the bats array to access all of the bats.

    Also, get rid of the [bat1 retain] line because by adding each bat to the bats array it is already retained. Cocos nodes are also retained when you make them a child of a parent by calling addChild:node.

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

Sidebar

Related Questions

I have just started learning ruby reading from different resources. One of them is
I have just started learning pthreads API and I am following the tutorial here
I have just started learning Javascript. I want Hello World! to be written to
I have just started learning asp.net mvc and one reason of the primary reasons
I have just started learning django. I created a form from django models. On
G'Day Programmers, I am from Java background however I have just started learning C++
I have just started learning python and am getting caught up. I come from
I have just started learning MySQL and am having trouble extracting matching flights from
Hi I have just started learning about xsd as I want to use it
I have just started learning MVVM. I've made the application from scratch by following

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.