2015-03-10 2 views
2

Я пытаюсь создать учебное пособие в игре, которое проходит через части пользовательского интерфейса и выделяет их, в то время как затемнение остальной части сцены.SpriteKit: Как вы выделяете раздел сцены, как в учебнике?

Я думаю, что смогу это сделать с помощью Sprites и SKBlendMode, но это плохо объяснено в справочнике по яблокам.

Любая идея?

ответ

2

Одним из способов достижения этого является составление желаемого «cookie» с помощью SKSpriteNodes, а затем создание текстуры для рендеринга «как есть» на новый SpriteNode, который затем будет смешаться в целом со сценой.

В этом простом примере я использовал прямоугольник для выделения, но вы можете сделать этот узел любым типом или изображением узла и соответствующим образом отрегулировать значение альфа.

Нарисуй свою сцену, как обычно, а затем добавить этот код:

// dim the entire background of the scene to 70% darker 
SKSpriteNode* background = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithRed:0 
                      green:0 
                       blue:0 
                      alpha:0.7] 
                 size:self.frame.size]; 

// make a square of 100,100. This could be an image or shapenode rendered to a spritenode 
// make the cut out only dim 20% - this is because no dim will look very harsh 
SKSpriteNode* cutOut = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithRed:0 
                     green:0 
                      blue:0 
                     alpha:0.2] 
                size:CGSizeMake(100,100)]; 

// add the cut out to the background and make the blend mode replace the colors 
cutOut.blendMode = SKBlendModeReplace; 
[background addChild:cutOut]; 

// we now need to make a texture from this node, otherwise the cutout will replace the underlying 
// background completely 

SKTexture* newTexture = [self.view textureFromNode:background]; 
SKSpriteNode* newBackground = [SKSpriteNode spriteNodeWithTexture:newTexture]; 

// position our background over the entire scene by adjusting the anchor point (or position) 
newBackground.anchorPoint = CGPointMake(0,0); 
[self addChild:newBackground]; 

// if you have other items in the scene, you'll want to increaes the Z position to make it go ontop. 
newBackground.zPosition = 5; 
+0

Или использовать SKCropNode для более быстрого решения. – Okapi

+0

Я думал, что SKCropNode обрезает спрайт, а не инвертирует обрезанную область - или есть режим обрезки, чтобы изменить его поведение? – GilesDMiddleton

+0

Есть ли быстрое решение? Спасибо – Marin

Смежные вопросы