Sprite Kit - Shadow on multiple moving object affected impulse and Gravity

筅森魡賤 提交于 2019-12-11 19:49:03

问题


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);

}]; 
}

回答1:


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.



来源:https://stackoverflow.com/questions/23353615/sprite-kit-shadow-on-multiple-moving-object-affected-impulse-and-gravity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!