大宮盆栽デイズ - Omiya Bonsai Days -

冗談めかす埼玉のファインマン

音を鳴らす@コードを学ぼう3

// 音を鳴らす@コードを学ぼう3
let blu = Graphic(image: #imageLiteral(resourceName: "Blu1@2x.png"))
let theOrigin = Point(x: 0, y: 0)
scene.place(blu, at: theOrigin)

// Event handler for Sound tool.
func soundGraphic(graphic: Graphic) {
    
    // Play a sound for Blu.
    if graphic == blu {
        playSound(.bluDance, volume: 75)
    }
    
    // Play a different sound for each type of fruit.
    if graphic.text == "🍐" {
        playSound(.electricity)
    } else if graphic.text == "🍊" {
        playSound(.phone)
    } else if graphic.text == "🍋" {
        playSound(.knock)
    }
}

// Event handler for Fruit tool.
func addFruit(touch: Touch) {
    if touch.previousPlaceDistance < 60 { return }
    let fruit = "🍐🍊🍋".componentsByCharacter()
    let graphic = Graphic(text: fruit.randomItem)
    scene.place(graphic, at: touch.position)
    graphic.scale = 2.0
}

// Create and add Fruit tool.
let fruitTool = Tool(name: "Fruit", emojiIcon: "🍊")
fruitTool.onFingerMoved = addFruit(touch:)
scene.tools.append(fruitTool)

// Create and add Sound tool.
let soundTool = Tool(name: "Sound", emojiIcon: "📣")
soundTool.onGraphicTouched = soundGraphic(graphic:)
scene.tools.append(soundTool)