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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:04:29+00:00 2026-06-12T06:04:29+00:00

I want my sprite to move to a precise pixel location. I use the

  • 0

I want my sprite to move to a precise pixel location. I use the following code in the update method to move my sprite (kPlayerSpeed = 60.0f):

if (ccpDistance(self.position, requestedPosition) < kPlayerSpeed) {
            [self changeState:kStateWalking];
            return;
        }

switch (self.characterState) {
        case kStateWalkingUp:
            self.position = ccp(self.position.x, self.position.y + kPlayerSpeed * deltaTime);
            break;
        case kStateWalkingDown:
            self.position = ccp(self.position.x, self.position.y - kPlayerSpeed * deltaTime);
            break;
        case kStateWalkingLeft:
            self.position = ccp(self.position.x - kPlayerSpeed * deltaTime, self.position.y);
            break;
        case kStateWalkingRight:
            self.position = ccp(self.position.x + kPlayerSpeed * deltaTime, self.position.y);
            break;
        case kStateIdle:
            break;
        default:
            break;

if (ccpDistance(self.position, requestedPosition) < kPlayerSpeed)

With this code the player stops near but not exactly on the spot.

  • 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-12T06:04:30+00:00Added an answer on June 12, 2026 at 6:04 am

    Andres, thanks for the reply.

    I couldn’t quite get your code to work, so using your lead I did a bit more research and came up with this…

        float step = kPlayerSpeed * deltaTime;
        float dist = ccpDistance(self.position, requestedPosition);
    
        if (dist <= step) {
            self.position = requestedPosition;
        } else {
            CGPoint vectorBetweenAB = ccpSub(self.position, requestedPosition);
            CGPoint normVectorBetweenPositionAndEnd = ccpNormalize(vectorBetweenAB);
            CGPoint movementVectorForThisFrame = ccpMult(normVectorBetweenPositionAndEnd, step);
            if (self.position.x > movementVectorForThisFrame.x) {
                movementVectorForThisFrame.x = -movementVectorForThisFrame.x;
            }
            if (self.position.y > movementVectorForThisFrame.y) {
                movementVectorForThisFrame.y = -movementVectorForThisFrame.y;
            }
            self.position = ccpAdd(self.position, movementVectorForThisFrame);
        }
    

    Is there a better way to do it than this?

    Thanks

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

Sidebar

Related Questions

What method should I use to smoothly move my sprite that responds to a
I have an sprite, i want to move it by using my finger to
I want to change the sprite image. Say for example: mainSprite=[Sprite spriteWithFile:@redFile.png]; [self addChild:mainSprite];
I am making an application, in that I want to move the sprite in
I want to move my Sprite in AndEngine in a projectile motion. Can any
I use some code to calculate the path of a sprite. The front of
I have an image that is a sprite, I want to move the scroll
I would like to use the accelerometer to move my player sprite. If the
When the user taps a button, I want to move a sprite to the
Here I want to move some cocos2D sprite object from top to bottom. Sprites

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.