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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:51:42+00:00 2026-06-04T16:51:42+00:00

As a very beginner in Cocos2D i´m trying to make an iPhone game where

  • 0

As a very beginner in Cocos2D i´m trying to make an iPhone game where some cows move randomly around the screen. I used the code for moving the sprites from here: highoncoding.com/…/. I´m adding the sprites in the init method wia an addAnimal method:

-(void) addAnimal {
animal = [CCSprite spriteWithFile:@"cow.png"];
animal.position = [self generateRandomCoordinates];

[self addChild:animal];
[self startAnimation:animal];
}

My problem:
When i add more than one cow to my game, they move from that random spawn position to another random position and then the first cow stops and the other cow goes on correctly. The startAnimation command in the finishedMoving method goes always to the last sprite. That means i need better control over my sprites but how to da that right?

  • 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-04T16:51:43+00:00Added an answer on June 4, 2026 at 4:51 pm

    You can try to implement animal class, that will contain your sprite and incapsulate random movement. Smth like

    @implementation Cow
    
    - (id) init
    {
        self = [super init];
    
        if( self != nil )
        {
            CCSprite* cowSprite = [CCSprite spriteWithFile:@"cow.png"];
            [self addChild: cowSprite];
        }
    
        return self;
    }
    
    - (void) onEnter
    {
        [super onEnter];
        [self makeRandomMovement];
    }
    
    - (void) makeRandomMovement
    {
        id randomMoveAction = // create your random move action here
        id moveEndCallback = [CCCallFunc actionWithTarget: self selector: @selector(makeRandomMovement)];
        id sequence = [CCSequence actionOne: randomMoveAction two: moveEndCallback];
        [self runAction: sequence];
    }
    
    @end
    

    In such way after ending random movement part, method makeRandomMovement will be called again to generate and start new random movement part.

    then remake your addAnimal method to smth like

    - (void) addAnimal
    {
        Cow* newCow = [Cow node];
        [newCow setPosition: [self generateRandomPosition]];
        [self addChild: newCow];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a very beginner to Java, recently trying to use some existing libraries
So I'm very beginner with javascript and would love some help simplifying this code.
I'm very new to Objective-C, and am having some beginner issues. I have an
I'm very beginner in Flash. I'm trying to use document class only. I created
Very beginner level question. Trying to learn JSON and having trouble with getting proper
This is a very beginner question, but driving me crazy. Why isn't this code
I'm trying to make canvas draw code for the purposes of electronics education. Please
This is a very beginner problem. I'm trying to display an image taken with
Ok guys, i'm very beginner and trying to enter string to a char array
probably a very beginner's question, but I'm really interested in how to make it

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.