我在尝试让 SKSpriteNode 同时投射阴影并在进入同一光源的阴影中消失时没有成功。我可以做到其中一个,但不是两个都能实现。
根据文档: 如果精灵在光线投射的阴影内,并且精灵的z位置低于光线,则阴影会影响精灵的照明。 我已经完成了所有这些操作。我的SKLightNode的zPosition为100,所有其他节点的zPositions都较低。
我尝试过所有关于 lightingBitMask、shadowCastBitMask 和 shadowedBitMask 的设置组合,但都没有效果。
我在下面发布了复制了问题的代码。蓝色的方块投射出阴影,但没有被墙上的阴影覆盖。紫色的方块不投射阴影,并被墙上的阴影覆盖。
光源会响应触摸移动,欢迎随意在屏幕上移动它。该项目处于横向模式。
我缺少什么或者没有看到什么?
#import "GameScene.h"
@implementation GameScene {
SKSpriteNode *lightBulb;
}
-(void)didMoveToView:(SKView *)view {
typedef NS_OPTIONS(uint32_t, Level1LightCategory)
{
CategoryLightPlayer = 1 << 0,
};
SKSpriteNode *worldNode = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(1136, 640)];
worldNode.zPosition = 10;
//worldNode.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:worldNode];
lightBulb = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(20, 20)];
lightBulb.zPosition = 100;
lightBulb.position = CGPointMake(50, 50);
[worldNode addChild:lightBulb];
SKLightNode *light = [[SKLightNode alloc] init];
//light.zPosition = 100; // <- tried setting this again but to no effect
light.categoryBitMask = CategoryLightPlayer;
light.falloff = 0.3;
light.ambientColor = [UIColor whiteColor];
light.lightColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
light.shadowColor = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
[lightBulb addChild:light];
SKSpriteNode *wall = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(10, 300)];
wall.zPosition = 50;
wall.position = CGPointMake(500, 200);
wall.lightingBitMask = CategoryLightPlayer;
wall.shadowCastBitMask = CategoryLightPlayer;
wall.shadowedBitMask = 0x00000000;
[worldNode addChild:wall];
SKSpriteNode *box0 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(30, 30)];
box0.zPosition = 40;
box0.position = CGPointMake(800, 200);
box0.lightingBitMask = CategoryLightPlayer;
box0.shadowCastBitMask = CategoryLightPlayer;
box0.shadowedBitMask = CategoryLightPlayer;
[worldNode addChild:box0];
SKSpriteNode *box1 = [SKSpriteNode spriteNodeWithColor:[SKColor purpleColor] size:CGSizeMake(30, 30)];
box1.zPosition = 40;
box1.position = CGPointMake(800, 300);
box1.lightingBitMask = CategoryLightPlayer;
//box1.shadowCastBitMask = CategoryLightPlayer;
//box1.shadowedBitMask = CategoryLightPlayer;
[worldNode addChild:box1];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
lightBulb.position = touchLocation;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
lightBulb.position = touchLocation;
}
-(void)update:(CFTimeInterval)currentTime {
//
}
@end