我正在尝试让精灵闪现白,就像它会受到伤害一样。这将涉及通过SKAction控制一些参数。首先,我尝试将SKCropNode与白色SKShapeNode结合使用,但我不认为我完全理解它是如何工作的,因为它没有做任何事情。我的第二次尝试是对精灵进行着色,但我发现当你为一个精灵白色着色时,它只显示其原始颜色。我确定着色器会起作用,但我对它们没有多少经验。有什么想法吗?
[编辑]
好的,在阅读了关于这个主题的一百多万个网站之后,我遇到了以下SO question,其中说SKCropNodes仅适用于SKSpriteNodes(用于屏蔽等)。所以我更新了我的代码并允许我的精灵变白。我以为我能够运行SKAction来调低白色SKSpriteNode上的alpha以显示屏蔽节点,但这只会让整个精灵消失。所以我想也许我可以将原始图像添加到SKCropNode,但随后白色SKSpriteNode消失了。我决定创建一个SKAction来调暗白色SKSpriteNode,然后将原始节点添加到SKCropNode。它有效,但有更好的方法吗?
SKSpriteNode *prod = [SKSpriteNode spriteNodeWithImageNamed:@"some image"];
[prod setSize:CGSizeMake(prod.frame.size.width*0.2, prod.frame.size.height*0.2)];
prod.zPosition = -1;
SKSpriteNode *mask = [SKSpriteNode spriteNodeWithImageNamed:@"some image"];
[mask setSize:CGSizeMake(mask.frame.size.width*0.2, mask.frame.size.height*0.2)];
mask.zPosition = 1;
//mask.position = CGPointMake(300, 300);
SKSpriteNode *shape = [SKSpriteNode spriteNodeWithColor:[NSColor whiteColor] size:CGSizeMake(800, 600)];
shape.zPosition = 2;
SKCropNode *cropNode = [SKCropNode node];
cropNode.maskNode = mask;
[cropNode addChild:shape];
//[cropNode addChild:prod];
cropNode.position = CGPointMake(300, 300);
[self addChild:cropNode];
SKAction *fadeOut = [SKAction fadeAlphaTo:0 duration:0.1];
[shape runAction:fadeOut completion:^{
[cropNode addChild:prod];
}];
答案 0 :(得分:2)
我明白了。这是我编写的用于处理此类的类的重要部分...忽略类的名称...它是我的应用程序特有的东西,只需调用它,无论你需要它被调用:
<强> VersusImage.h 强>
#import <SpriteKit/SpriteKit.h>
@interface VersusImage : SKSpriteNode
-(instancetype) initWithImage:(SKSpriteNode*)image;
-(void) flashWhite;
@end
<强> VersusImage.m 强>
#import "VersusImage.h"
@interface VersusImage()
@property SKCropNode *cropNode;
@property SKSpriteNode *image;
@property SKSpriteNode *shape;
@end
@implementation VersusImage
-(instancetype) initWithImage:(SKSpriteNode*)image {
if (self = [super init]) {
_image = image;
[self initImage];
}
return self;
}
-(void) initImage {
_image.zPosition = -1;
_image.alpha = 1;
float width = _image.frame.size.width, height = _image.frame.size.height;
SKTexture *texture = [_image texture];
SKSpriteNode *mask = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(width, height)];
mask.zPosition = 1;
_shape = [SKSpriteNode spriteNodeWithColor:[NSColor whiteColor] size:CGSizeMake(width, height)];
_shape.zPosition = 2;
_shape.alpha = 0;
_cropNode = [SKCropNode node];
_cropNode.maskNode = mask;
[_cropNode addChild:_shape];
[_cropNode addChild:_image];
[self addChild:_cropNode];
}
-(void) flashWhite {
SKAction *showWhite = [SKAction fadeAlphaTo:1.0 duration:0];
SKAction *hideImage = [SKAction fadeAlphaTo:0 duration:0];
SKAction *fadeOut = [SKAction fadeAlphaTo:0 duration:0.2];
SKAction *fadeIn = [SKAction fadeAlphaTo:1 duration:0.2];
[_shape runAction:[SKAction sequence:@[showWhite, fadeOut]]];
[_image runAction:[SKAction sequence:@[hideImage, fadeIn]]];
}
@end