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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:28:50+00:00 2026-05-26T15:28:50+00:00

Alright, here we go. I have a cocos2d app, and there are targets that

  • 0

Alright, here we go. I have a cocos2d app, and there are targets that move toward the player. When the player moves, I would like for them to slowly change their destination toward the player again, so they aren’t just moving into empty space. Is it possible to change the destination of a sprite mid-runAction?

edit:

This is the code in – (void)changeTargetDest

- (void)changeTargetDest {
    NSMutableArray* deleteArray = [[NSMutableArray alloc] init];
    for(CCSprite* s in _targets) {
        float offX = s.position.x - player.position.x;
        float offY = s.position.y - player.position.y;
        float adjustX;
        float adjustY;
        float offDistance = sqrt(powf(offX, 2.0f) + powf(offY, 2.0f));

        if(offDistance < 15) {
            [deleteArray addObject:s];

            deaths++;
            [deathLabel setString:[NSString stringWithFormat:@"Deaths: %ld", deaths]];
            if(deaths == 0)
            [kdLabel setString:[NSString stringWithFormat:@"K/D ratio: %ld.00", score]];
            else
                [kdLabel setString:[NSString stringWithFormat:@"K/D ratio: %.2f", ((float)score / (float)deaths)]];
        }

        else {
            adjustX = offX * .99;
            adjustY = offY * .99;

            CGPoint point = CGPointMake(player.position.x + adjustX, player.position.y + adjustY);

            [s setPosition:point];
        }//else

    }//for

    for (CCSprite *target in deleteArray) {

        [_targets removeObject:target];
        [self removeChild:target cleanup:YES];                                  
    }
}

This works well, except for one problem. Because the new position is calculated by just taking .99 of the previous offset, the closer the target gets to the player, the more slowly it moves. How can I make its speed constant?

  • 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-26T15:28:51+00:00Added an answer on May 26, 2026 at 3:28 pm

    You can stop the action and run a new action each few frames in a scheduled method.
    but the better way is to compute the position of targets according to players position and use setPosition to manualy change their positions each frame in your update method.

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

Sidebar

Related Questions

Alright here's the situation, I have an application written in the Zend_Framework, that is
Alright, so here's the dealio: I'm working on a Ruby app that'll take data
Alright, so here's the deal. I would like to be able to create a
Alright here is the thing, I have this site that was once wordpress but
Alright, I have a strange one here. We currently have a tools application that
Alright, possible a naive question here. I have a service that needs to log
Alright, here we go! I am currently developing an iPhone app that will work
Alright so here is the question. I have a user class which contains a
Alright, here goes. I'm making a very basic app, and I want the user
Alright, so I'm making a Facebook-style chat. But that doesn't really matter. See 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.