(SpriteKit + Swift 2) - Remove sprite when user tapped on it - ios

I am trying to make a simple bubble tapped game. I have a created a SKSpriteKid node in my code. The problem is I want to sprite to disappear when user tap on it. I can't seem to removeparent() the node. How do I tackle this?
I have created a sprite with bubble1 as the name.
func bubblePressed(bubble1: SKSpriteNode) {
print("Tapped")
bubble1.removeFromParent()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let touchLocation = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(touchLocation)
if (touchedNode.name == "bubble1") {
touchedNode.removeFromParent()
print("hit")
}
}
}
//the node's creation.
func bubblesinView() {
//create the sprite
let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")
//spawn in the X axis min is size, max is the total height minus size bubble
let width_bubble_1 = random(min: bubble1.size.width/2, max: size.width - bubble1.size.height/2)
//y: size.height * X, X is 0 at bottom page to max
//spawn position, let x be random
bubble1.position = CGPoint(x: width_bubble_1, y: size.height)
// Add the monster to the scene
addChild(bubble1)
// Determine speed of the monster from start to end
let bubblespeed = random(min: CGFloat(1.1), max: CGFloat(4.0))
//this tell the bubble to move to the given position, changeble x must be a random value
let moveAction = SKAction.moveTo(CGPoint(x: width_bubble_1, y: size.height * 0.1), duration: NSTimeInterval(bubblespeed))
let actionMoveDone = SKAction.removeFromParent()
bubble1.runAction(SKAction.sequence([moveAction, actionMoveDone]))
}
I am really trying to make this work. Thanks in advance!

If you want to use the name property of the node, you should set it to something. The runtime cannot figure out the variable name as the name. So in bubblesInView function after
let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")
you should do
bubble1.name = "bubble1"

You just forgot to name the node :
//create the sprite
let bubble1 = SKSpriteNode(imageNamed: "bubble_green.png")
bubble1.name = "bubble1"

Related

Making a "Joy stick" swift

What I have been trying to do is create a "Joy stick" that moves a player around. Here is what I have so far:
import UIKit
import SpriteKit
import SceneKit
class GameViewController: UIViewController, SCNSceneRendererDelegate {
var isTracking = false
var firstTrackingLocation = CGPoint.zero
var trackingVelocity = CGPoint.zero
var trackingDistance : CGFloat = 0.0
var previousTime : NSTimeInterval = 0.0
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene(named: "art.scnassets/level.scn")!
let scnView = self.view as! SCNView
scnView.delegate = self
scnView.scene = scene
scnView.showsStatistics = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if isTracking == false {
for touch in touches {
isTracking = true
let location = touch.locationInView(self.view)
firstTrackingLocation = location
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
if isTracking {
trackingVelocity = touches.first!.locationInView(self.view)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
isTracking = false
trackingVelocity = CGPoint.zero
}
func renderer(renderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {
if isTracking == true {
let scnView = self.view as! SCNView
let character = scnView.scene!.rootNode.childNodeWithName("person", recursively: true)
let deltaTime = time - previousTime
let pointsPerSecond: CGFloat = 1.0 * CGFloat(deltaTime)
var xResult:CGFloat = 0.0
var yResult:CGFloat = 0.0
let point = firstTrackingLocation
let endPoint = trackingVelocity
let direction = CGPoint(x: endPoint.x - point.x, y: endPoint.y - point.y)
if direction.x > direction.y {
let movePerSecond = pointsPerSecond/direction.x
xResult = direction.x*movePerSecond
yResult = direction.y*movePerSecond
} else {
let movePerSecond = pointsPerSecond/direction.y
xResult = direction.x*movePerSecond
yResult = direction.y*movePerSecond
}
character!.position = SCNVector3(CGFloat(character!.position.x) + (xResult), CGFloat(character!.position.y), CGFloat(character!.position.z) + (yResult))
let camera = scnView.scene?.rootNode.childNodeWithName("camera", recursively: true)
camera?.position = SCNVector3(CGFloat(camera!.position.x) + (xResult), CGFloat(camera!.position.y), CGFloat(camera!.position.z) + (yResult))
}
previousTime = time
}
override func shouldAutorotate() -> Bool {
return true
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Landscape
}
}
Now this works except if you drag your finger to the other side of the phone the character moves 10 times faster then it would if you barely moved your finger. So what I would like to have, is a Joy stick that moves the character the same speed if you drag a little bit or to the other side of the screen. And I would also like if you changed direction at the other side of the screen that the character would move the other way. So, my guess is that there needs to be a lastPoint saved then when the touchesMoved gets called that somehow we calculate a direction from lastPoint to the currentPoint and then move the character in renderer. I understand that most of this code is probably rubbish, but thanks in advance.
Your joystick should be a value from 0 to 1, you need to determine the radius of your joystick, then calculate distance of (point touched to center of control) and the angle of the control with arc tan.
Now we need to ensure we never go past maxRadius, so if our distance is > maxRadius, we just set it to max radius, then we divide this value by our maxRadius to get out distance ratio.
Then we just take the cos and sin of our angle, and multiply it by our distance ratio, and get the x and y ratio values. (Should be between 0 and 1)
Finally, take this x and y value, and multiply it to the speed at which your object should be moving at.
let maxRadius = 100 //Your allowable radius
let xDist = (p2.x - p1.x)
let yDist = (p2.y - p1.y)
let distance = (sqrt((xDist * xDist) + (yDist * yDist))
let angle = atan2(yDist , xDist )
let controlDistanceRatio = (distance > maxRadius) ? 1 : distance / maxRadius
let controllerX = cos(angle) * controlDistanceRatio
let controllerY = sin(angle) * controlDistanceRatio
This seems like a good case to use a custom UIGestureRecognizer. See Apple API Reference.
In this particular case you would create a continuous gesture. The resultant CGVector would be calculated from the center (origin) point of your joystick on screen. The recognizer would fail if the joystick node isn't selected and end if unselected (deselected). The resultant CGVector will be updated while the gesture's state is moved.
Now the tricky part to figure out would be moving the node image in such a way that allows the user to have the feeling of a joystick. For this you may need to update the node texture and make slight adjustments to the node position to give the appearance of moving a node around.
See if this helps you: Single Rotation Gesture Recognizer
Let me know if this points you in the right direction.
Your stated problem is the character moves at a variable rate depending on the how far from the start point the user drags their finger. The crucial point in the code seems to be this
let direction = CGPoint(x: endPoint.x - point.x, y: endPoint.y - point.y)
The difference between endPoint and point is variable and so you are getting a variable magnitude in your direction. To simplify, you could just put in a constant value like
let direction = CGPoint(x: 10, y: 10)
That gets the character moving at a constant speed when the user presses the joystick, but the character is always moving the same direction.
So somehow you've got to bracket in the variable directional values. The first thing that comes to mind is using min and max
let direction = CGPoint(x: endPoint.x - point.x, y: endPoint.y - point.y)
direction.x = min(direction.x, 10)
direction.x = max(direction.x, -10)
direction.y = min(direction.y, 10)
direction.y = max(direction.y, -10)
That seems like it would keep the magnitude of the direction values between -10 and 10. That doesn't make the speed completely constant, and it allows faster travel along diagonal lines than travel parallel to the x or y axis, but maybe it is closer to what you want.

Access Constant from outside Function Swift

I have a game that runs like this: The class file is run and uses the function "addObject()" to an add an object to the screen. The object then falls from the top of the screen to the bottom and the player has to try to catch the object by tapping on it. However, the object is declared (as an SKSpriteNode)in the addObject function. So when I go to add the "touch controls" function to the game, I can't reference the object. What can I do to fix this? Here is some of my code:
class GameSceneTest: SKScene, SKPhysicsContactDelegate {
var ObjectDestroyed = 0
let label = SKLabelNode(fontNamed: "Title 1")
var money: Int = 0
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.clearColor()
physicsWorld.gravity = CGVectorMake(0, 0)
physicsWorld.contactDelegate = self
//Change duration
runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(addObject), SKAction.waitForDuration(1)])
))
}
func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(min min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}
func addObject() {
let Object = SKSpriteNode(imageNamed: "Object\(arc4random_uniform(6) + 1).png")
Object.size = CGSize(width: 50, height: 50)
Object.physicsBody = SKPhysicsBody(rectangleOfSize: Object.size)
Object.physicsBody?.dynamic = true
//Determine where to spawn Object across y axis
let actually = random(min: Object.size.width/2, max: size.width - Object.size.width/2)
//Position Object slightly off screen along right edge
//and along random y axis point
//Object.position = CGPoint(x: size.width + Object.size.width/2 , y: actually)
Object.position = CGPoint(x: actually, y: size.height + Object.size.height/2)
//Add the Object to the scene
addChild(Object)
//Determines speed of Object (edit later to where speed depends on type of Object)
let actualDuration = random(min: CGFloat(4), max: CGFloat(5))
//Create the Actions
let actionMove = SKAction.moveTo(CGPoint(x: actually, y: gem.size.height/2), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
let loseAction = SKAction.runBlock() {
let reveal = SKTransition.flipHorizontalWithDuration(0.5) //Change to flipe vertical
let gameOverScene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(gameOverScene, transition: reveal)
}
Object.runAction(SKAction.sequence([actionMove, loseAction, actionMoveDone]))
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
if Object.containsPoint(touch.locationInNode(self)) {
Object.removeFromParent()
print("touched")
}
}
}
There are several different ways that you could correctly implement this. One option would be to make an class level variable that is an array of type SKSpriteNode and append the objects to it when you create them in that function.
// Declare an array variable at the class level
var objectArray = [SKSpriteNode]()
// Then in the function after you create the object and configure it
objectArray.append(object)
Another alternative would be to use a dictionary that you add the new objects to. It just depends on exactly what you need to do with the objects when you reference them again.
Note: This answer assumes that you will have multiple objects that you need reference to at any given time. If you only need reference to one at a time, you can just make a class level variable of type SKSpriteNode and set it in your addObject function
For Example:
// Declaring class level variable
var myNode: SKSpriteNode?
When your addObject method is called, set myNode equal to the object that you create and configure
// When you go to use myNode, just unwrap it to make sure that it has a value
if let unwrappedNode = self.myNode {
// Do something with unwrappedNode
}

