Assign Multiple Masks to SKCropNode - swift

As an example, I have a circle which travels to the left and then the right on the screen. I want the circle to only be visible if it is inside two specific squares (maskNodes). I'm using SKCropNode to try and achieve this, but the SKCropNode mask only lets me assign one mask. Does anybody know a way of assigning two or more mask to a SKCropNode, or if it is even possible to do so. Thanks!
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
backgroundColor = UIColor.whiteColor()
let mask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
mask1.position.x = -100
let mask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
mask2.position.x = 100
let cropNode = SKCropNode()
cropNode.maskNode = mask1 // && mask2
addChild(cropNode)
let circle = SKShapeNode(circleOfRadius: 25)
circle.fillColor = UIColor.blackColor()
cropNode.addChild(circle)
// Move Circle
let moveLeft = SKAction.moveToX(-frame.size.width/2, duration: 2)
let moveRight = SKAction.moveToX(frame.size.width/2, duration: 2)
let seq = SKAction.repeatActionForever(SKAction.sequence([moveLeft, moveRight]))
circle.runAction(seq)
}

Figured it out, just has to add the two mask to a parent and then assign the parent as the SKCropNode mask.
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
backgroundColor = UIColor.whiteColor()
let maskParent = SKSpriteNode()
let mask1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
mask1.position.x = -100
maskParent.addChild(mask1)
let mask2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
mask2.position.x = 100
maskParent.addChild(mask2)
let cropNode = SKCropNode()
cropNode.maskNode = maskParent
addChild(cropNode)
let circle = SKShapeNode(circleOfRadius: 25)
circle.fillColor = UIColor.blackColor()
cropNode.addChild(circle)
// Move Circle
let moveLeft = SKAction.moveToX(-frame.size.width/2, duration: 2)
let moveRight = SKAction.moveToX(frame.size.width/2, duration: 2)
let seq = SKAction.repeatActionForever(SKAction.sequence([moveLeft, moveRight]))
circle.runAction(seq)
}

Related

How to change color of SKShapeNode randomly each time spawned?

Currently, when running this code, a colored ball runs from the top of the screen to the bottom. When it reaches the bottom it respawns at the top and drops again. I need the ball to change to a random color each time it spawns again. I have a colorChange action but it still doesn't change the color of the ball when it runs.
import SpriteKit
class GameScene: SKScene {
let ball = SKShapeNode(circleOfRadius: 20)
let label = SKLabelNode(fontNamed: "Futura")
let colorChange = SKAction.colorizeWithColor(.blueColor(), colorBlendFactor: 1, duration: 1)
let randomRed = Int(arc4random_uniform(255))
let randomGreen = Int(arc4random_uniform(255))
let randomBlue = Int(arc4random_uniform(255))
override func didMoveToView(view: SKView) {
let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = sceneBody
//Ball Transition
let ballTransition = SKAction.sequence([SKAction.fadeInWithDuration(1)])
ball.runAction(ballTransition)
ball.fillColor = UIColor(red: 100, green: 100, blue: 0, alpha: 1)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 25)
ball.physicsBody?.affectedByGravity = false
ball.position = CGPoint(x: self.frame.size.width/2, y: CGFloat(self.frame.size.height*1))
ballMovement()
self.addChild(ball)
self.addChild(label)
}
func ballMovement() {
let moveBall = SKAction.moveToY(0, duration: 3)
let goBackUp = SKAction.moveToY(self.frame.size.height, duration:0)
let keepFalling = SKAction.sequence([moveBall, goBackUp, colorChange])
ball.runAction(SKAction.repeatActionForever(keepFalling))
//Label Sprite
label.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
label.fontColor = SKColor.redColor()
label.fontSize = 30
}
override func update(currentTime: CFTimeInterval) {
label.text = "\(ball.position.y)"
if ball.position.y < 26 {
ball.position = CGPoint(x: self.frame.size.width/2, y: CGFloat(self.frame.size.height*1))
}
}
}
Instead of using an SKAction to change the color, just do
ball.color = UIColor...
To generate a random color check this out: How to make a random background color with Swift
and use that to generate a random color
ball.color = .randomColor()
I suggest you don't use an SKShape for an important piece that moves because I find that they can slow down the app and cause problems. I would use an SKSpritenode instead for the ball. If you then make the ball default as all white, you can change the colour by doing this:
ball.colorBlendFactor = 1
ball.color = SKColor(hue: randomNumberFunctionBetweenZeroAndOne(), saturation: 0.75, contrast: 0.5)
The hue should be a random value between 0 and 1.
If you put this inside a function, you could call the function to change the colour of the ball inside and SKAction or anywhere else.

