UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Select random element from SKScene after shake gesture

Forums > Swift

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

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.