simple question on a box2d ball motion

I am new to box2d and I have a question. Suppose I throw a ball up(swipe).

Something like
Code:
 
md.bodyA = _groundBody;
md.bodyB = _body;
md.target = locationWorld;
md.collideConnected = true;
md.maxForce = 90.0f * _body->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
_body->SetAwake(true);


Is there a way to determine when the ball is moving up and when it is moving down? Also, is there a way to update the z-value of a sprite when its in motion? I need this because I am trying to implement a basketball game.

I have set up the hoop back on z-value 1 and hoop front on z-value 3. The ball is on z-value 2.
Code:
CCSprite *hoopback = [CCSprite spriteWithFile:@"hoopback.png"];
      hoopback.position = ccp(170,240);
      [self addChild:hoopback z:1];

CCSprite *hoopfront = [CCSprite spriteWithFile:@"hoopfront.png"];
      hoopfront.position = ccp(170,240);
      [self addChild:hoopfront z:3]

_ball = [CCSprite spriteWithFile:@"basketball.png" rect:CGRectMake(0, 0, 45, 45)];
      _ball.position = ccp(120, 450);
      [self addChild:_ball z:2];


When I throw the ball up, I want it to be on z-value 3. On z-value 2 it looks as though the ball is going up from inside the hoop. Also, when the ball is coming down, i want it to be on z-value 2 to simulate the ball coming from inside the hoop. So I want to know whats an easy way to determine if the ball is moving up and coming down and is it possible to change the z-values of sprites at runtime.
I think, in your tick method, store the previous position of the ball and compare it to the current position.

Code:
b2Vec2 previousBodyPosition;


First initialize the ball's previous y position to the current y position (maybe when you create your joint)

Code:
previousBodyPosition.y = _body->GetPosition().y;



Code:
- (void)tick:(ccTime) dt {   
   _world->Step(dt, 10, 10);
   if (previousBodyPosition.y < _body->GetPosition().y){
      //The ball is going up
   } else if (previousBodyPosition.y > _body->GetPosition().y){
      //The ball is going down
   } else {
      //The ball is not moving
   }
   previousBodyPosition.y = _body->GetPosition().y;
}


I think to change the z level, you will need to delete the sprite and readd it at a new z level, but I'm not sure, I'm also new to this.

Hope that helps a bit.
You can look at the linear velocity without having to store/compare position. If the velocity is negative in the y direction, it is moving down.

The z-order question is a cocos question - you'd probably get better answers on that forum. Anyway, you can call the reorderChild method on the parent to change the z-order. No need to delete/recreate the sprite.

Original thread: https://box2d.org/forum/viewtopic.php?t=5877