Sprite not appearing - ios

I have a sprite defined and it isn't appearing when I run the app. Here is my code and some images to help figure out the error:
import SpriteKit
class GameScene: SKScene {
var SingleplayerButton: SKSpriteNode! = nil
override func didMoveToView(view: SKView) {
SingleplayerButton = SKSpriteNode(imageNamed: "Unknown")
SingleplayerButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
SingleplayerButton.size = CGSize(width: 200, height: 50)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if SingleplayerButton.containsPoint(location) {
}
}
}
}

you forget addChild to your scene

Related

Multiple movable objects in swift

I am trying to make a game that has five simple objects that the user can touch and move these have physics on them but having trouble getting them to move separately any help would be appreciated.
This is what I have so far:
import SpriteKit
class GameScene: SKScene {
let starSprite = SKSpriteNode(imageNamed: "starSprite")
let circleSprite = SKSpriteNode(imageNamed: "circleSprite")
let squareSprite = SKSpriteNode(imageNamed: "squareSprite")
let triangleSprite = SKSpriteNode(imageNamed: "triangleSprite")
let moonSprite = SKSpriteNode(imageNamed: "moonSprite")
var touchLocation = CGPoint()
override func didMove(to view: SKView) {
layoutScene()
}
func layoutScene() {
backgroundColor = UIColor(red: 44/255, green: 62/255, blue:
80/255, alpha: 1.0)
starSprite.size = CGSize(width: 350, height: 350)
starSprite.position = CGPoint(x: frame.midX, y: frame.midY)
starSprite.physicsBody = SKPhysicsBody(texture:
starSprite.texture!, size: starSprite.size)
addChild(starSprite)
circleSprite.size = CGSize(width: 350, height: 350)
circleSprite.position = CGPoint(x: 0, y: 100)
circleSprite.physicsBody = SKPhysicsBody(texture:
circleSprite.texture!, size: circleSprite.size)
addChild(circleSprite)
squareSprite.size = CGSize(width: 350, height: 350)
squareSprite.position = CGPoint(x: 0, y: 200)
squareSprite.physicsBody = SKPhysicsBody(texture:
squareSprite.texture!, size: squareSprite.size)
addChild(squareSprite)
triangleSprite.size = CGSize(width: 350, height: 350)
triangleSprite.position = CGPoint(x: 0, y: 300)
triangleSprite.physicsBody = SKPhysicsBody(texture:
triangleSprite.texture!, size: triangleSprite.size)
addChild(triangleSprite)
moonSprite.size = CGSize(width: 350, height: 350)
moonSprite.position = CGPoint(x: 0, y: 400)
moonSprite.physicsBody = SKPhysicsBody(texture:
moonSprite.texture!, size: moonSprite.size)
addChild(moonSprite)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
touchLocation = touch.location(in: self)
moonSprite.position.x = (touchLocation.x)
moonSprite.position.y = (touchLocation.y)
starSprite.position.x = (touchLocation.x)
starSprite.position.y = (touchLocation.y)
}
}
}
you should first detect which object has been selected in touchDown, then change the position in touchesMoved, and release the object in touchUp!
define a value as a temp and use that!
In addition , and you don't need to use this
moonSprite.position.x = (touchLocation.x)
moonSprite.position.y = (touchLocation.y)
it's better
moonSprite.position = touchLocation
Edited Code
//
// GameScene.swift
// aa
//
// Created by ehsan on 1/30/19.
// Copyright © 2019 ehsan. All rights reserved.
//
import SpriteKit
import GameplayKit
class GameScene: SKScene {
private var spinnyNode1 = SKSpriteNode(imageNamed: "1")
private var spinnyNode2 = SKSpriteNode(imageNamed: "2")
private var movingSprite:SKSpriteNode?
override func didMove(to view: SKView) {
addChild(spinnyNode1)
addChild(spinnyNode2)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let location = t.location(in: self)
let node = self.atPoint(location)
if node is SKSpriteNode
{
movingSprite = node as? SKSpriteNode
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if movingSprite != nil
{
movingSprite!.position = touchLocation
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
movingSprite = nil
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}

SKShpapeNode position and touch.location difference

I was learning about SKNodes in Swift and have encountered a problem. App was supposed to be very simple: touching screen and getting node to appear in the touched position. I don't know why but the rectangle keeps showing in different place on the screen but sn.position is the same as touch.location(in: view) position. What's wrong?
import SpriteKit
import GameplayKit
class GameScene: SKScene {
private var spinnyNode : SKShapeNode?
private var touchPoint : CGPoint!
override func didMove(to view: SKView) {
let size = CGSize(width: 50, height: 50)
spinnyNode = SKShapeNode(rectOf: size, cornerRadius: CGFloat(0.6)) //init
if let spinnyNode = self.spinnyNode{
spinnyNode.lineWidth = 3
let rotate = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 1))
let fade = SKAction.fadeOut(withDuration: 0.5)
let remove = SKAction.removeFromParent()
spinnyNode.run(rotate)
spinnyNode.run(SKAction.sequence([fade, remove]))
}
}
func touchDown(point pos : CGPoint){
if let sn = self.spinnyNode?.copy() as! SKShapeNode? {
sn.position = CGPoint(x: touchPoint.x , y: touchPoint.y)
print("touchDown x:\(touchPoint.x), y:\(touchPoint.y)")
self.addChild(sn)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard touches.count == 1 else{
return
}
if let touch = touches.first{
touchPoint = touch.location(in: view)
touchDown(point: touchPoint)
print("touchesBegan -> x: \(touchPoint.x) y: \(touchPoint.y)")
}
}
Try doing something like this
import SpriteKit
import GameplayKit
class GameScene: SKScene {
//use whichever image for your node
var ballNode = SKSpriteNode(imageNamed: "ball")
var fingerLocation = CGPoint()
override func didMove(to view: SKView) {
ballNode.size = CGSize(width: 100, height: 100)
ballNode.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.5)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let location = touches.first?.location(in: self){
if let copy = ballNode.copy() as? SKSpriteNode {
copy.position = location
addChild(copy)
}
}
}
}
Every time I click on a random spot, the ball appears. Hope this helps.

Node not moving

I'm trying to throw a node around the screen, I've taken the below code but nothing actually happens.
When the scene loads the node spawns in the middle of the screen then drops to the bottom.
The code was taken from elsewhere in the site and granted it was a bit old so I'm assuming I'm missing something?
var sprite: SKSpriteNode!
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
override func didMove(to view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
sprite = SKSpriteNode(color: UIColor.red, size: CGSize(width: 50, height: 50))
sprite.physicsBody = SKPhysicsBody(rectangleOf: sprite.size)
sprite.position = CGPoint(x: 50.0, y: 50.0)
self.addChild(sprite)
}
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
if sprite.frame.contains(location) {
touchPoint = location
touching = true
}
}
}
func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
touchPoint = location
}
}
func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
touching = false
}
func update(currentTime: TimeInterval) {
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = velocity
}
}
}