Embed icon beside a SKLabelNode

I was wondering if there's any way to set an icon besides a SKLabelNode (since I need to use SKAction to move this label up) like this:
All I found about it was using UILabel (here) or a GitHub project (here), where I can't move or bounce (with SpriteKit-Spring) my label.
I was thinking in create a sprite node with the icon image and set it's position besides the coinsLabel, but since this label is used as a coin counter, it would get larger when increased; and the icon would be overlaid.
I made this example project below to make it easier to visualize (it doesn't have the icon, of course. It's only incrementing and moving coinsLabel by buttons).
If you want, you can download it here.
import SpriteKit
class GameScene: SKScene {
//Declaration
var icon = SKSpriteNode()
var coins = Int()
var coinsLabel = SKLabelNode()
var incrementButton = SKSpriteNode()
//Setup
func setupIcon(){
//icon
icon = SKSpriteNode(imageNamed: "icon")
icon.position = CGPoint(x: self.frame.width / 1.45, y: self.frame.height / 1.075)
icon.setScale(0.1)
}
func setupCoinsLabel(){
//coinsLabel
coinsLabel.position = CGPoint(x: self.frame.width / 150 - 300, y: 0)
coinsLabel.setScale(12.5)
coinsLabel.text = "0"
}
func setupIncrementButton(){
//incrementButton
incrementButton = SKSpriteNode(imageNamed: "incrementButton")
incrementButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 3.15)
incrementButton.setScale(2.0)
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
setupIcon()
addChild(icon)
setupCoinsLabel()
icon.addChild(coinsLabel)
setupIncrementButton()
addChild(incrementButton)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
//When touch buttons/screen
for touch in touches{
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
//Increment
if node == incrementButton{
coins += 1
coinsLabel.text = NSString(format: "%i", coins) as String
coinsLabel.position = CGPoint(x: self.frame.width / 150 - coinsLabel.frame.width, y: 0)
}
}
}
}
Just make a SKSpriteNode and add it as a child to the SKLabelNode, you can always set the SKSpriteNode's position to be to the right of the SKLabel regardless of how many digits are in your label, so overlapping would never happen
//Increment
if node == incrementButton{
coins += 1
coinsLabel.text = NSString(format: "%i", coins) as String
icon.position = CGPoint(x: coinsLabel.frame.width / 2, y: 0)
}

Detect the degrees in a circle with users touch

Im looking to create a swift function to return the degrees (Float) of the users touch on an image (SKSpritenode). In touchesBegan, I know how to detect the x & y positions of my image. The idea is to create a function that takes in these positions and returns the degrees.
Amended - The following code now works:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.anchorPoint = CGPointMake(0.5, 0.5)
myNode.position = CGPointMake(0, -myNode.frame.height / 2)
self.addChild(myNode)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
if myNode.containsPoint(location) {
print("tapped!")
let origin = myNode.position
let touch = touch.locationInNode(myNode.parent!)
let diffX = touch.x - origin.x
let diffY = touch.y - origin.y
let radians = atan2(diffY, diffX)
let degrees = radians * CGFloat(180 / M_PI)
print("degrees = \(degrees)")
}
}
}
You need to compare the user's touch position to an origin point, which might be the centre of your sprite node for example. Here's some code to get you started:
let origin = CGPoint(x: 0, y: 0)
let touch = CGPoint(x: 100, y: 100)
let diffX = touch.x - origin.x
let diffY = touch.y - origin.y
let radians = atan2(diffY, diffX)
let degrees = radians * CGFloat(180 / M_PI)
That last value – degrees – is the one you want if you want to show users information. If you want to do more calculations, you should probably stick with radians.

