Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使对象免于SKcameranode移动_Ios_Iphone_Xcode_Swift - Fatal编程技术网

Ios 如何使对象免于SKcameranode移动

Ios 如何使对象免于SKcameranode移动,ios,iphone,xcode,swift,Ios,Iphone,Xcode,Swift,我目前正在开发一个滚动平台游戏,我想知道如何让操纵杆对象随着屏幕移动,但仍然可用。我尝试了很多方法,但都没有找到可行的选择。此时,操纵杆可用,但将与地图的其余部分一起从屏幕上滚动 这是我的密码: import SpriteKit import UIKit var map = SKNode() var idleFrames = [SKTexture]() var walkFrames = [SKTexture]() var idleFrames1 = [SKTexture]() var walkF

我目前正在开发一个滚动平台游戏,我想知道如何让操纵杆对象随着屏幕移动,但仍然可用。我尝试了很多方法,但都没有找到可行的选择。此时,操纵杆可用,但将与地图的其余部分一起从屏幕上滚动

这是我的密码:

import SpriteKit
import UIKit
var map = SKNode()
var idleFrames = [SKTexture]()
var walkFrames = [SKTexture]()
var idleFrames1 = [SKTexture]()
var walkFrames1 = [SKTexture]()
var attackFrames1 = [SKTexture]()
var idling = 0
var bullets = 0
var bullet = SKSpriteNode()
var leaf = SKEmitterNode()
var hud: SKSpriteNode?


class GameScene: SKScene, SKPhysicsContactDelegate {
    var sp33d: CGVector = CGVectorMake(0.0,0.0)
    var knock: CGVector = CGVectorMake(-10.0, 0.0)
    var knock1: CGVector = CGVectorMake(10.0, 0.0)
    var jsp33d: CGFloat = 170
    var gameStick: Joystick?
    var player: SKSpriteNode?
    var ground: SKSpriteNode?
    var ground2: SKSpriteNode?
    var ground3: SKSpriteNode?
    var canJump = false
    var specialbutton = SKSpriteNode(imageNamed: "special")

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        hud = self.childNodeWithName("hud") as? SKSpriteNode
        gameStick = Joystick()
        gameStick?.createJoystick(hud!.frame.width/4, nameBack: "joystick", nameMoving: "joystick1")
        gameStick!.backPart!.zPosition = 4
        gameStick!.movingPart!.zPosition = 5
        map.addChild(gameStick!.backPart!)
        map.addChild(gameStick!.movingPart!)

        self.addChild(map)

        player = self.childNodeWithName("player") as? SKSpriteNode
        ground = self.childNodeWithName("testGround") as? SKSpriteNode
        ground2 = self.childNodeWithName("testGround2") as? SKSpriteNode
        player?.physicsBody?.categoryBitMask = category.player
        ground?.physicsBody?.categoryBitMask = category.ground
        ground2?.physicsBody?.categoryBitMask = category.ground
        player?.physicsBody?.collisionBitMask = category.ground
        ground?.physicsBody?.collisionBitMask = category.player
        player!.position = CGPoint(x: CGRectGetMidX(self.frame), y: 70)

        specialbutton.position = CGPointMake(600, 83)
        specialbutton.xScale = 0.165
        specialbutton.yScale = 0.165
        specialbutton.alpha = 0.5
        map.addChild(specialbutton)

        camera = self.childNodeWithName("camera") as? SKCameraNode
        camera?.position = player!.position

        if charnumber == 2{
        player?.texture = SKTexture(imageNamed:"Sarah")
        }

        let idleAtlas = SKTextureAtlas(named: "idle.atlas")
        var idleframes = [SKTexture]()
        for var i=1; i<=4; i++ {
            let idleframe = "JohnIdle\(i)"
            idleframes.append(idleAtlas.textureNamed(idleframe))
        }
        idleFrames = idleframes

        let walkAtlas = SKTextureAtlas(named: "jrun.atlas")
        var walkframes = [SKTexture]()
        for var i=1; i<=8; i++ {
            let walkframe = "Johnrun\(i)"
            walkframes.append(walkAtlas.textureNamed(walkframe))
        }
        walkFrames = walkframes

        let idleAtlas1 = SKTextureAtlas(named: "Sarahidle.atlas")
        var idleframes1 = [SKTexture]()
        for var i=1; i<=4; i++ {
            let idleframe1 = "SarahIdle\(i)"
            idleframes1.append(idleAtlas1.textureNamed(idleframe1))
        }
        idleFrames1 = idleframes1

