Scale SpriteNode from a Specific Point - swift

When scaling a spriteNode to a smaller size (or larger size), the spriteNode by default scales from the centre, which is usually fine. However, I need my sprite to scale from the very left of it's body. Is there any way of changing the spriteNode's centre point so that I can achieved this?
I tried adjusting the anchor point of the spriteNode (below), which does scale from the left of the body, but it also repositions the spriteNode, which I do not want.
import SpriteKit
var mySprite: SKSpriteNode!
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
backgroundColor = UIColor.greenColor()
mySprite = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 50))
mySprite.anchorPoint = CGPointMake(0.0, 0.5)
addChild(mySprite)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
mySprite.runAction(SKAction.scaleTo(0.5, duration: 1))
}
I was hoping for something like the below (which doesn't exist):
mySprite.centerPoint.x = -mySprite.size.width/2
Also, the below gets the result I want, but I don't really want to have to adjust the spriteNode position everytime I adjust the anchorPoint:
var mySprite: SKSpriteNode!
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
anchorPoint = CGPointMake(0.5, 0.5)
backgroundColor = UIColor.greenColor()
mySprite = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 50))
mySprite.anchorPoint = CGPointMake(0.0, 0.5)
mySprite.position.x = -mySprite.size.width/2
addChild(mySprite)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
mySprite.runAction(SKAction.scaleTo(0.5, duration: 1))
}

Try to delete mySprite.anchorPoint = CGPointMake(0.0, 0.5) in didload and in touchesBegan:
mySprite.anchorPoint = CGPointMake(0.0, 0.5)
func repositionAncorPoint() {
mySprite.anchorPoint = CGPointMake(0.5, 0.5)
}
let resetAncor = SKAction.runBlock({ repositionAncorPoint() })
let rescale = SKAction.scaleTo(0.5, duration: 1)
let sequence = [rescale,resetAncor]
mySprite.runAction(SKAction.sequence(sequence))
I've not test it... :)

Related

SpriteKit: How to create a region that's the same size as the device screen?

I'm working in SpriteKit and I need to create a 'playable area', an area that is same size as the device screen so that I can stop my player from moving off-screen.
I'm using the following line of code:
var playableRect: CGRect = UIScreen.main.bounds
But the resulting rectangle is about a quarter of the device screen, with a corner of that rectangle at what looks to be the center of the screen. And the device orientation doesn't change that.
I've tried everything I can think of. Nothing is working.
How do I create a rectangle that's the same size as the device screen?
Here's my full coding, after changing to include Gene's suggestion. I incorporated Gene's suggestion by revising playableRect inside the didMove method. But the result is unchanged with that coding.
import SpriteKit
import GameplayKit
import CoreMotion
#objcMembers
class GameSceneUsingTilt: SKScene {
let player = SKSpriteNode(imageNamed: "player-motorbike")
let motionManager = CMMotionManager()
var playableRect = UIScreen.main.bounds
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "road")
background.zPosition = -1
addChild(background)
if let particles = SKEmitterNode(fileNamed: "Mud") {
let farRightPt = frame.maxX // start the emitter at the far right x-point of the view
particles.advanceSimulationTime(10)
particles.position.x = farRightPt
addChild(particles)
}
let nearLeftPt = frame.minX * 3 / 4 // start the player at a quarter of the way to the far left x-point of the view
player.position.x = nearLeftPt
player.zPosition = 1
addChild(player)
motionManager.startAccelerometerUpdates()
// coding below shows outline of playableRect
let bounds = UIScreen.main.bounds
let scale = UIScreen.main.scale
// let size = CGSize(width: bounds.size.width * scale, height: bounds.size.height * scale)
playableRect = CGRect(x: 0, y: 0, width: bounds.size.width * scale, height: bounds.size.height * scale)
drawPlayableRect(rect: playableRect)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
func boundsCheck() {
let playableRect = CGRect(x: frame.minX, y: frame.minY, width: frame.width, height: frame.height)
let bottomLeft = CGPoint(x: playableRect.minX, y: playableRect.minY)
let topRight = CGPoint(x: playableRect.maxX, y: playableRect.maxY)
if player.position.x <= bottomLeft.x {
player.position.x = bottomLeft.x
}
if player.position.x >= topRight.x {
player.position.x = topRight.x
}
if player.position.y <= bottomLeft.y {
player.position.y = bottomLeft.y
}
if player.position.y >= topRight.y {
player.position.y = topRight.y
}
}
override func update(_ currentTime: TimeInterval) {
if let accelerometerData = motionManager.accelerometerData {
// note that since the game will be running in landscape mode, up and down movement is controlled by the x axis and right to left movement is controlled by the y-axis...so this is the inverse of what you'd normally think.
let changeX = CGFloat(accelerometerData.acceleration.y) * 4
let changeY = CGFloat(accelerometerData.acceleration.x) * 4
// you have to subtract from x position and add to the y position because device is rotated so the axises aren't what you would normally expect.
player.position.x -= changeX
player.position.y += changeY
// check to make sure position isn't outside payable area:
boundsCheck()
}
}
// function below shows outline of playableRect
func drawPlayableRect(rect: CGRect) {
let shape = SKShapeNode()
let path = CGMutablePath()
path.addRect(rect)
shape.path = path
shape.strokeColor = .red
shape.lineWidth = 4.0
addChild(shape)
}
}
Use: UIScreen.main.nativeBounds