How to rotate sprite in sprite kit with swift

Hi my problem is regarding the rotation of two sprites. When I touch the right half of the screen the rotation starts and moving sprite2 and sprite3. If I touch the left half of the screen the rotation stops because velocity-velocity = 0. If I touch the left half again the rotation begins.
However, if I touch the half of the screen corresponding with the current rotational-direction the velocity is duplicated. I want to be able to change the direction of the rotation, but for the speed to remain constant.
Video demonstrating the problem: http://youtu.be/HxLwl1QZiNM
import SpriteKit
class GameScene: SKScene {
let sprite = SKSpriteNode(imageNamed:"bWhite")
let sprite2 = SKSpriteNode(imageNamed:"bBlue")
let sprite3 = SKSpriteNode(imageNamed:"bRed")
let circle = SKSpriteNode(imageNamed:"bCircle")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backColor = SKColor(red: 0, green: 0, blue: 0, alpha: 1)
self.backgroundColor = backColor
sprite.setScale(1.25)
sprite2.setScale(1)
sprite3.setScale(1)
sprite.position = CGPointMake(self.frame.size.width/2, (self.frame.size.height/2)-200);
circle.position = CGPointMake(self.frame.size.width/2, (self.frame.size.height/2)-200);
sprite3.zRotation = 172.77
sprite2.anchorPoint = CGPointMake(-1.10, 0.5);
sprite3.anchorPoint = CGPointMake(-1.10, 0.5);
self.addChild(sprite)
self.addChild(circle)
sprite.addChild(sprite2)
sprite.addChild(sprite3)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
let action2 = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)
if (location.x > self.frame.size.width / 2)
{
sprite2.runAction(SKAction.repeatActionForever(action2))
sprite3.runAction(SKAction.repeatActionForever(action2))
} else {
sprite2.runAction(SKAction.repeatActionForever(action))
sprite3.runAction(SKAction.repeatActionForever(action))
}
}
}
OK, take three. I'm not 100% sure about the specifics of when the rotation should end etc. but all the pieces should be in place with the below code. It will:
start rotation clockwise or counterclockwise based on the position of the first touch
stop the rotation if the user touches for the same direction again
switch the rotation if the user touches on the other half of the screen
import SpriteKit
enum rotationDirection{
case clockwise
case counterClockwise
case none
}
class GameScene: SKScene {
var currentRotationDirection = rotationDirection.none
let sprite = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(100, 100))
override func didMoveToView(view: SKView) {
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.physicsBody.affectedByGravity = false
sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(sprite)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch : UITouch = touches.anyObject() as UITouch
let touchPosition = touch.locationInNode(self)
let newRotationDirection : rotationDirection = touchPosition.x < CGRectGetMidX(self.frame) ? .clockwise : .counterClockwise
if currentRotationDirection != newRotationDirection && currentRotationDirection != .none{
reverseRotation()
currentRotationDirection = newRotationDirection
} else if currentRotationDirection == newRotationDirection{
stopRotation()
currentRotationDirection = .none
} else if (currentRotationDirection == .none){
setupRotationWith(direction: newRotationDirection)
currentRotationDirection = newRotationDirection
}
}
func reverseRotation(){
let oldRotateAction = sprite.actionForKey("rotate")
let newRotateAction = SKAction.reversedAction(oldRotateAction)
sprite.runAction(newRotateAction(), withKey: "rotate")
}
func stopRotation(){
sprite.removeActionForKey("rotate")
}
func setupRotationWith(#direction: rotationDirection){
let angle : Float = (direction == .clockwise) ? Float(M_PI) : -Float(M_PI)
let rotate = SKAction.rotateByAngle(angle, duration: 1)
let repeatAction = SKAction.repeatActionForever(rotate)
sprite.runAction(repeatAction, withKey: "rotate")
}
}
Edit: Changed example to cater the specific needs in the question. Something odd with the code formatting not quite sure what's going on there.
I suggest removing all current actions before adding new, since they may conflict. I'm still not sure what you are trying to achieve, can you explain more?
Have you just tired removing the actions at touchesEnded? I had a similar problem before where I wanted my object to move left to right based on which half of the screen the user touched. I had a similar issue where if I touched right and right again my velocity would double, and if I touched left my object would just stop.
I fixed this issue by adding the touchesEnded method and just removed the actions whenever I let go. This essentially reset my move action so the object would stop when I let go and would go to the opposite direction right away instead of having to tap it twice.

Resources