        let walkAtlas1 = SKTextureAtlas(named: "Sarahrun.atlas")
        var walkframes1 = [SKTexture]()
        for var i=1; i<=8; i++ {
            let walkframe1 = "Sarahrun\(i)"
            walkframes1.append(walkAtlas1.textureNamed(walkframe1))
        }
        walkFrames1 = walkframes1

        let attackAtlas1 = SKTextureAtlas(named: "sattack.atlas")
        var attackframes1 = [SKTexture]()
        for var i=1; i<=9; i++ {
            let attackframe1 = "sattack\(i)"
            attackframes1.append(attackAtlas1.textureNamed(attackframe1))
        }
        attackFrames1 = attackframes1

        self.physicsWorld.gravity = CGVectorMake(0, -10.0)
        self.physicsWorld.contactDelegate = self
    }

    func didBeginContact(contact:SKPhysicsContact) {
        if (contact.bodyA.categoryBitMask == category.player) && (contact.bodyB.categoryBitMask == category.ground) {
            idling = 0
            johnmove()
            print("hit")
            canJump = true
        }else{
        }
    }

    func didEndContact(contact: SKPhysicsContact) {
        if (contact.bodyA.categoryBitMask == category.player) && (contact.bodyB.categoryBitMask == category.ground) {
            canJump = false

        }
    }

    func bulletfire(){
        bullet = SKSpriteNode(imageNamed: "bullet1")
        leaf = SKEmitterNode(fileNamed: "leafParticle")!
        bullets = bullets + 1
        bullet.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(0.1, 0.1))
        bullet.physicsBody?.collisionBitMask = category.ground
        bullet.xScale = 0.016
        bullet.yScale = 0.016
        bullet.physicsBody?.affectedByGravity = false
        self.addChild(bullet)
        self.addChild(leaf)
        if player?.xScale == 1{
            bullet.xScale = -0.016
            bullet.position.x = (player?.position.x)! + 21.5
            bullet.position.y = (player?.position.y)! + 15.4
            bullet.physicsBody?.velocity = (CGVectorMake(1000.0, 0.0))
        }else{
            bullet.position.x = (player?.position.x)! - 21.5
            bullet.position.y = (player?.position.y)! + 15.4
            bullet.physicsBody?.velocity = (CGVectorMake(-1000.0, 0.0))
        }
        let action = SKAction.sequence([SKAction.waitForDuration(1), SKAction.removeFromParent()])
        bullet.runAction(action, completion: {bullets = bullets - 1})
        leaf.runAction(action)
    }

    func move(){
        if player?.xScale ==  1 {
            player?.size = CGSizeMake(80,80)
            player?.position.x = (player?.position.x)! - 20
        }else{
            player?.size = CGSizeMake(80,80)
            player?.position.x = (player?.position.x)! + 20

        }

    }

    func johnmove() {
        if charnumber == 1 {
            if idling == 1 {
                player?.size = CGSizeMake(80,80)
                player?.removeActionForKey("idle")
                player?.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(walkFrames, timePerFrame: 0.12, resize: false, restore: true)),withKey: "walk")
            }
            else if idling == 0 {
                player?.removeActionForKey("walk")
                player?.size = CGSizeMake(80,80)
                player?.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(idleFrames, timePerFrame: 0.6, resize: false, restore: true)),withKey: "idle")
            }else if idling == 2 {
                player?.removeActionForKey("walk")
                player?.removeActionForKey("idle")
                player?.size = CGSizeMake(80,80)
                player?.texture = SKTexture(imageNamed: "JohnJump.png")
            }
        }else{
            sarahhmove()
        }
    }

    func sarahhmove() {
        if idling == 1 {
            player?.removeActionForKey("idle1")
            player?.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(walkFrames1, timePerFrame: 0.12, resize: true, restore: true)),withKey: "walk1")
        }
        else if idling == 0 {
            player?.removeActionForKey("walk1")
            player?.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(idleFrames1, timePerFrame: 0.6, resize: false, restore: true)),withKey: "idle1")
        }else if idling == 2 {
            player?.removeActionForKey("wal1k")
            player?.removeActionForKey("idle1")
            player?.texture = SKTexture(imageNamed: "SarahJump.png")
        }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in (touches ) {
            let location = touch.locationInNode(self)
            var nodeTouched = SKNode()
            nodeTouched = self.nodeAtPoint(location)
            if nodeTouched.name == "joystick1" {
                gameStick?.movingPart?.position = location
                if location.x > gameStick!.backPart!.position.x + gameStick!.backPart!.frame.width/2{
                    gameStick?.movingPart?.position = CGPointMake(gameStick!.backPart!.position.x + gameStick!.backPart!.frame.width/2, gameStick!.movingPart!.position.y)
                }
                if location.y > gameStick!.backPart!.position.y + gameStick!.backPart!.frame.height/2{
                    gameStick?.movingPart?.position = CGPointMake(gameStick!.movingPart!.position.x, gameStick!.backPart!.position.y + gameStick!.backPart!.frame.height/2)
                }
            }
        }
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */
        for touch in (touches ) {
            let location = touch.locationInNode(self)
            var nodeTouched = SKNode()
            nodeTouched = self.nodeAtPoint(location)
            if nodeTouched.name == "joystick1" {
                gameStick?.movingPart?.position = location
                if location.x > gameStick!.backPart!.position.x + gameStick!.backPart!.frame.width/2{
                    gameStick?.movingPart?.position = CGPointMake(gameStick!.backPart!.position.x + gameStick!.backPart!.frame.width/2, gameStick!.movingPart!.position.y)
                }
                if location.y > gameStick!.backPart!.position.y + gameStick!.backPart!.frame.height/2{
                    gameStick?.movingPart?.position = CGPointMake(gameStick!.movingPart!.position.x, gameStick!.backPart!.position.y + gameStick!.backPart!.frame.height/2)
                }
            }else if idling == 0 && charnumber == 2{
                player?.size = CGSizeMake(80,80)
                player?.removeAllActions()
                if player?.xScale ==  1 {
                    player?.size = CGSizeMake(80,80)
                    player?.position.x = (player?.position.x)! + 20
                }else{
                    player?.size = CGSizeMake(80,80)
                    player?.position.x = (player?.position.x)! - 20
                }
                player?.size = CGSizeMake(80,80)
                player?.runAction(SKAction.repeatAction(SKAction.animateWithTextures(attackFrames1, timePerFrame: 0.09, resize: true, restore: true),count: 1),completion: {self.move()})
                    player?.size = CGSizeMake(80,80)
                player?.texture = SKTexture(imageNamed: "Sarah")
            }else if charnumber == 1 && idling == 0{
                player?.removeAllActions()
                bulletfire()
                let yrand = (arc4random_uniform(100) + 50)
                let xrand = (arc4random_uniform(300) + 80)
                let cartridge = SKSpriteNode(imageNamed: "casing")
                cartridge.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(0.1, 0.1))
                cartridge.physicsBody?.collisionBitMask = category.ground
                cartridge.xScale = 0.05
                cartridge.yScale = 0.05
                cartridge.zPosition = 3
                cartridge.physicsBody?.restitution = 0.5
                let spin = SKAction.rotateByAngle(CGFloat(M_PI), duration:0.6)
                cartridge.runAction(SKAction.repeatActionForever(spin))
                cartridge.physicsBody?.dynamic = true
                let flash = SKEmitterNode(fileNamed: "muzzleFlash")
                if player?.xScale == 1{
                    leaf.xScale = -1
                    flash?.position.x = (player?.position.x)! + 40
                    flash?.position.y = (player?.position.y)! + 15.3
                    cartridge.position.x = (player?.position.x)! + 27
                    cartridge.position.y = (player?.position.y)! + 15.3
                    player?.physicsBody?.applyImpulse(knock)
                    cartridge.physicsBody?.velocity = CGVectorMake(-CGFloat(yrand), CGFloat(xrand))
                }else{
                    flash?.position.x = (player?.position.x)! - 40
                    flash?.position.y = (player?.position.y)! + 15.3
                    flash?.xAcceleration = -5000
                    cartridge.position.x = (player?.position.x)! - 27
                    cartridge.position.y = (player?.position.y)! + 15.3
                    player?.physicsBody?.applyImpulse(knock1)
                    cartridge.physicsBody?.velocity = CGVectorMake(CGFloat(yrand), CGFloat(xrand))
                }
                flash?.zPosition = 3
                flash?.xScale = 0.15
                flash?.yScale = 0.15
                self.addChild(flash!)
                self.addChild(cartridge)
                let action = SKAction.sequence([SKAction.waitForDuration(0.15), SKAction.removeFromParent()])
                let action1 = SKAction.sequence([SKAction.waitForDuration(1.5), SKAction.removeFromParent()])
                flash?.runAction(action)
                cartridge.runAction(action1)
                johnmove()
            }
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in (touches ) {
            let location = touch.locationInNode(self)
            var nodeTouched = SKNode()
            nodeTouched = self.nodeAtPoint(location)
            if nodeTouched.name == "joystick1" {
                let act = SKAction.moveTo(gameStick!.backPart!.position, duration: 0.2)
                gameStick?.movingPart?.runAction(act)
            }
        }
    }

    override func update(currentTime: CFTimeInterval) {
        hud!.position = (camera?.position)!
        let action = SKAction.moveTo((player?.position)!, duration: 0.01)
        camera!.runAction(action)
        player?.size = CGSizeMake(80,80)
        leaf.position = bullet.position
        print(canJump)
        if (sp33d.dx > 1.5 || sp33d.dx < -1.5) && idling != 1 && idling != 2 {
            idling = 1
            johnmove()
        }else if sp33d.dx < 0.1 && sp33d.dx > -0.1 && idling != 0  && idling != 2{
            idling = 0
            johnmove()
        }
        if sp33d.dx > 0.1 {
            player!.xScale = 1
        } else if sp33d.dx < -0.1 {
            player!.xScale = -1
        }

        /* Called before each frame is rendered */
        let vX = gameStick!.movingPart!.position.x - gameStick!.backPart!.position.x

        let vY: CGFloat = gameStick!.movingPart!.position.y

        if  vY > gameStick!.backPart!.position.y + 20 && canJump == true{
            sp33d = CGVectorMake(vX/13, jsp33d)
            idling = 2
            johnmove()
        }else{
            sp33d = CGVectorMake(vX/13, 0)
        }
        player?.physicsBody?.applyImpulse(sp33d)
    }

}
任何帮助都将不胜感激


