如何在对象中弹出精灵?

时间:2016-07-06 05:30:12

标签: swift sprite-kit

我试图让一个球在一个圆圈内反弹,所以它不能离开圆圈。现在球在自我框架中反弹。如何让球在圆圈内反弹?

let data = NSKeyedArchiver.archivedDataWithRootObject(urlString)

1 个答案:

答案 0 :(得分:6)

我已经准备了一个简单的测试项目来解释如何做到这一点:

import UIKit
import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    enum CollisionTypes: UInt32 {
        case Circle = 1
        case Ball = 2
        //case Enemy1 = 4
        //case Enemy2 = 8
        //case Enemy3 = 16
        //case EnemyBoss = 32
    }

    var circulo: SKSpriteNode!
    var padding: CGFloat = 40.0
    var center: CGPoint!

    override func didMoveToView(view: SKView) {

        self.physicsWorld.gravity = CGVectorMake(0, -6)
        self.physicsWorld.contactDelegate = self
        self.center = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))

        // Prepare circle
        circulo = SKSpriteNode(imageNamed: "circuloFondo2.png")
        //Circle that I want the ball bounce within
        circulo.size = CGSize(width:view.frame.size.width - padding , height: view.frame.size.width - padding)
        let circlePath = UIBezierPath(arcCenter: self.center, radius: (view.frame.size.width - padding*2)/2, startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
        circulo.color = UIColor(red: 0.15, green: 0.15, blue: 0.15, alpha: 1)
        circulo.colorBlendFactor = 1
        circulo.alpha = 1
        circulo.position = self.center
        circulo.zPosition = 1

        // Set physics
        let circuloBody = SKPhysicsBody.init(edgeLoopFromPath: circlePath.CGPath)
        self.physicsBody = circuloBody
        self.physicsBody!.affectedByGravity = false
        self.physicsBody!.usesPreciseCollisionDetection = true
        self.physicsBody!.dynamic = true
        self.physicsBody!.mass = 0
        self.physicsBody!.friction = 0
        self.physicsBody!.linearDamping = 0
        self.physicsBody!.angularDamping = 0
        self.physicsBody!.restitution = 1
        self.physicsBody!.categoryBitMask = CollisionTypes.Circle.rawValue
        self.physicsBody!.contactTestBitMask = CollisionTypes.Ball.rawValue
        //self.circulo.physicsBody!.collisionBitMask = 0

        self.addChild(circulo)


        //Prepare ball
        let ball = SKShapeNode(circleOfRadius: 9)
        ball.fillColor = SKColor.whiteColor()
        ball.position = self.center
        ball.zPosition = 1

        // Set physics
        ball.physicsBody = SKPhysicsBody(circleOfRadius: 9)
        ball.physicsBody!.affectedByGravity = true
        ball.physicsBody!.restitution = 0.8
        ball.physicsBody!.linearDamping = 0
        ball.physicsBody!.friction = 0.3
        ball.physicsBody?.dynamic = true
        ball.physicsBody!.mass = 0.5
        ball.physicsBody!.allowsRotation = true
        ball.physicsBody!.categoryBitMask = CollisionTypes.Ball.rawValue
        ball.physicsBody!.contactTestBitMask = CollisionTypes.Circle.rawValue
        //ball.physicsBody!.collisionBitMask = 0

        self.addChild(ball)

    }

    func didBeginContact(contact: SKPhysicsContact) {
        // elements
        if (contact.bodyA.categoryBitMask == CollisionTypes.Circle.rawValue &&
            contact.bodyB.categoryBitMask == CollisionTypes.Ball.rawValue) {
            print("contact between circle and ball")
        }
    }

circuloFondo2 图片:

enter image description here

关于该项目的

演示

enter image description here

在这个答案中,您可以看到所有代码,但如果您愿意,可以下载项目here