Sprite Kit - Sometimes a ball disappears from the screen?

enter image description here

I am new to sprite pack. I tried a simple ball game with 2 players, the other a slow ball tracking. But I discovered a problem. When I move the line to the ball (with the edge), the ball disappears from the screen. Another time, no problem, the ball bounces. What is the problem?

I have one GameScene, sks and ViewController. My sprite nodes are from sks. If someone explains this case. Will be better. I attached what I did below.

My GameScene:

class GameScene: SKScene {

var ball = SKSpriteNode()
var enemy = SKSpriteNode()
var main = SKSpriteNode()

override func didMove(to view: SKView) {

    ball = self.childNode(withName: "ball") as! SKSpriteNode
    enemy = self.childNode(withName: "enemy") as! SKSpriteNode
    main = self.childNode(withName: "main") as! SKSpriteNode

    ball.physicsBody?.applyImpulse(CGVector(dx: -20, dy: -20))
    ball.physicsBody?.linearDamping = 0
    ball.physicsBody?.angularDamping = 0

    let border = SKPhysicsBody(edgeLoopFrom: self.frame)
    border.friction = 0
    border.restitution = 1

    self.physicsBody = border

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches {

        let location = touch.location(in: self)
        main.run(SKAction.moveTo(x: location.x, duration: 0.2))
    }
}

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

    for touch in touches {

        let location = touch.location(in: self)
        main.run(SKAction.moveTo(x: location.x, duration: 0.2))
    }

}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered

    enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.5))
}

View controller:

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true


    }
}

override var prefersStatusBarHidden: Bool {
    return true
}

Pad Settings:

pad

Ball Settings:

ball

Some updates

I tried some messages in the update function, and then ran into the same housing ball coming out on the left side of the device (using iPhone 6S)

enter image description here

enter image description here

2016-12-08 14: 27: 54.436485 Pong [14261: 3102941] fatal error: ball from the left borders: file

enter image description here

+4
3

, . , , / , , . , , .

"" - :

enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.5))

, , , , . , , , .

, , , ... , , ​​ .

, , , , , .

+2

? update() , , , "". ( , ).

, , , , (.. ) , ( ). , ( ), , "" , - preciseCollisionDetection=true , collisionBitMask.

+1

, ( .update())

if ball.position.x > frame.maxX { fatalError(" ball out of right bounds") }
if ball.position.x < frame.minX { fatalError(" ball out of left bounds") }
if ball.position.y > frame.maxY { fatalError(" ball out of top bounds") }
if ball.position.y < frame.minY { fatalError(" ball out of bottom bounds) }

:

print(ball.position)

, - , - , - .

( ) "fatalError" "ball.position = CGPoint(x: 0, y: 0)" - "reset" .

, , if-statements.

var lastBallLocation = CGPoint(x: 0, y: 0) // Just to initialize
override func update( prams ) {
  if ball.position.x > frame.maxX { ball.position = lastBallLocation }
  // .. copy the other three cases
  lastBallLocation = ball.position // update only on successful position

( node spritenode , , , - "" )

be sure to give SKSpriteNode a physical body, so bounce off the red blocks

:

enter image description here

+1

Source: https://habr.com/ru/post/1663209/


All Articles