提前谢谢

尝试在update()函数中调用resetMovingPart()。

希望您能解决这个问题!我正在尝试解决相同的问题,洛莉已经解决了这个问题,实际上,你们必须使操纵杆成为摄影机节点的子节点,并使操纵杆在摄影机节点上而不是在场景上跟踪。啊。干得好,谢谢!
import Foundation
import UIKit
import SpriteKit

class Joystick: UIView {

    var movingPart: SKSpriteNode?
    var backPart: SKSpriteNode?

    let speed: CGFloat = 0.4

    var joyStickCenter: CGPoint?

    func createJoystick(dimensions: CGFloat, nameBack: String, nameMoving: String)
    {
        backPart = SKSpriteNode(imageNamed: "joystick")
        backPart?.size = CGSizeMake(dimensions, dimensions)
        backPart?.position = CGPoint(x: backPart!.size.width/2, y: backPart!.size.width/2)
        backPart?.name = nameBack
        backPart?.alpha = 0.4

        movingPart = SKSpriteNode(imageNamed: "joystick")
        movingPart?.size = CGSizeMake(dimensions*0.5, dimensions*0.5)
        movingPart?.position = backPart!.position
        movingPart?.name = nameMoving
        movingPart?.alpha = 0.8

        joyStickCenter = backPart!.position
    }

    func getDistance(p1: CGPoint, p2: CGPoint) -> Double
    {
        let firstPow = p2.x-p1.x
        let secondPow = p2.y-p1.y
        var squaredAdded = pow(firstPow, 2)
        squaredAdded += pow(secondPow, 2)
        let theSquirt = sqrt(Double(squaredAdded))

        return theSquirt
    }

    func resetMovingPart()
    {
        movingPart?.runAction(SKAction.moveTo(joyStickCenter!, duration: 0.4))
    }

    func calcXYDiff(loc: CGPoint) -> CGPoint
    {
        let oldMovingPartPoint = movingPart?.position
        movingPart?.position = loc
        if movingPart!.position.x-joyStickCenter!.x > backPart!.frame.width/2
        {
            movingPart?.position.x = oldMovingPartPoint!.x
        }
        if movingPart!.position.y-joyStickCenter!.y > backPart!.frame.height/2
        {
            movingPart?.position.y = oldMovingPartPoint!.y
        }
        let x = loc.x - joyStickCenter!.x
        let y = loc.y - joyStickCenter!.y
        return CGPoint(x: x*speed, y: y*speed)
    }