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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:25:17+00:00 2026-05-21T11:25:17+00:00

I don’t know how the best way is to explain what I am trying

  • 0

I don’t know how the best way is to explain what I am trying to do, but I will give it a shot.
So I have a football on the screen and when I touch the screen I can drag my finger across the screen which by using a CCProgressTimer gives me a representation of the power of the throw. In other words it is like having a volume bar on the screen and when I touch the screen and drag away from the volume bar it starts to increase and dragging towards the volume bar decreases it. So my question is I am recording my touch when I touch the screen and that touch is what all the calculations are based off of, but I want to some how be able to no matter where I am on the screen be able to decrease or increase. The way it works right now is, I can’t start to decrease the power of the throw until I reach that recorded touch. Is it possible to have it work, where when the power is at %100 no matter where I am on the screen if I drag back towards the football to have it decrease before I reach that original touch on the screen. Here is my code.

    footballPath = [CCProgressTimer progressWithFile:@"footballPath.png"];
            footballPath.visible = NO;
            footballPath.percentage = 0;
            footballPath.type = kCCProgressTimerTypeHorizontalBarLR;
            footballPath.anchorPoint = ccp(0, 0.5);
            [self addChild:footballPath];

-(BOOL)ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event {

    cachedTouchPt = [self convertTouchToNodeSpace:touch];

    if (!self.currentFootball.footballHasEmbarked) {
        footballPath.percentage = 0;
        [self updateArrowWithTouch:touch];
        arrow.visible = YES;
        footballPath.visible = YES;

        return YES; 
    }
    else {
        return NO;
    }

}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {

    [self updateArrowWithTouch:touch];

}

-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event {

    const float max = 100;
    CGPoint touchPt = [self convertTouchToNodeSpace:touch];
    if (touchPt.x > self.currentFootball.position.x) {
        touchPt = ccp(self.currentFootball.position.x, touchPt.y);
    }

    arrow.visible = NO;
    footballPath.visible = NO;
    self.currentFootball.footballHasEmbarked = YES;
    self.currentFootball.spiraling = YES;

    if (self.currentFootball) {
        [smgr morphShapeToActive:self.currentFootball.shape mass:25];
    }

    diff = ccpSub(touchPt, self.currentFootball.position);

    if (diff.x == 0 && diff.y == 0) {
        diff = ccp(1, 1);
    }
    float len = footballPath.percentage;
    CGPoint norm = ccpNormalize(diff);

    if (len > max){
        len = max;
    }

    [self.currentFootball applyImpulse:ccpMult(norm, (len * 245))];

    pos = self.currentFootball.position.y;

    [self schedule:@selector(newFootball)]; 

}

- (void) updateArrowWithTouch: (UITouch*) touch {

    const float distFromFb = 100;
    CGPoint touchPt = [self convertTouchToNodeSpace:touch];
    if (touchPt.x > self.currentFootball.position.x) {
        touchPt = ccp(self.currentFootball.position.x, touchPt.y);
    }

    CGPoint fpt = self.currentFootball.position;

    CGPoint vect = ccpSub(touchPt, fpt);
    float dist = ccpLength(vect);
    CGPoint vect2 = ccpSub(touchPt, cachedTouchPt);
    float dist2 = ccpLength(vect2);
    float degrees = -CC_RADIANS_TO_DEGREES(ccpToAngle(vect));
    float factor = dist2;
    CGPoint normalVect = ccpMult(vect, 1/dist);

    factor = distFromFb;

    CGPoint newPoint = ccpAdd(fpt, ccpMult(normalVect, factor));
    if (newPoint.x < self.currentFootball.position.x+1) {
        arrow.rotation = degrees; 
        arrow.position = newPoint;

        self.currentFootball.rotation = -CC_RADIANS_TO_DEGREES (ccpToAngle(ccpSub(touchPt, self.currentFootball.position)));
        footballPath.rotation = self.currentFootball.rotation;
        footballPath.position = fpt;
    }   

    float percentage = dist - ccpLength(ccpSub(cachedTouchPt, self.currentFootball.position));

    if (percentage < 0.0f){

        percentage = 0.0f;
    }

//  CCLOG(@"cachedDist = %f", cachedDist);
    CCLOG(@"percentage = %f", percentage);

        diff = vect2;
        footballPath.percentage = percentage;

}

The cachedTouchPt = [self convertTouchToNodeSpace:touch]; is the original point that I am talking about. So when I touch the screen it makes a new point and stores it in cachedTouchPt, and so I can’t decrease the the power/CCProgressTimer until I reach the X and Y of the cachedTouchPt. If I didn’t make sense of what I was trying to say. I need to be able to decrease the power/CCProgressTimer without needing to be at the original point. Is there a way reset the point so that no matter where I am on the screen I can drag towards the football and have it decrease, same with the increasing.

  • 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-21T11:25:18+00:00Added an answer on May 21, 2026 at 11:25 am

    I’ll answer your question by showing you the math but you need to convert it to codes yourself.

    First, you need to decide (you probably did) how long (in points) does a touch need to move to increase the power of the throw from 0 to 1. Let’s refer to this value as D.

    Then, let’s refer to the coordinate where the touch began as Tx and Ty.

    Then, when the touch moved to new coordinate Ux and Uy, you the distance the touch has moved from (Tx, Ty) using formula:

    E = sqrt( pow( Ux - Tx, 2 ) + pow( Uy - Ty, 2 ) )
    

    And you calculate the power using formula:

    P = E / D
    

    Up to this point I think your code is already doing all these calculations. But what’s coming next is how you would handle if player still moves the touch exceeding the distance D from the touch began point, that is to say that E > D.

    First, put an IF block:

    if (E > D) { ... }
    

    So, now you want to modify Tx and Ty values (i.e. the touch began point) so that the distance from the current touch position to that coordinate is D. Here’s the magic formula:

    angle = atan2( Uy - Ty, Ux - Tx )
    Ty = Uy - D * sin( angle )
    Tx = Ux - D * cos( angle )
    

    At this point you might want to modify the power value to 1:

    P = 1
    E = D
    

    And that’s it! The formula will move the touch began point towards the current touch position to maintain the condition of E <= D. Naturally the case where the player moves the touch towards the touch began point is taken care of.

    Good luck!

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

Sidebar

Related Questions

Don't know how to explain it better but i'm trying to get a response
don't know if the title describes anything about what I'm trying to say but
Don't know how to google for such, but is there a way to query
Don't know if this is the right place to ask this, but I will
Don't know if I'm over-thinking this or not.. but I'm trying to be able
Don't know why but I can't find a solution to this. I have 3
don't know better title for this, but here's my code. I have class user
Don't know a better title but here is what im trying to do. I
Don't ask me how but I'm in a situation where I have DCPs published
Don't really know how to formulate the title, but it should be pretty obvious

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.