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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:42:07+00:00 2026-05-21T08:42:07+00:00

I’m adding an enemy like this: -(void)addEnemy { if ([spawnedEnemies count] < 25) {

  • 0

I’m adding an enemy like this:

-(void)addEnemy {
    if ([spawnedEnemies count] < 25) {
        CCSprite* sprite = [CCSprite spriteWithFile:@"white.png"];
        float randomX = arc4random() % 480;
        float randomY = arc4random() % 320;

        sprite.position = ccp(randomX,randomY);
        [spawnedEnemies addObject:sprite];
        [self addChild:sprite];  

        [sprite runAction:[CCMoveTo actionWithDuration:5 position:player.position]];
    } }

but if my player moves the sprite still moves to the last player position…
because of that i tried this in my tick:

-(void)tick:(ccTime)delta {

for (CCSprite *sp in spawnedEnemies) {
    [sp stopAllActions];
    [sp runAction:[CCMoveTo actionWithDuration:5

position:player.position]];
} }

but the enemies will only move if i stop moving (there just moving reallllllllyyyyyyyyy slow because every time i move it calls [sp stopAllActions]

What should I do now?

EDIT:*EDIT:*EDIT:*EDIT:*EDIT:*EDIT:*EDIT:*EDIT:*EDIT:
Now I’m doing this and the enemies are moving to the player even if the player is moving
but there’s a problem: the nearer the enemy (is to the player) the slower they are moving…
How to solve this?

//function to apply a velocity to a position with delta
static CGPoint applyVelocity(CGPoint velocity, CGPoint position, float delta){
    return CGPointMake(position.x + velocity.x * delta, position.y + velocity.y * delta);
}


-(void)tick:(ccTime)delta {    
    for (CCSprite *sp in spawnedEnemies) {
        CGPoint m = ccpSub(player.position, sp.position);
        sp.position = applyVelocity(m, sp.position, 1.0/60.0f);
    }
}
  • 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-21T08:42:08+00:00Added an answer on May 21, 2026 at 8:42 am

    I have four ideas:

    1. Schedule a new update selector with a lower frequency and move the enemies in that method: [self schedule:@selector(moveEnemies) interval:0.1];

    2. Only move the enemies when the player.position changes, maybe in your ccTouchesMoved-method.

    3. Instead of using CCActions, set the position of the sprite directly, you need to do some vector-computations.

    4. Use a physics-engine like Box2D (included in Cocos2D SDK). Then you can simply apply a force to each enemy to the direction of the player in each step.

    EDIT:
    In order to move the enemies with constant velocity, normalize the velocity vector m:

    m = ccpMult(ccpNormalize(m), kSpeed);
    

    kSpeed is a constant float value to adjust the speed.

    Hope that helps…

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.