1

嗨,我不是一个经验丰富的 ios 应用程序开发人员,我正在使用表情符号创建这个太空游戏,但一切都在屏幕中间产生,当我使用加速度计移动玩家时,如果它运行正确,它就会消失,但是如果我向左倾斜它会消失并从右循环返回,我该如何解决这个问题并为我的游戏视图添加边框?

这是我的模拟器的截图 在此屏幕截图中,它显示了表情符号如何仅在屏幕的右半部分生成,而玩家也无法到达左半部分

import SpriteKit
import GameplayKit
import CoreMotion

class GameScene: SKScene, SKPhysicsContactDelegate {
    var player:SKSpriteNode!
    var emojis = ["emoji1","emoji2", "emoji3", "emoji4", "emoji5"]
    var stars:SKEmitterNode!
    var scoreLabal: SKLabelNode!
    var score: Int = 0 {
        didSet {
            scoreLabal.text = "Score: \(score)"
        }
    }
    var gameTimer: Timer!
    let emojiCatagory: UInt32 = 0x1 << 1
    let photonTorpedoCatagory: UInt32 = 0x1 << 0
    let motionManger = CMMotionManager()
    var xAcceleration:CGFloat = 0
    override func sceneDidLoad() {




        stars = SKEmitterNode(fileNamed: "Starfield")
        stars.position = CGPoint(x: 0, y: 1472)
        stars.advanceSimulationTime(10)
        self.addChild(stars)
        player = SKSpriteNode(imageNamed: "Gun")


        player.position = CGPoint(x: self.frame.size.width / 32, y: (0 - (self.frame.size.height - player.size.height ) ) / 2 )
        print("The current deviec's screen height is \(self.frame.height), and the current device's width is \(self.frame.size.width) and the player's position is \(player.position)")


        self.addChild(player)

        self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
        self.physicsWorld.contactDelegate = self
        scoreLabal = SKLabelNode(text: "Score: 0")
        scoreLabal.position = CGPoint(x:(self.frame.size.width - scoreLabal.frame.size.width ) / 2 - 20 , y: (self.frame.size.height - scoreLabal.frame.size.height ) / 2  - 20 )
        scoreLabal.fontName = "Symbol"
        scoreLabal.fontColor = .white
        scoreLabal.fontSize = 36
        score = 0
        self.addChild(scoreLabal)


        gameTimer = Timer.scheduledTimer(timeInterval: 0.75, target: self, selector: #selector(setEmoji), userInfo: nil, repeats: true)



         motionManger.accelerometerUpdateInterval = 0.2
        motionManger.startAccelerometerUpdates(to: OperationQueue.current!) { (data:CMAccelerometerData?, error:Error?) in
            if let accelerometerData = data {
                let acceleration = accelerometerData.acceleration
                self.xAcceleration = CGFloat(acceleration.x) * 0.75 + self.xAcceleration * 0.25
            }

        }
    }
    @objc func setEmoji() {
        emojis = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: emojis) as! [String]
        let emoji = SKSpriteNode(imageNamed: emojis[0])
        let randomPosition = GKRandomDistribution(lowestValue: 0, highestValue: Int(self.view!.bounds.width))
        print("this is the self . view", self.view!.bounds.width)
        print("this is the self the frame", self.frame.size.width)
        let position = CGFloat(randomPosition.nextInt())
        emoji.position = CGPoint(x: position, y: self.view!.bounds.height + emoji.size.height)
        emoji.physicsBody = SKPhysicsBody(rectangleOf: emoji.size)
        emoji.physicsBody?.isDynamic = true
        emoji.physicsBody?.categoryBitMask = emojiCatagory
        emoji.physicsBody?.contactTestBitMask = photonTorpedoCatagory
        emoji.physicsBody?.collisionBitMask = 0
        self.addChild(emoji)
        let animationduration = 5
        var acctionArray = [SKAction]()
        acctionArray.append(SKAction.move(to: CGPoint(x: position, y: (0 - (self.frame.size.height) ) / 2 ), duration: TimeInterval(animationduration)))

        acctionArray.append(SKAction.removeFromParent())
        emoji.run(SKAction.sequence(acctionArray))
    }


    func fireTorpedo(soundFileName: String, positionOf: CGPoint) {


        self.run(SKAction.playSoundFileNamed(soundFileName, waitForCompletion: false))
        let torpedoNode = SKSpriteNode(imageNamed: "torpedo")
        torpedoNode.position = player.position
        torpedoNode.position.y += 5
        torpedoNode.physicsBody = SKPhysicsBody(circleOfRadius: torpedoNode.size.width / 2)
        torpedoNode.physicsBody?.categoryBitMask = photonTorpedoCatagory
        torpedoNode.physicsBody?.contactTestBitMask = emojiCatagory
        torpedoNode.physicsBody?.collisionBitMask = 0
        self.addChild(torpedoNode)
        let animationDuration = 0.3
        var acctionArray = [SKAction]()
        acctionArray.append(SKAction.move(to: CGPoint(x: positionOf.x , y: self.frame.size.height + 10 ), duration: TimeInterval(animationDuration)))
        print("positionOf.x value = \(positionOf.x)")
        print("positionOf value = \(positionOf)")
        print("positionOf.y value = \(positionOf.y)")

        acctionArray.append(SKAction.removeFromParent())
        torpedoNode.run(SKAction.sequence(acctionArray))



    }



    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let PositionOfTouch = touch.location(in: view)
        }
        let PositionOfTouches = touches.first?.location(in: view)
        fireTorpedo(soundFileName: "torpedo1", positionOf: PositionOfTouches!)
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let PositionOfTouch = touch.location(in: view)
        }
        if score > 100 && score < 200 {
                    let PositionOfTouches = touches.first?.location(in: view)
            fireTorpedo(soundFileName: "torpedo2", positionOf: PositionOfTouches!)
    }
    }



    func torpedoCollideWithEmoji (torpedoNode: SKSpriteNode, emojiNode: SKSpriteNode) {
        let explosion = SKEmitterNode(fileNamed: "Explosion")!
        explosion.position = emojiNode.position
        self.addChild(explosion)
        torpedoNode.removeFromParent()
        emojiNode.removeFromParent()
        self.run(SKAction.wait(forDuration: 2)) {
            explosion.removeFromParent()
        }

    score += 5
    }

    override func didSimulatePhysics() {
        player.position.x += xAcceleration * 50


        if player.position.x < -20 {
            player.position = CGPoint(x: self.size.width, y: player.position.y)
        } else if player.position.x > self.size.width * 20 {
            player.position = CGPoint(x:  -20,y: player.position.y )
        }
    }




    func didBegin(_ contact: SKPhysicsContact) {
        var firstBody: SKPhysicsBody
        var secondBody: SKPhysicsBody

        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        }else {
            secondBody = contact.bodyA
            firstBody = contact.bodyB
        }

        if (firstBody.categoryBitMask & photonTorpedoCatagory) != 0 && (secondBody.categoryBitMask & emojiCatagory ) != 0 {
            torpedoCollideWithEmoji(torpedoNode: firstBody.node as! SKSpriteNode, emojiNode: secondBody.node as! SKSpriteNode)
        }
    }


    override func update(_ currentTime: TimeInterval) {

}

}
4

1 回答 1

0

您应该通过执行以下操作为视图添加边框:

// 1
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
// 2
borderBody.friction = 0
// 3
self.physicsBody = borderBody
于 2018-07-14T16:03:50.913 回答