Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17078

Move Sprite along path at variable speeds

$
0
0

@Rockster160 wrote:

Hey all! This is my first post here, so pardon me if there are any basics I'm missing.

I've been scouring the internet for a solution to this one, but have yet to find anything.

What I am trying to do is move a sprite along a Bezier path at a variable speed. Ideally, I would be able to do something like this:

ccBezierConfig path; // With it's points
ccBezierConfig speed; // With it's points
BezierBy::create(duration, path, speed);

The effect I am trying to go for is showing a sprite on a screen, but when the player touches the sprite, it hops upward, then falls. (Think old school Mario, when Mario dies) Gravity is not implemented elsewhere in the game, and I would prefer a simplistic solution, if at all possible.

I've tried implementing a Physics method that just continues to add a negative velocity and then displaces the sprite, however this method has proved to be extremely choppy. The frame rate of the sprite becomes quite low, despite the rest of the game still running smoothly.

That attempt is shown here:

// Called on every `update` cycle of main game loop.
// Has an initial X/Y velocity, and this method to update the position
void Coin::tick(float dt) {
  velocityY += gravity * dt;
  float newX = coin->getPositionX() + velocityX;
  float newY = coin->getPositionY() + velocityY;
  coin->setPosition(newX, newY);
  // Using `MoveBy::create(dt, Vec2(newX, newY))` here does not change anything. Still very choppy.
}

Another attempt I made, which solved the choppiness (has good frame rate) was to use a BezierBy path. The path is good, but now the sprite moves at a constant speed, instead of starting fast, then slowing down to a stop, then continuously accelerating downwards.

Does anybody have a solution? Am I missing something here? I thought there would be a pretty easy built-in way to do this, but I guess not. smiley

Thanks in advance for reading and for any help!

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 17078

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>