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

The Archive Base Latest Questions

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

My CCSprite subclass has two float properties called, velX and velY to keep track

  • 0

My CCSprite subclass has two float properties called, velX and velY to keep track of the velocity of my player. When it gets swiped in the game layer, I figure out the vector direction of the swipe and start the player moving that way. In the -update:(ccTime)dt method of my game layer, I call [player update:(ccTime)dt (I know, same name) and that method in the CCSprite class has this code.

// move
    self.position = ccp(self.position.x + self.velX * dt, self.position.y + self.velY * dt);
    // do edge checking

// decelerate
    #define kDeceleration .95
    NSLog(@">velX = %1.1f, velY = %1.1f, fabs(velX) = %1.1f, fabs(velY) = %1.1f", self.velX, self.velY, fabs(velX), fabs(velY));
    self.velX *= kDeceleration;
    if (fabs(self.velX < 1.0)) self.velX = 0.0;
    self.velY *= kDeceleration;
    if (fabs(self.velY < 1.0)) self.velY = 0.0;

This gets called every time the game layer calls update and passes in the same time increment that the game loop uses.

It works great if I swipe in a positive direction, but if either of the swipe components are negative (left or down) then they go straight to zero. If I take out the conditionals, everything works, I just figured that multiplying by zero should be cheaper than constantly multiplying by very small decimals.

I must be using the fabs() function incorrectly…

EDIT: Some sample output from two swipes, one positive in both axes, and one negative in both.

>velX = 21.8, velY = 20.6, fabs(velX) = 21.8, fabs(velY) = 20.6
>velX = 20.7, velY = 19.6, fabs(velX) = 20.7, fabs(velY) = 19.6
>velX = 19.7, velY = 18.6, fabs(velX) = 19.7, fabs(velY) = 18.6
>velX = 18.7, velY = 17.7, fabs(velX) = 18.7, fabs(velY) = 17.7
>velX = 17.8, velY = 16.8, fabs(velX) = 17.8, fabs(velY) = 16.8

>velX = -23.0, velY = -19.3, fabs(velX) = 23.0, fabs(velY) = 19.3
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
  • 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:50:08+00:00Added an answer on May 22, 2026 at 3:50 pm

    I believe this block is a syntax typo:

    self.velX *= kDeceleration;
    if (fabs(self.velX < 1.0)) self.velX = 0.0;
    self.velY *= kDeceleration;
    if (fabs(self.velY < 1.0)) self.velY = 0.0;
    

    It should be this instead:

    self.velX *= kDeceleration;
    if (fabs(self.velX) < 1.0) self.velX = 0.0;
    self.velY *= kDeceleration;
    if (fabs(self.velY) < 1.0) self.velY = 0.0;
    

    You need to close the fabs parentheses before the comparison operator

    p/s: it’s okay, everyone does this kind of mistake all the time 🙂

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

Sidebar

Related Questions

I have a strange problem while working with CCSprite subclass Creature. Lets,my object is
I have a hierarchy like this: Scene  - gameWorld (CCLayer Z:0)     - player (CCSprite
I I'm working on a game in Cocos2D and I have a CCSprite Subclass
I have a class called Projectiles which inherits from CCSprite class, Currently there are
I have two CCAnimations running on a CCSprite. How do I check if the
I have a marshmallow class which has (among other things) a CCSprite object as
I have cocos2d project with custom CCSprite subclass: MyCustomSprite.h: #import cocos2d.h @interface MyCustomSprite :
I have a class that inherits from CCSprite called GameObject . I also have
[I have 2 objects, both of them subclass of CCSprite. Each one of them
That's the question xD Given an instance of a CCSprite in cocos2d in iphone,

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.