How to code the accelerometer to have a fixed x position

I have a ship that moves based on the direction of gravity which is changed by the accelerometer, but I want it to only move along the width of the screen (I already know how to do that), but I do not know how to keep it on a fixed horizontal line. Here's my code
class GameScene: SKScene {
var manager = CMMotionManager()
var ship = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let shipTexture = SKTexture(imageNamed: "EvadersShipVert2.png")
ship = SKSpriteNode(texture: shipTexture)
ship.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) - 250)
ship.size = CGSize(width: 90, height: 115)
shipTexture.filteringMode = SKTextureFilteringMode.Nearest
ship.zPosition = 2
ship.physicsBody = SKPhysicsBody(texture: shipTexture, size: CGSize(width: 90, height: 115))
ship.physicsBody?.dynamic = true
self.addChild(ship)
manager.startAccelerometerUpdates()
manager.accelerometerUpdateInterval = 0.1
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
(data, error) in
self.physicsWorld.gravity = CGVectorMake(CGFloat((data?.acceleration.x)!), CGFloat((data?.acceleration.y)!))
}
}
thanks in advance!
Replace
self.physicsWorld.gravity = CGVectorMake(CGFloat((data?.acceleration.x)!), CGFloat((data?.acceleration.y)!))
With
self.physicsWorld.gravity = CGVectorMake(0.0, CGFloat((data?.acceleration.y)!)
So you're not affecting the x position

Making SKLabelNode as a crop node of SKShapeNode

Can't find in web, how to make SKLabelNode cropping SKShapeNode. When i colorize the background, my goal is colorize the label too with the same method, so both of them have to colorize simultaneously. But can't imagine how to crop SKShapeNode with this label. Help me please!
But can't imagine how to crop SKShapeNode with this label.
If I understand you correctly, you can set SKLabelNode as a mask of a SKCropNode, like this:
override func didMoveToView(view: SKView) {
backgroundColor = .blackColor()
let cropNode = SKCropNode()
cropNode.position = CGPoint(x: frame.midX, y: frame.midY)
cropNode.zPosition = 1
let mask = SKLabelNode(fontNamed: "ArialMT")
mask.text = "MASK"
mask.fontColor = .greenColor()
mask.fontSize = 28
cropNode.maskNode = mask
let nodeToMask = SKSpriteNode(color: .purpleColor(), size: CGSize(width: 200, height: 200))
nodeToMask.position = CGPoint(x: 0, y: 0)
nodeToMask.name = "character"
cropNode.addChild(nodeToMask)
//Now colorize the sprite which acts like background
let colorize = SKAction.sequence([
SKAction.colorizeWithColor(.orangeColor(), colorBlendFactor: 0, duration: 1),
SKAction.colorizeWithColor(.purpleColor(), colorBlendFactor: 0, duration: 1)
])
nodeToMask.runAction(SKAction.repeatActionForever(colorize), withKey: "colorizing")
addChild(cropNode)
}
The result:

Fading action of SKNode() with SKCropNode() children

I have a SKNode() which has SKCropNode() children. I am able to run all kind of actions to rotate and scale my SKNode(), but when I want to use any kind of fading actions the result always is alpha 1 or alpha 0. No way of fading.
By replacing the SKCropNode() with a SKShapeNode() the fading action is working fine.
Can anyone tell my why? Isn't it possible to fade masked nodes?
import SpriteKit
class GameScene: SKScene {
let player = SKNode()
let playerSize = CGFloat(50)
let playerCrop = SKCropNode()
let playerMask = SKSpriteNode(color: SKColor.blackColor(), size: CGSizeMake(CGFloat(100), CGFloat(100)))
let playerCircle = SKShapeNode(circleOfRadius: CGFloat(100))
let playerCenterMask = SKShapeNode(circleOfRadius: CGFloat(100))
let playerCenterCrop = SKCropNode()
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.blackColor()
player.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
player.alpha = 0
addChild(player)
playerCenterMask.lineWidth = 20
playerCenterCrop.maskNode = playerCenterMask
playerMask.position.x = -playerSize
playerMask.position.y = playerSize
playerCircle.lineWidth = 0
playerCircle.fillColor = SKColor.redColor()
playerCrop.addChild(playerCircle)
playerCrop.maskNode = playerMask
playerCenterCrop.addChild(playerCrop)
player.addChild(playerCenterCrop)
// Animations
let playerScale = SKAction.scaleTo(2.0, duration: 5)
let playerFadeIn = SKAction.fadeInWithDuration(5)
let playerAnimation = SKAction.group([playerScale,playerFadeIn])
player.runAction(playerAnimation, completion: {})
}
}
As mentioned in the comments:
SKCropNode uses alpha < 0.5 to not draw. >= 0.5 to draw, and you can't set the blend mode, so it is probably doing source blend mode. Which means it is overwriting the alpha. Children afterwards get blended.
The player context is created, it draws at the given alpha, then the SKCropNode is drawn, overwriting alpha instead of blending.
Run the fade in action on your child, not your parent to get the results you are looking for.
Here is what your source looks like with changes:
import SpriteKit
class GameScene: SKScene {
let player = SKNode()
let playerSize = CGFloat(50)
let playerCrop = SKCropNode()
let playerMask = SKSpriteNode(color: SKColor.blackColor(), size: CGSizeMake(CGFloat(100), CGFloat(100)))
let playerCircle = SKShapeNode(circleOfRadius: CGFloat(100))
let playerCenterMask = SKShapeNode(circleOfRadius: CGFloat(100))
let playerCenterCrop = SKCropNode()
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.blackColor()
player.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
player.alpha = 1
addChild(player)
playerCircle.alpha = 0
playerCenterMask.lineWidth = 20
playerCenterCrop.maskNode = playerCenterMask
playerMask.position.x = -playerSize
playerMask.position.y = playerSize
playerCircle.lineWidth = 0
playerCircle.fillColor = SKColor.redColor()
playerCrop.addChild(playerCircle)
playerCrop.maskNode = playerMask
playerCenterCrop.addChild(playerCrop)
player.addChild(playerCenterCrop)
// Animations
let playerScale = SKAction.scaleTo(2.0, duration: 5)
let playerFadeIn = SKAction.fadeInWithDuration(5)
let playerAnimation = playerScale
player.runAction(playerAnimation, completion: {})
playerCircle.runAction(playerFadeIn, completion: {})
}
}

