• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Spritekit

My WordPress site

  • Top
  • SpriteKitBase
    • Collision
    • Enumerated
    • Shooting
    • Breakout
    • Physics1
      • physics4
      • physics3
      • Physics2
  • SwiftUI&SpriteKit
    • TabScroll
    • LocalWebView
      • Counter
      • StateObject
      • Button
      • SceneMove
    • Swift Learning
  • Photo Gallery
    • Photo Galler 2
    • Photo Gallery 3
    • リンク
      • プロフィール
      • p5.play test

Button

04/10/2022 by kyougif


GameScene
import SpriteKit
import GameplayKit

class GameScene: SKScene {
  let label = SKLabelNode(fontNamed: "HelveticaNeue-Light")
  let button = SKSpriteNode(imageNamed: "blue2.png")

    override func didMove(to view: SKView) {
      physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
      self.backgroundColor = .green

       label.text = "Hello World!"
       label.position = CGPoint(x: self.frame.midX, y: self.frame.midY + 10)
       label.fontSize = 20
       label.fontColor = SKColor.white
      self.addChild(label)

       button.position = CGPoint(x: size.width/2, y: size.height/2+120)
       button.zPosition = 1
       button.name = "button"
       button.setScale(0.4)
       self.addChild(button)
        button.run(SKAction.scale(to: 0.5, duration: 0.3))
       button.alpha = 1

    }
    override func touchesEnded(_ touches: Set, with event: UIEvent?) {
        button.alpha = 1
        button.setScale(0.5)
    }

    override func touchesBegan(_ touches: Set, with event: UIEvent?){
        for touch in touches {
          let location = touch.location(in: self)
          let touchedNode = atPoint(location)

          if touchedNode.name == "button" {
              button.alpha = 0.5
              button.setScale(0.6)
              if label.fontSize < 50 {
              label.fontSize += 2
              }
              else{
              label.fontSize = 20
             }
         }
         
      }
    }
}
//////////////////////////
ContentView.swift
import SwiftUI
import SpriteKit

struct ContentView: View {
    let screenWidth  = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var scene: SKScene {
        let scene = GameScene()
        scene.size = CGSize(width: screenWidth, height: screenHeight)
        scene.scaleMode = .fill
        return scene
    }
    var body: some View {
        SpriteView(scene: scene)
          .frame(width: screenWidth, height: screenHeight)
          .ignoresSafeArea()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

//////////////////////////
ButtonApp.swift

import SwiftUI
@main
struct ButtonApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Filed Under: Programming, spritekit

Primary Sidebar

最近の投稿

  • ObservableObj
  • SideMenu
  • TabScroll
  • Text
  • TappedNodes
  • Collision2
  • CoreMotion
  • Collision
  • Timer
  • Enumerated
  • Random
  • SwiftUI-List
  • Shooting
  • Sound
  • Camera
  • SpriteSheet
  • SKPhysics42
  • physics4
  • physics3
  • Physics2

アーカイブ

  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2014年9月
  • 2014年5月

カテゴリー

  • Programming
  • spritekit
  • swiftui

固定ページ

  • Home
  • p5.play test
  • Photo Galler 2
  • Photo Gallery
  • Photo Gallery 3
  • Top
  • プロフィール
  • リンク

Copyright © 2026 · Genesis-child on Genesis Framework · WordPress · Log in