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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:13:00+00:00 2026-05-22T15:13:00+00:00

I am making a game in Cocos2d. I want enemies to spawn on the

  • 0

I am making a game in Cocos2d. I want enemies to spawn on the right and left sides of the screen, and move to a random point on the screen, then repeat. Despite my efforts, I cannot figure it out. This should be relatively easy to answer, it should look like something out of one of Ray Wenderlich’s tutorials. Some code would be nice. 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-05-22T15:13:01+00:00Added an answer on May 22, 2026 at 3:13 pm

    This is the code from ray Wanderlich tutorial..

    [self schedule:@selector(addTarget) interval:2.0];
    
    -(void)addTarget {
    
      CCSprite *target = [CCSprite spriteWithFile:@"Target.jpg"  
        rect:CGRectMake(0, 0, 27, 40)]; //Creating Sprite and setting rect
    
      // Determine where to spawn the target along the Y axis
      CGSize winSize = [[CCDirector sharedDirector] winSize]; //Get the screensize
      int minY = target.contentSize.height/2;
      int maxY = winSize.height - target.contentSize.height/2;
      int rangeY = maxY - minY;
      int actualY = (arc4random() % rangeY) + minY;
    
      // Create the target slightly off-screen along the right edge,
      // and along a random position along the Y axis as calculated above
      target.position = ccp(winSize.width + (target.contentSize.width/2), actualY);
      [self addChild:target];
    
      // Determine speed of the target
      int minDuration = 2.0;
      int maxDuration = 4.0;
      int rangeDuration = maxDuration - minDuration;
      int actualDuration = (arc4random() % rangeDuration) + minDuration;
    
      // Create the actions
      id actionMove = [CCMoveTo actionWithDuration:actualDuration 
        position:ccp(-target.contentSize.width/2, actualY)];
      id actionMoveDone = [CCCallFuncN actionWithTarget:self 
        selector:@selector(spriteMoveFinished:)];
      [target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
    
    }
    

    minY–>position at the bottom of the screen
    maxY—>position at the top of the screen.
    rangeY —> the height of the screen.
    actualY—>calculate the random point between bottom of the screen and top of the screen.

    target.position –> setting random position for the sprite to move.

    actualDuration –> getting random duration.so that the sprite moves in various time delay.

    actionMove –> creating Move action.
    actionMoveDone —> After completing move action,calling spriteMoveFinished to remove sprites.

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

Sidebar

Related Questions

I am making a little game using cocos2d. Now i want to remove value
I'm making a game using cocos2d-iphone, I want's to rotate a Texture2D of maybe
Hi I am making a cocos2d game, In that i am adding enemies dynamically
I am making a tile game. Was following http://www.raywenderlich.com/1163/how-to-make-a-tile-based-game-with-cocos2d . Using the whole screen
my problem is: I'm making a game for iOS using cocos2d and this game
I am making a game in java and I want to create a simulation
I'm making an iPhone game in cocos2d. I was wondering how I would make
I'm making a game on iPhone using cocos2d, and I have a question. In
I'm making a game for the iPhone using Cocos2D. At the beginning of the
I am making a 2D game in Android with Cocos2D, written in Java. Here

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.