• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Sprite Kit 所有对象仅出现在屏幕的右半部分

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:21:36 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

这是我的模拟器的截图 In this screenshot, it shows how the emojis only spawn on the right half of the screen, and the player can't get to the left half either

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(xself.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) {

}

}



Best Answer-推荐答案


你应该这样做给 View 添加一个边框:

// 1
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
// 2
borderBody.friction = 0
// 3
self.physicsBody = borderBody

关于ios - Sprite Kit 所有对象仅出现在屏幕的右半部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51340877/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap