Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite Kit - Sometimes ball disappearing from the screen?

enter image description here

I'm new with sprite kit. I have tried simple ball bouncing game with 2 player, another is tracking the ball slowly. But I have discovered a problem. When I move the line to ball (with edge) ball disappearing from the screen. Another times not a problem, ball bouncing. What is the problem?

I have one GameScene, sks and ViewController. My sprite nodes coming from sks. If someone explain this case. It would be better. I have 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 have tried some messages in update function, then encountered with same case ball goes outside from 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 out of left bounds: file

enter image description here

like image 527
iamburak Avatar asked Dec 08 '16 08:12

iamburak


1 Answers

You're pinching the ball against the wall, with the enemy. This means that the force is eventually enough to create enough speed of ball movement/force to overcome the physics system, so it pops through the wall. If you make your enemy stop before it pinces the ball against the wall, you should be fine.

This 'pincing' is occurring because of this line of code:

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

This is making the enemy chase the ball, which is a good idea for a ball game, but for the way it's being moved is wrong. Using an Action means the enemy has infinite force applied to it, and is aiming for the middle of the ball.

So when the ball gets to the wall, it's stopped against a physics object with infinite static force, then this enemy comes along and applies infinite force from the other side... and the ball either pops inside the bounds of the enemy, or over the other side of the wall, because it's being crushed by infinite forces.

So you either need to take very good care of how you control the enemy with Actions, or use forces to control the enemy, as these won't be infinite, and the physics system will be able to push back on the enemy.

like image 156
Confused Avatar answered Nov 15 '22 07:11

Confused