I have a very basic app, that displays at random positions on the screen circles (that I named bubbles) and those bubbles moves when I rotate my device.
I'm trying to implement a method which is triggered when I shake the device, and that makes every bubbles disappears from the scene and choose a random bubble and place it at the center of the screen (a kind of lottery of bubbles basically).
That same bubble should not move until I touch it.When I do touch it, everything goes back to normal with all of my bubbles on my screen moving with my device's rotation.
My question : how I can make disappear every node and choose a random node to display it and center it on the screen.
This is my code :
import CoreMotion
import SpriteKit
class Bubble: SKShapeNode{}
class BName: SKLabelNode {}
class GameScene: SKScene {
var motionManager: CMMotionManager?
let bubbles = ["bubble1", "bubble2", "bubble3"]
override func didMove(to view: SKView) {
for bubble in bubbles {
let ball = Bubble(circleOfRadius: 50)
let ball_name = SKLabelNode(text: bubble)
ball_name.position = CGPoint(x: ball.frame.midX, y: ball.frame.midY)
ball.position = CGPoint(x: (CGFloat(arc4random_uniform(UInt32(frame.width)))) , y: (CGFloat(arc4random_uniform(UInt32(frame.height)))))
ball.physicsBody = SKPhysicsBody(circleOfRadius: 50)
ball.fillColor = UIColor.blue
ball.physicsBody?.restitution = 1
ball.physicsBody?.mass = 1
ball.physicsBody?.allowsRotation = false
ball.addChild(ball_name)
addChild(ball)
}
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
motionManager = CMMotionManager()
motionManager?.startAccelerometerUpdates()
}
override func update(_ currentTime: TimeInterval) {
if let accelerometerData = motionManager?.accelerometerData {
physicsWorld.gravity = CGVector(dx: accelerometerData.acceleration.y * -66, dy: accelerometerData.acceleration.x * 66)
}
}
override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
UIView.animate(withDuration: 0.4) {
// call a function that take a random bubble and
// display it at the center of the screen, while the
// other ones disappears until I touch the bubble
// and then all of the others re-appears including that random one
// with every bubbles floating on the screen ie. what we had before
// the shake
}
}
}
I think I need to use SKAction but I do not know how to choose the random node, and make all of the other bubbles disappears.
Thank you