ios - JoyStick 仅在 Camera Controls = true 时移动 Xcode、SWIFT、SpriteKit、SceneKit

标签 ios xcode sprite-kit scenekit joystick

嘿,我有一个 3D 游戏,但它还不是真正的游戏。但它有一个 SceneKit 3D 场景和一个用于 HUD/控件的 overlayskscene。 “基地”是操纵杆的基地,球是 handle 问题是操纵杆根本不会移动,除非 scnView.allowsCameraControl = true。我觉得这很奇怪。这是我的整个 View Controller 代码,所以没有遗漏任何内容,如果您愿意,可以直接将其复制并粘贴到 Xcode 中以查看我在说什么。有什么帮助吗? 代码:

   import iAd
   import UIKit
   import GameKit
   import SceneKit
   import StoreKit
   import SpriteKit
   import QuartzCore
   import Foundation
   import AVFoundation
   import AudioToolbox

 class GameViewController: UIViewController, ADBannerViewDelegate, SKPhysicsContactDelegate, SKSceneDelegate, SCNSceneRendererDelegate, SCNPhysicsContactDelegate{

var stickActive:Bool = false

let base = SKSpriteNode(imageNamed:"VirtualJoystickBase")
let ball = SKSpriteNode(imageNamed:"VirtualJoyStickHandle")
let ship = SKSpriteNode(imageNamed:"Ship")

var ButtonA = SKSpriteNode(imageNamed:"GreenAButton")
var ButtonO = SKSpriteNode(imageNamed:"CircleButton")
var ButtonY = SKSpriteNode(imageNamed:"YellowYButton")
var ButtonSquare = SKSpriteNode(imageNamed:"BlueSquareButton")

let FieldScene = SCNScene(named: "art.scnassets/TesingCampusField.dae")!

let GuyScene = SCNScene(named: "art.scnassets/Guy.dae")!

let overlayScene = SKScene(size: CGSizeMake(100, 100))
override func viewDidLoad() {
    super.viewDidLoad()

    let scnView = self.view as! SCNView
    scnView.overlaySKScene = overlayScene
    scnView.backgroundColor = UIColor.whiteColor()
    scnView.scene = FieldScene
    scnView.delegate = self
    scnView.overlaySKScene!.delegate = self
    scnView.overlaySKScene!.anchorPoint = CGPointMake(0, 0)
    scnView.overlaySKScene!.physicsWorld.contactDelegate = self
    scnView.overlaySKScene!.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
    scnView.allowsCameraControl = true
    scnView.showsStatistics = false

    let Guy1: SCNNode = GuyScene.rootNode.childNodeWithName("Bob_014", recursively: true)!
    FieldScene.rootNode.addChildNode(Guy1)

    //----Positioning-the-Base-of-the-Joystick-----------
    base.size = CGSize(width: 14, height: 24)
    base.position = CGPointMake(15, 19)
    base.zPosition = 0
    overlayScene.addChild(base)
    //----Positing-the-Ball/Joystick-----------
    ball.size = CGSize(width: 10, height: 17)
    ball.position = base.position
    ball.zPosition = 1
    overlayScene.addChild(ball)
    //----A-Button--Creation -------------------
    ButtonA.size = CGSize(width: 6, height: 9)
    ButtonA.anchorPoint = CGPointMake(-13.3, -0.5)
    ButtonA.zPosition = 0
    overlayScene.addChild(ButtonA)
    //----B-Button--Creation -------------------
    ButtonO.size = CGSize(width: 6, height: 9)
    ButtonO.anchorPoint = CGPointMake(-14.4, -1.7)
    ButtonO.zPosition = 0
    overlayScene.addChild(ButtonO)
    //----C-Button--Creation -------------------
    ButtonSquare.size = CGSize(width: 6, height: 9)
    ButtonSquare.anchorPoint = CGPointMake(-12.2, -1.7)
    ButtonSquare.zPosition = 0
    overlayScene.addChild(ButtonSquare)
    //----C-Button--Creation -------------------
    ButtonY.size = CGSize(width: 6, height: 9)
    ButtonY.anchorPoint = CGPointMake(-13.3, -2.7)
    ButtonY.zPosition = 0
    overlayScene.addChild(ButtonY)


    //---Setting-Up-Ships-Position/PhysicsBody---------------------
    ship.position = CGPointMake(0, 100)
    ship.size = CGSize(width: 80, height: 80)
    ship.physicsBody?.dynamic = true
    ship.physicsBody?.allowsRotation = true
    ship.physicsBody?.affectedByGravity = true
    ship.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ship"), size: ship.size)
    ship.physicsBody!.friction = 0
    ship.physicsBody!.restitution = 0
    ship.physicsBody!.linearDamping = 0
    ship.physicsBody!.angularDamping = 0


    //--------------------------
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    GuyScene.rootNode.addChildNode(cameraNode)
    cameraNode.position = SCNVector3(x: 0, y: 5, z: 15)
    //-----------------------------------------------
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = SCNLightTypeOmni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    FieldScene.rootNode.addChildNode(lightNode)
    //-----------------------------------------------
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = UIColor.darkGrayColor()
    FieldScene.rootNode.addChildNode(ambientLightNode)
    //----------------------------------------------
}

func YButtonPressed() {
    let YButtonPressed = SKTexture(imageNamed: "YellowYButtonPressed")
    let OrignalButtonY = SKTexture(imageNamed:"YellowYButton")
    let YButtonPressedAnimation = SKAction.animateWithTextures([YButtonPressed, OrignalButtonY], timePerFrame: 0.2)
    let RunYButtonPressedAnimation = SKAction.repeatAction(YButtonPressedAnimation, count: 1)
    ButtonY.runAction(RunYButtonPressedAnimation)

}
func OButtonPressed() {
    let OButtonPressed = SKTexture(imageNamed: "CircleButtonPressed")
    let OrignalButtonO = SKTexture(imageNamed:"CircleButton")
    let OButtonPressedAnimation = SKAction.animateWithTextures([OButtonPressed, OrignalButtonO], timePerFrame: 0.2)
    let RunOButtonPressedAnimation = SKAction.repeatAction(OButtonPressedAnimation, count: 1)
    ButtonO.runAction(RunOButtonPressedAnimation)

}
func SquareButtonPressed() {
    let SquareButtonPressed = SKTexture(imageNamed: "BlueSquareButtonPressed")
    let OrignalButtonSquare = SKTexture(imageNamed:"BlueSquareButton")
    let SquareButtonPressedAnimation = SKAction.animateWithTextures([SquareButtonPressed, OrignalButtonSquare], timePerFrame: 0.2)
    let RunSquareButtonPressedAnimation = SKAction.repeatAction(SquareButtonPressedAnimation, count: 1)
    ButtonSquare.runAction(RunSquareButtonPressedAnimation)

}
func AButtonPressed() {
    let AButtonPressed = SKTexture(imageNamed: "GreenAButtonPressed")
    let OrignalButtonA = SKTexture(imageNamed:"GreenAButton")
    let AButtonPressedAnimation = SKAction.animateWithTextures([AButtonPressed, OrignalButtonA], timePerFrame: 0.2)
    let RunAButtonPressedAnimation = SKAction.repeatAction(AButtonPressedAnimation, count: 1)
    ButtonA.runAction(RunAButtonPressedAnimation)

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */
    for touch: AnyObject in touches  {
        let location = touch.locationInNode(self.overlayScene)
        if (CGRectContainsPoint(base.frame, location)) {
            print("stickActive = true")
            stickActive = true
        } else {
            print("stickActive = false")
            stickActive = false
        }
    }
    for touch: AnyObject in touches {
        let location1 = touch.locationInNode(self.overlayScene)
        if self.overlayScene.nodeAtPoint(location1) == self.ButtonA {
            AButtonPressed()
            print("AButtonPressed")
        }
    }
    for touch: AnyObject in touches {
        let location2 = touch.locationInNode(self.overlayScene)
        if self.overlayScene.nodeAtPoint(location2) == self.ButtonO {
            OButtonPressed()
            print("OButtonPressed")
        }
    }
    for touch: AnyObject in touches {
        let location3 = touch.locationInNode(self.overlayScene)
        if self.overlayScene.nodeAtPoint(location3) == self.ButtonY {
            YButtonPressed()
            print("YButtonPressed")
        }
    }
    for touch: AnyObject in touches {
        let location4 = touch.locationInNode(self.overlayScene)
        if self.overlayScene.nodeAtPoint(location4) == self.ButtonSquare {
            SquareButtonPressed()
            print("SquarButtonPressed")
        }
    }

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

    for touch: AnyObject in touches  {
        let location = touch.locationInNode(self.overlayScene)
        if self.overlayScene.nodeAtPoint(location) == self.ball {

        if (stickActive == true) {
            let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
            let angle = atan2(v.dy, v.dx)
            //println( deg + 180)
            let length:CGFloat = base.frame.size.height / 2
            let xDist:CGFloat = sin(angle - 1.57879633) * length
            let yDist:CGFloat = cos(angle - 1.57879633) * length

            if (CGRectContainsPoint(base.frame, location)) {
                ball.position = location

            } else {
                ball.position = CGPointMake( base.position.x - xDist, base.position.y + yDist)
            }

            ship.zRotation = angle - 1.57879633
            let calcRotation : Float = Float(angle - 1.57879633) + Float(M_PI_2);
            let intensity : CGFloat = 200.0 // put your value
            let xVelocity = intensity * CGFloat(cosf(calcRotation))
            let yVelocity = intensity * CGFloat(sinf(calcRotation))
            let vector : CGVector = CGVectorMake(xVelocity, yVelocity)
            //Apply force to spaceship
            ship.physicsBody?.applyForce(vector)
        // ends stackActive
        }
     }
   }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: AnyObject in touches  {
        let location = touch.locationInNode(self.overlayScene)
        if self.overlayScene.nodeAtPoint(location) == self.ball {

    if (stickActive == true) {

        let move:SKAction = SKAction.moveTo(base.position, duration: 0.05)
        move.timingMode = .EaseOut

        ball.runAction(move)
   }
  }
 }
}
   //====================================================================
override func shouldAutorotate() -> Bool {
    return true
}
   //====================================================================
override func prefersStatusBarHidden() -> Bool {
    return true
}
   //====================================================================
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .AllButUpsideDown
    } else {
        return .All
    }
}
   //====================================================================
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

}

