0

我尝试创建位掩码类别,将所有恢复值设置为 1 以及阳光下的所有其他内容,包括在设备上运行应用程序,但它仍然无法正常工作。在设置一个 SKSpriteNode 与 retitution 1 以在 dx: 100 处击中另一个 skspritenode 后,它仍然没有反弹!这是我的 GameScene.swift 代码:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

override func didMove(to view: SKView) {

    let Ball = SKSpriteNode()
    Ball.self.childNode(withName: "Ball")

    let playerPaddle = SKSpriteNode()
    playerPaddle.self.childNode(withName: "playerPaddle")

    let computerPaddle = SKSpriteNode()
    computerPaddle.self.childNode(withName: "computerPaddle")

    let ballCategory: UInt32 = 0x1 << 1
    let paddleCategory: UInt32 = 0x1 << 2
    let borderCategory: UInt32 = 0x1 << 3

    Ball.physicsBody?.categoryBitMask = ballCategory
    playerPaddle.physicsBody?.categoryBitMask = paddleCategory
    computerPaddle.physicsBody?.categoryBitMask = paddleCategory
    self.physicsBody?.categoryBitMask = borderCategory

    let playerScore = SKLabelNode()
    playerScore.fontName = "Pong Score"
    playerScore.text = "0"
    playerScore.fontSize = 100
    playerScore.color = SKColor.white
    playerScore.position = CGPoint(x: -200, y: 220)
    playerScore.zPosition = 3
    addChild(playerScore)

    let comScore = SKLabelNode()
    comScore.fontName = "Pong Score"
    comScore.text = "0"
    comScore.fontSize = 100
    comScore.color = SKColor.white
    comScore.position = CGPoint(x: 200, y: 220)
    comScore.zPosition = 3
    addChild(comScore)

    var comScoreInt: Int = Int(comScore.text!)!
    comScoreInt += 1
    comScore.text = String(comScoreInt)

    var playerScoreInt: Int = Int(playerScore.text!)!
    playerScoreInt += 1
    playerScore.text = String(playerScoreInt)

    let bodyBorder = SKPhysicsBody(edgeLoopFrom: self.frame)
    bodyBorder.friction = 0
    bodyBorder.restitution = 1
    self.physicsBody = bodyBorder
}
}

PS 大部分物理都在 GameScene.sks 而不是 GameScene.swift 中,但这是位掩码所在的位置。

编辑:

球配置;

位置 x:0, y:0, z:0,

旋转:0,

尺寸:宽:25,高:25,

锚点:x:0.5, y:0.5,

白颜色,

混合因子:0,

混合模式:阿尔法,

阿尔法:1,

Ik 约束:最小:0,最大:360,

比例尺:x:1,y:1,

物理

身体类型:边界矩形,

仅动态检查,

摩擦力:0,

归还:1,

线性阻尼:0,

角阻尼:0,

类别、碰撞和现场掩码:4294967295,

接触面罩:0

初始速度:dx:100 dy:0

桨是相同的物理明智请帮助!

4

1 回答 1

0

我没有在 GameScene.sks 中应用初始速度,而是在 GameScene.swift 中应用了一个力,如下所示:Ball.physicsBody?.applyImpulse(CGVector(dx:50, dy:0))恢复开始工作

于 2017-08-08T07:37:51.050 回答