3

I want to show any effects or animations for my SKSpriteNode when it was dragging on screen.

I gone through many sites but i didn't find relevant answer for my question.I have little bit knowledge on SKSpriteNode.

Can any one guide me to solve this issue.

Thanks.

Mounika
  • 412
  • 2
  • 20
  • Have you tried using SKLightNodes? – Knight0fDragon Dec 13 '16 at 16:25
  • Take a look at this answer: http://stackoverflow.com/a/40362874/6728196. It shows how to make a glowing effect. Then you can hide it when not moving or whatever you want – Nik Dec 13 '16 at 19:53
  • how are you currently creating your shadows? – Confused Dec 13 '16 at 21:27
  • or... how do you want your shadows to look and behave? Either answer will help me answer your question. – Confused Dec 13 '16 at 21:27
  • Okay i will explain... I have an Apple image on screen now i am able to drag that image using mouse.While i am dragging i need to show glow effect or shadow or something else. – Mounika Dec 14 '16 at 05:06
  • Ok. So you want to show that it's been lifted up off the surface whilst being dragged? That's your reason for the glow/shadow? btw, when replying, add my name to the reply like I'll do with yours, here: @Mounika, that way I'll get notified of a reply. – Confused Dec 14 '16 at 10:16
  • @Confused Okay is there any way to solve my issue ? – Mounika Dec 14 '16 at 10:19
  • just use SKLightNode, place it whereever you want your light source to come from – Knight0fDragon Dec 14 '16 at 15:03
  • @Knight0fDragon I tried that here is my code var skLightNode = new SKLightNode(); skLightNode.LightColor = NSColor.White; skLightNode.Position = new CGPoint(0, 0); skLightNode.Falloff = 1; skLightNode.AmbientColor = NSColor.DarkGray; this.AddChild(skLightNode); but not showing any difference :( – Mounika Dec 15 '16 at 04:32

1 Answers1

3

Use a factory to make a shadow:

import SpriteKit

class MAKE {

    private static let view:SKView = SKView()

    static func makeShadow(from source: SKTexture, rgb: SKColor, a: CGFloat) -> SKSpriteNode {
        let shadowNode = SKSpriteNode(texture: source)
            shadowNode.colorBlendFactor = 0.5  // makes the following line more effective
            shadowNode.color = SKColor.gray // makes for a darker shadow. Off for "glow" shadow
        let textureSize = source.size()
        let doubleTextureSize = CGSize(width: textureSize.width * 2, height: textureSize.height * 2)
        let framer = SKSpriteNode(color: UIColor.clear, size: doubleTextureSize)
            framer.addChild(shadowNode)
        let filter = CIFilter(name: "CIGaussianBlur")
        let blurAmount = 10
        filter?.setValue(blurAmount, forKey: kCIInputRadiusKey)
        let effectsNode = SKEffectNode()
            effectsNode.filter = filter
            effectsNode.blendMode = .alpha
            effectsNode.addChild(framer)
            effectsNode.shouldRasterize = true
        let tex = view.texture(from: effectsNode)
        let shadow = SKSpriteNode(texture: tex)
            shadow.colorBlendFactor = 0.5
            shadow.color = rgb
            shadow.alpha = a
            shadow.zPosition = -1
    return shadow
    }
}

Now make a button anyway you like, but make sure you have created a buttonTexture that's the exact same size and shape as your button, preferably a grey rendition of it. You'll need this to send to the blurring factory shadowmaker above, like this:

shadowSprite = MAKE.makeShadow(from: buttonTexture, rgb: myColor, a: 0.33)
        shadowSprite.position = CGPoint(x: self.frame.midX, y: self.frame.midY - 5)
        addChild(shadowSprite)

You can see more details on how and why a factory, here: Create \(Use) SKView as \(in a) factory \(static class)

Community
  • 1
  • 1
Confused
  • 6,048
  • 6
  • 34
  • 75
  • 1
    Thanks for your answer I am trying in Xamarin.Mac i think its a swift code :( – Mounika Dec 14 '16 at 12:41
  • I don't know anything about C# @Mounika. Or the key of E. – Confused Dec 14 '16 at 12:51
  • Hmm... okay i will try to your answer in tech – Mounika Dec 14 '16 at 13:08
  • A majority of answers that you may find won't be in C#. Learning to translate to C# is an important skill. These docs may help - https://developer.xamarin.com/guides/mac/application_fundamentals/patterns/ https://developer.xamarin.com/guides/mac/application_fundamentals/mac-apis/ – Chris Hamons Dec 15 '16 at 18:41