最佳答案

SceneKit 中存在一个问题,当在 2D 场景中进行更改但 3D 场景保持不变时,SpriteKit 叠加层不会自动重新绘制。换句话说,只有在需要重绘 3D View 时才会重绘 2D 叠加层。

您可以将 SCNViewplaying 属性设置为 YES 来解决这个问题。或者,您可以在对覆盖场景进行更改时调用 -setNeedsDisplay

关于ios - JoyStick 仅在 Camera Controls = true 时移动 Xcode、SWIFT、SpriteKit、SceneKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813558/

相关文章:

ios - prepareForSegue 正在通过对目标 View Controller 的引用传递值

ios - typedef 枚举与 CGSize?

ios - 无法在 Xcode 6 Playground 中使用 SpriteKit

ios - 无法使用 Parse 库 : Parse linker warning: file was built for unsupported file format

objective-c - 为什么在 Objective-C 中 nil 被预处理为 ((void *)0)?

ios - TableView 单元格不显示更新的数据

xcode - Apple文档中有关应用程序图标的困惑

objective-c - iOS 8 SpriteKit 在从 block / Action 中添加或删除 child 时崩溃

ios - 一局中有多个skshapenode?

ios - 调用内容类型为 : application/x-www-form-urlencoded and Authorization using NSURLSession 的 REST API