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

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

ボタンに応答する@コードを学ぼう3

`iterate` って「反復」「繰り返し処理する」って意味なんだね。`Em!` は Wikipedia によると「英語の3人称複数代名詞「them」の短縮形「'em」」のようです。

it・er・ate | ɪ́tərèɪt |
動詞
他動詞
1 ⦅かたく⦆…を反復する, 繰り返す.
2 〈コンピュータの命令〉を繰り返し処理する.
ìt・er・á・tion
名詞

英語の3人称複数代名詞「them」の短縮形「'em」

// ボタンに応答する@コードを学ぼう3
let blackHole = Graphic(image: #imageLiteral(resourceName: "BlackHole@2x.png"))
let x = randomDouble(from: -400, to: 400)
let y = randomDouble(from: -400, to: 400)
let blackHolePosition = Point(x: x, y: y)
scene.place(blackHole, at: blackHolePosition)

// Array of graphics on the scene.
var graphics: [Graphic] = [Graphic]()

// Fade out graphic, the remove it.
func squishGraphic(graphic: Graphic) {
    graphic.scale -= 0.5
    graphic.alpha -= 0.25
    
    // Remove graphic when scale gets small.
    if graphic.scale < 0.6 {
        graphic.moveAndZap(to: blackHole.position)
    }
}

// Squish all graphics.
func squishEm() {
    // Iterate over graphics and squish each one.
    for graphic in graphics {
        squishGraphic(graphic: graphic)
    }
}


// UFF tool event handler.
func addFructoid(touch: Touch) {
    if touch.previousPlaceDistance < 60 { return }
    let fruit = "🍏🍐🍊🍋🍉🍒🍓🍌".componentsByCharacter()
    let graphic = Graphic(text: fruit.randomItem)
    scene.place(graphic, at: touch.position)
    graphics.append(graphic)
    graphic.scale = 2.0
    
    let x = randomDouble(from: 50, to: 400)
    let y = randomDouble(from: 50, to: 400)
    let period = randomDouble(from: 8.0, to: 15.0)
    graphic.orbit(x: x, y: y, period: period)
}

// Create and add UFF tool.
let fructoidTool = Tool(name: "UFF", emojiIcon: "🍋")
fructoidTool.onFingerMoved = addFructoid(touch:)
scene.tools.append(fructoidTool)

// Create and add Squish tool.
let squishTool = Tool(name: "Squish", emojiIcon: "💥")
squishTool.onGraphicTouched = squishGraphic(graphic:)
scene.tools.append(squishTool)

// Create and add Squish ’Em! button.
let squishButton = Button(name: "Squish ’Em!")
squishButton.onTap = squishEm
scene.button = squishButton