【问题标题】:Horizontal spinning wheel with Spritekit带有 Spritekit 的水平纺车
【发布时间】:2021-02-12 18:13:36
【问题描述】:

我愿意用SpriteKit创建一个水平旋转的轮子作为我的主角手机主页。 像这样,为了更好地解释我:水平旋转时滚动元素的轮子。 What I'm looking for

我已经创建了一个名为 Box 的类来创建图标并委托他们的幻灯片,我在此处附上了两个代码,以便尽可能清晰并帮助您完成我所做的事情。 这是我的 Box 类代码:

    import SpriteKit
    import GameplayKit
    
    
    protocol BoxDelegate: NSObjectProtocol {
        func boxSwiped(to: String)
    }

    class Box: SKSpriteNode {

    weak var boxDelegate: BoxDelegate!
    private var moveAmtX: CGFloat = 0
    private var moveAmtY: CGFloat = 0
    private let minimum_detect_distance: CGFloat = 50
    private var initialPosition: CGPoint = CGPoint.zero
    private var initialTouch: CGPoint = CGPoint.zero
    private var resettingSlider = false
    var rest: CGFloat = 0
    var clickable: Bool = false

    override init(texture: SKTexture?, color: UIColor, size: CGSize) {

        super.init(texture: texture, color: color, size: size)
        self.clickable = false

        self.isUserInteractionEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func moveWithCamera() {
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first as UITouch? {

            initialTouch = touch.location(in: self.scene!.view)
            moveAmtY = 0
            moveAmtX = 0
            initialPosition = self.position
            print(initialPosition)
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first as UITouch? {

            let movingPoint: CGPoint = touch.location(in: self.scene!.view)

            moveAmtX = movingPoint.x - initialTouch.x
            moveAmtY = movingPoint.y - initialTouch.y
            print(moveAmtX, moveAmtY)
            self.position.x = initialPosition.x + moveAmtX
            let minus = UIScreen.main.bounds.width/2 + ((UIScreen.main.bounds.width/2) - self.position.x)
            if (UIScreen.main.bounds.width/2)/self.position.x < 1 {
                rest = (UIScreen.main.bounds.width/2)/self.position.x
            } else {
                let theFloat = (UIScreen.main.bounds.width/2)/self.position.x
                rest = (UIScreen.main.bounds.width/2)/minus
            }
            self.xScale = rest
            self.yScale = rest
            
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

        var direction = ""
        if fabs(moveAmtX) > minimum_detect_distance {

            //must be moving side to side
            if moveAmtX < 0 {
                direction = "left"
            }
            else {
                direction = "right"
            }
        }
        else if fabs(moveAmtY) > minimum_detect_distance {

            //must be moving up and down
            if moveAmtY < 0 {
                direction = "up"
            }
            else {
                direction = "down"
            }
        }

        print("object \(self.name!) swiped " + direction)

        self.boxDelegate.boxSwiped(to: direction)
    }
}

这是我的 GameScene 课程:

import SpriteKit
import GameplayKit

class CellScene: SKScene, BoxDelegate {

    var box = Box()
    var box2 = Box()
    var box3 = Box()
    var Boxes: [Box] = []
    var Slots: [CGPoint] = []
    var swiped = ""

    override func sceneDidLoad(){
        createBoxes()
        createSlot()
    }

    func boxSwiped(to: String) {
        swiped = to
    }

    override func update(_ currentTime: TimeInterval) {
        checkBox()
    }
    
    func createSlot(){
        Slots[0] = CGPoint(x: UIScreen.main.bounds.width/2 , y: UIScreen.main.bounds.height/2)
        Slots[1] = CGPoint(x: UIScreen.main.bounds.width/5, y: UIScreen.main.bounds.height/2)
        Slots[2] = CGPoint(x: UIScreen.main.bounds.width  - UIScreen.main.bounds.width/5, y: UIScreen.main.bounds.height/2)
    }
    
    func createBoxes(){
        box = Box(color: .white, size: CGSize(width: 200, height: 200))
        box.zPosition = 1
        box.position = CGPoint(x: UIScreen.main.bounds.width/2 , y: UIScreen.main.bounds.height/2)
        box.name = "1"
        box.boxDelegate = self
        addChild(box)

        box2 = Box(color: .blue, size: CGSize(width: 200, height: 200))
        box2.zPosition = 1
        box2.name = "2"
        box2.position = CGPoint(x: UIScreen.main.bounds.width/5, y: UIScreen.main.bounds.height/2)
        box2.boxDelegate = self
        addChild(box2)

        box3 = Box(color: .red, size: CGSize(width: 200, height: 200))
        box3.zPosition = 1
        box3.name = "3"
        box3.position = CGPoint(x: UIScreen.main.bounds.width  - UIScreen.main.bounds.width/5, y: UIScreen.main.bounds.height/2)
        box3.boxDelegate = self
        addChild(box3)
        
        Boxes = [box, box2, box3]
    }
    
    func checkBox(){
        for i in 1...3 {
            let node = childNode(withName: String(i))
            
            var rest: CGFloat = 0
            let minus = UIScreen.main.bounds.width/2 + ((UIScreen.main.bounds.width/2) - node!.position.x)
            
            if (UIScreen.main.bounds.width/2)/node!.position.x < 1 {
                rest = (UIScreen.main.bounds.width/2)/node!.position.x
            } else {
                rest = (UIScreen.main.bounds.width/2)/minus
            }
            node!.xScale = rest
            node!.yScale = rest
            
            if Boxes[i].position == Slots[0] {
                Boxes[i].clickable = true
            }
        }
    }
}

感谢任何可以帮助我的人,我仍处于 SpriteKit 的起步阶段,我想知道优化我的代码的最佳方法 :)

【问题讨论】:

    标签: ios swift xcode sprite-kit skspritenode


    【解决方案1】:

    为什么不使用带有三个独立面板的 SpriteKit 动画,您可以在其中将新联系人作为图像(精灵)添加到列表的同一位置的集合中。然后为每个面板滚动一个偏移量。每次接触三个图像,一个左角,一个中心,一个右角。

    【讨论】:

      猜你喜欢
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      相关资源
      最近更新 更多