How to move sprite relative to its original position with touch in spritekit

I looking to move a sprite relative to the player's touch. I am familiar with the moveTo SKActions, however I am wondering how to implement sprite movement where the sprite moves along with the user's touch movement.
For example, I have a sprite in the centre of the screen. If I apply a touch at the bottom of the screen and move my finger up , the sprite will move up from the centre(it's original position).
Thanks!
Try this :
import SpriteKit
class GameScene: SKScene {
var node = SKSpriteNode()
var nodePosition = CGPoint()
var startTouch = CGPoint()
override func didMove(to view: SKView) {
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
// node set up
node = SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50))
node.position = CGPoint.zero
self.addChild(node)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
startTouch = location
nodePosition = node.position
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
node.run(SKAction.move(to: CGPoint(x: nodePosition.x + location.x - startTouch.x, y: nodePosition.y + location.y - startTouch.y), duration: 0.1))
}
}
}

How to change the alpha value of spriteNode?

I am changing the alpha value of spriteNode like this,
if (firstBody.node?.name)! == "Player" && secondBody.node?.name == "Alpha 1" {
var item: SKSpriteNode?
item = SKSpriteNode(imageNamed: "BG")
item!.alpha = 0.1
score += 1
scoreLabel?.text = String(score)
}
This is what it says in the documentation (use node.alpha = value), but I am not sure why it isn't working.
you can fade the alpha by using a Action, I have left an example below.
class GameScene: SKScene {
var player: SKSpriteNode!
override func didMove(to view: SKView) {
player = SKSpriteNode(color: .purple, size: CGSize(width: 100, height: 100))
player.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(player)
}
func fadePlayer() {
let fadeAlpha = SKAction.fadeAlpha(to: 0.1, duration: 1.0)
self.run(fadeAlpha)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
fadePlayer()
}
}
When a touch begins the SpriteNode will fade out to the alpha value you insert, it will fade out gradually during a time you insert in the duration also, This action fades the SpriteNode's alpha to 0.1 gradually over 1 second.

My Sprite won't perform the actions I tell it to?

I am making an app in Xcode 7 using Swift and SpriteKit. My idea is to have a spaceship that moves with the finger, or basically just follows it, yet I'm having trouble. This is the code:
import Foundation
import SpriteKit
class secondScene: SKScene {
override func didMoveToView(view: SKView) {
// var timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector(spawnBullets()), userInfo: nil, repeats: true)
//buttons
let leftbutton = SKSpriteNode(imageNamed: "LeftArrow")
leftbutton.position = CGPoint(x: self.frame.size.width*0.2, y: self.frame.size.height*0.1)
leftbutton.size = CGSize(width: 220, height: 80)
leftbutton.color = SKColor.blueColor()
leftbutton.zPosition = 100
self.addChild(leftbutton)
let rightbutton = SKSpriteNode(imageNamed: "RightArrow")
rightbutton.position = CGPoint(x: self.frame.size.width*0.8, y: self.frame.size.height*0.1)
rightbutton.size = CGSize(width: 220, height: 80)
rightbutton.color = SKColor.blueColor()
rightbutton.zPosition = 100
self.addChild(rightbutton)
//spaceship
let spaceship = SKSpriteNode(imageNamed: "spaceship")
spaceship.zPosition = 100
spaceship.size = CGSize(width: 200, height: 120)
spaceship.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.15)
self.addChild(spaceship)
}
let spaceship = SKSpriteNode(imageNamed: "spaceship")
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
spaceship.position.x = location.x
}
}
}
I run this and to me it seems like my spaceship should follow my finger, yet it doesn't. It just stays in the position I specifies before. I've got no errors and I'm out of ideas. Any thoughts?
Remove this line in your spaceship code in didMoveToView
let spaceship = SKSpriteNode(imageNamed: "spaceship")
so that you are actually using the global property you created above touchesBegan.
You probably also want to include/move your code in touchesMoved.
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
spaceship.position.x = location.x
}
}
Hope this helps

swift 2: Sprite tilting effect

Looking for a method to tilt a sprite upwards on the right side when tapping the screen (like in flappy bird) Like a start of a rotation that returns to the place it were. I will use this as an effect of going faster then the sprite normally does.
I'll add the code I got so far below:
var player = SKSpriteNode()
override func didMoveToView(view: SKView) {
let playerTexture = SKTexture(imageNamed: "player")
player = SKSpriteNode(texture: playerTexture)
player.position = CGPoint(x: 200, y: 80)
player.physicsBody = SKPhysicsBody(texture: playerTexture, size: playerTexture.size())
player.physicsBody!.affectedByGravity = true
self.addChild(player)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}