摇动手势后从SKScene中选择随机元素

问题描述

我有一个非常基本的应用,该应用会在屏幕圆圈上的任意位置显示(我将其命名为气泡),并且当我旋转设备时气泡会移动。

我正在尝试实现一种方法,当我摇晃设备时会触发该方法,该方法可使每个气泡从场景中消失,并选择一个随机气泡并将其放置在屏幕中央(一种彩票)基本上)。 直到我触摸它,该气泡才会移动。当我触摸它时,屏幕上的所有气泡都随着设备旋转而移动,一切恢复正常。

我的问题:如何使每个节点消失,并选择一个随机节点以显示它并将其居中显示在屏幕上。

这是我的代码:

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
        }
    }

}


我认为我需要使用SKAction,但我不知道如何选择随机节点,并使所有其他气泡消失。

谢谢

解决方法

不需要枚举所有节点。

如果创建气泡容器节点,则可以将所有气泡分配给该容器。

然后从子对象中随机选择一个气泡,然后通过removeAllChildren跟随它。

import CoreMotion
import SpriteKit


class Bubble: SKShapeNode{}
class BName: SKLabelNode {}

class GameScene: SKScene {
    var motionManager: CMMotionManager?
    let bubbles = ["bubble1","bubble2","bubble3"]
    let bubbleContainer = SKNode()
    override func didMove(to view: SKView) {
        addChild(bubbleContainer)
        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)
            bubbleContainer.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
        }
    }

    func popBubbles(){
        let random = *** // use random method of your choice,limit it to children count
        let selectedBubble =  bubbleContainer.children[random]
        bubbleContainer.removeAllChildren()
        bubbleContainer.addChild(selectedBubgle)
        //perform actons on selectedBubble here
        ***
        //add remaining bubbles here
        ***
    }

}
,

使用enumerateChildNodesWithName:usingBlock:(https://developer.apple.com/documentation/spritekit/sknode/1483024-enumeratechildnodeswithname?language=objc)遍历所有气泡。将气泡添加到数组中,获取气泡数量,随机选择一个气泡,然后(使用removeFromParent)删除所有其他气泡。

将剩余的一个气泡放在屏幕中心。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...