0

Base on my first question here which was answered by Theis Egeberg (The solution was explained more by Theis in the comment and worked like magic)I would need to also know the following:

(This might have a simple answer but I will leave it it to Theis or anyone who can figure it out.)

What if I have another object bouncing off the the already moving ball in Y axis witch as a different speed ? how do I change alpha of its shadow on the ball.

To explain it nicer and make it more confusing, lets say we have a ping pong racket moving up and down and a ping pong ball that is bouncing of it. With the formula that Theis has provided before, we now know how to animate the alpha of the ping pong racket (previously the the Ball) on the ground but what about the shadow of the ping pong ball on the racket? how can we change its opacity on the racket? Would it be as simple as using the same formula on both rocket an ball:)? Can it be used as a class and be used multiple time here and there and how would you write and use it in places like update?

Here is the code I have implemented using Theis Egeberg's formula.

- (void)update:(CFTimeInterval)currentTime
{

[self enumerateChildNodesWithName:@"shadowOnTheGround" usingBlock:^(SKNode *node, BOOL *stop) {

        SKSpriteNode* racket = (SKSpriteNode*)[self childNodeWithName:@"racket"];

        node.position = CGPointMake(60,217);

        float racketY = racket.position.y;
        float theisFormula = (430.0- recketY)/430;
        node.alpha = theisFormula;
      //NSLog(@"%f", theisFormula);

}]; 
}
Community
  • 1
  • 1
adimona
  • 127
  • 7

1 Answers1

0

You can associate each shadow node with it's specific shadow caster using the userData property

shadowNode.name = @"shadowOnTheGround" //as you have set

NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:racketNode forKey:@"caster"];
//racketnode refers to the caster object

shadowNode.userData = dic;

Now, in the -update: method,

- (void)update:(CFTimeInterval)currentTime
{

    [self enumerateChildNodesWithName:@"shadowOnTheGround" usingBlock:^(SKNode *node, BOOL *stop) {

        SKSpriteNode* caster = (SKSpriteNode*)[node.userData objectForKey:@"caster"];

//You have your shadow node and it's caster, apply generic code for transforming shadow.

        node.position = CGPointMake(60,217); //Is this needed?

        float casterY = caster.position.y;
        float theisFormula = (430.0- casterY)/430;  //Make this generic
        node.alpha = theisFormula;
      //NSLog(@"%f", theisFormula);

}]; 
}

Using this code, you can associate multiple shadows with their specific objects and transform them with the same block of code.

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
  • Hi there, thanks. where and how do u use caster? I can see you have taken out rocket Node reference from the update and replaced it with the caster but how would you use the caster? would it go like caster.postion.y ? – adimona Apr 29 '14 at 09:26
  • Well, caster is just the name of the node which 'casts' the shadow. So, caster can be anything, a ball, a racquet, or whatever. – ZeMoon Apr 29 '14 at 09:46
  • And yes, you are right about using caster.postion.y. I have edited the answer. – ZeMoon Apr 29 '14 at 09:48