action makes shape node disappear when using bezier path action

I am trying to move a SKShapeNode along a UIBezierPath. Here is the code I have so far:
// Set up the circle track
let circleTrack = UIBezierPath(roundedRect: CGRectMake(screenWidth/2, screenHeight/2, screenWidth/3, screenWidth/3), cornerRadius: 100)
let shapeTrack = SKShapeNode(path: circleTrack.CGPath, centered: true)
shapeTrack.position = CGPointMake(screenWidth/2, screenHeight/2)
shapeTrack.strokeColor = SKColor.whiteColor()
self.addChild(shapeTrack)
// Create the ball
let circle = SKShapeNode(circleOfRadius: 15)
circle.position = CGPointMake(screenWidth/2, screenHeight/2)
circle.fillColor = SKColor.whiteColor()
self.addChild(circle)
//Move the circle
circle.runAction(SKAction.repeatActionForever(SKAction.followPath(circleTrack.CGPath, speed: 3.0)))
All the action does is make the shape node disappear. How can I get it so that the circle moves along the bezier path forever?
You are placing the bezier path circleTrack in one position, and the shapeTrack in another position. The origin of circleTrack is at (screenWidth/2,screenHeight/2) and the shapeTrack is centered at (screenWidth/2,screenHeight/2). shapeTrack.position is the position of the center of the SKShapeNode. Try the following code.
let screenWidth = size.width
let screenHeight = size.height
let trackWidth = screenWidth/3
let circleTrack = UIBezierPath(roundedRect: CGRectMake(screenWidth/2 - trackWidth/2, screenHeight/2 - trackWidth/2, trackWidth, trackWidth), cornerRadius: 100)
let shapeTrack = SKShapeNode(path: circleTrack.CGPath, centered: true)
shapeTrack.position = CGPointMake(screenWidth/2, screenHeight/2)
shapeTrack.strokeColor = UIColor.whiteColor()
self.addChild(shapeTrack)
// Create the ball
let circle = SKShapeNode(circleOfRadius: 15)
circle.fillColor = UIColor.whiteColor()
self.addChild(circle)
let followPath = SKAction.followPath(circleTrack.CGPath, asOffset: false, orientToPath: false, speed: 200.0)
//Move the circle
circle.runAction(SKAction.repeatActionForever(followPath))