How would I move my image with a touch and hold on the left and right side of the screen? - Swift

I need help with the movement in Swift. I have a SKSpriteNode and I need help with touch and drag movement left or right to move and the SKSpriteNode. But only the left and right. When I click on the other side than the x position of the picture is to just run over smoothly. Could anyone help how to do it? Thanks.
import SpriteKit
class PlayScene: SKScene {
let rocket = SKSpriteNode(imageNamed: "rocket")
override func didMoveToView(view: SKView) {
rocket.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 0.8)
self.addChild(rocket)
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
rocket.position = CGPoint(x: location.x, y: self.frame.height * 0.8)
}
}
In this, the rocket animates for 1 second to the location of the touch and then moves with the touch if the user moves their finger.
class GameScene: SKScene {
let rocket = SKSpriteNode(imageNamed: "rocket")
override func didMoveToView(view: SKView) {
rocket.position = CGPoint(x: self.frame.width/2, y: self.frame.height * 0.8)
self.addChild(rocket)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
rocket.position = location
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let moveAction = SKAction.moveTo(location, duration: 1/*Or as long as you want it to move*/)
rocket.runAction(moveAction)
}
}
}
Hope this helps.

Throwing an object with SpriteKit

I have the following code at the moment. Even though the code build is successful, i cannot seem to get it to work. I am trying to make it so when you flick the object, it moves at the velocity of your begin and end touch.
import SpriteKit
class GameScene: SKScene {
var sprite: SKSpriteNode!
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
override func didMoveToView(view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
self.addChild(sprite)
}
//for touch: AnyObject in touches {
//let location = touch.locationInNode(self)
//let touchedNode = self.nodeAtPoint(location)
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if sprite.frame.contains(location) {
touchPoint = location
touching = true
}
}
func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
touchPoint = location
}
func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
func update(currentTime: CFTimeInterval) {
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = velocity
}
}
}}}
You accidentally placed touchesMoved, touchesEnded and update inside touchesBegan. Besides that your code works. A hint that there were problems was the fact you didn't need to prefix touchesMoved, touchesEnded or update with override.
In the future, I would recommend using breakpoints and print statements to check the methods you expect to execute, are in fact running. Doing that you'd see that your versions of touchesMoved, touchesEnded and update weren't being called.
Anyway, here's it corrected it and now it works perfectly:
import SpriteKit
class GameScene: SKScene {
var sprite: SKSpriteNode!
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
override func didMoveToView(view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
self.addChild(sprite)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if sprite.frame.contains(location) {
touchPoint = location
touching = true
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
touchPoint = location
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
touching = false
}
override func update(currentTime: NSTimeInterval) {
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = velocity
}
}
}
ABakerSmith's solutions updated for Swift 4:
import SpriteKit
class GameScene: SKScene {
var sprite: SKSpriteNode!
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
override func didMove(to view: SKView) {
self.physicsBody = SKPhysicsBody.init(edgeLoopFrom: self.frame)
sprite = SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50))
sprite.physicsBody = SKPhysicsBody.init(rectangleOf: sprite.size)
sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
self.addChild(sprite)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first?.location(in: self) {
if sprite.frame.contains(touch) {
touchPoint = touch
touching = true
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let location = t.location(in: self)
touchPoint = location
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touching = false
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
touching = false
}
override func update(_ currentTime: TimeInterval) {
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = velocity
}
}
}
Solution updated to not have the touch/drag snap to the middle of the sprite. If you're dragging/throwing an object you're going to want that throw to come from where the user touched the object and not snap to the middle of the object. This will make it look a lot smoother
import SpriteKit
class GameScene: SKScene {
var sprite: SKSpriteNode!
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
var xDif = CGFloat()
var yDif = CGFloat()
override func didMove(to view: SKView) {
self.physicsBody = SKPhysicsBody.init(edgeLoopFrom: self.frame)
sprite = SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50))
sprite.physicsBody = SKPhysicsBody.init(rectangleOf: sprite.size)
sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
self.addChild(sprite)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first?.location(in: self) {
xDif = sprite.position.x - touch.x
yDif = sprite.position.y - touch.y
if sprite.frame.contains(touch) {
touchPoint = touch
touching = true
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let location = t.location(in: self)
touchPoint = location
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touching = false
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
touching = false
}
override func update(_ currentTime: TimeInterval) {
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-sprite.position.x + xDif, dy: touchPoint.y-sprite.position.y + yDif)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = velocity
}
}
}

Resources