2

この unity チュートリアルに従って、動的な 2D 水を作成したいです。

http://gamedevelopment.tutsplus.com/tutorials/creating-dynamic-2d-water-effects-in-unity--gamedev-14143

http://gamedevelopment.tutsplus.com/tutorials/make-a-splash-with-2d-water-effects--gamedev-236

あとこれも

http://blog.prime31.com/water2d-part1/

これが私のコードです

class GameScene: SKScene, SKPhysicsContactDelegate {

    var box: SKSpriteNode!
    var nodes:[SKNode] = []

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        self.anchorPoint = CGPointMake(0.5, 0.5)

        // Set physics body
        let borderBody: SKPhysicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame);
        borderBody.friction = 0.0;
        self.physicsBody = borderBody;

        // Set contact delegate
        self.physicsWorld.contactDelegate = self;

        box = SKSpriteNode(color: UIColor.blackColor(), size: CGSize(width: 200, height: 20))
        box.position = CGPointMake(0, 0)
        box.physicsBody = SKPhysicsBody(rectangleOfSize: box.size)
        box.physicsBody!.dynamic = false

        self.addChild(box)

        let one = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 20, height: 20))
        one.position = CGPointMake(box.position.x - box.frame.size.width/2, box.position.y + box.frame.size.height * 2)

        one.physicsBody = SKPhysicsBody(rectangleOfSize: one.size)
        one.physicsBody!.allowsRotation = false
        self.addChild(one)
        nodes.append(one)
        self.attachPoint(box, point2: one, box: true)

        let two = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 20, height: 20))
        two.position = CGPointMake(box.position.x, box.position.y + box.frame.size.height * 2)

        two.physicsBody = SKPhysicsBody(rectangleOfSize: two.size)
        two.physicsBody!.allowsRotation = false
        self.addChild(two)
        nodes.append(two)
        self.attachPoint(box, point2: two, box: true)

        let three = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 20, height: 20))
        three.position = CGPointMake(box.position.x + box.frame.size.width/2, box.position.y + box.frame.size.height * 2)

        three.physicsBody = SKPhysicsBody(rectangleOfSize: three.size)
        three.physicsBody!.allowsRotation = false
        self.addChild(three)
        nodes.append(three)
        self.attachPoint(box, point2: three, box: true)

        self.attachPoint(one, point2: two, box: false)
        self.attachPoint(two, point2: three, box: false)

    }

    func attachPoint(point1: SKSpriteNode, point2: SKSpriteNode, box: Bool){

        if(box == true){

            let newPoint1 = CGPointMake(self.frame.size.width/2 + point2.position.x, self.frame.size.height/2  + point1.position.y)

            let newPoint2 = CGPointMake(self.frame.size.width/2 + point2.position.x, self.frame.size.height/2  + point2.position.y)

            // create a joint between two bodies
            let joint: SKPhysicsJointSpring = SKPhysicsJointSpring.jointWithBodyA(point1.physicsBody, bodyB: point2.physicsBody, anchorA: newPoint1, anchorB: newPoint2)

            joint.damping = 2.0
            joint.frequency = 9.0;

            self.physicsWorld.addJoint(joint)
        } else {

        let newPoint1 = CGPointMake(self.frame.size.width/2 + point1.position.x, self.frame.size.height/2  + point1.position.y)

        let newPoint2 = CGPointMake(self.frame.size.width/2 + point2.position.x, self.frame.size.height/2  + point2.position.y)

        // create a joint between two bodies
        let joint: SKPhysicsJointSpring = SKPhysicsJointSpring.jointWithBodyA(point1.physicsBody, bodyB: point2.physicsBody, anchorA: newPoint1, anchorB: newPoint2)

        joint.damping = 2.0
        joint.frequency = 9.0;

        self.physicsWorld.addJoint(joint)

        }

    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        let node = nodes[2]
        node.physicsBody?.velocity.dy = 20000

    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

これが結果です

この結果は変更なし 最初

タッチ後、touchesBegan メソッドでコードを実行します 2番目

ステップ 2 の後、ノードは回転を開始し、ノードの下に落ちます。 最後の

彼らが評価しない場合は、チュートリアルに従って、ノードの色で SKShapeNode を作成する必要があります。しかし、SKShapeNode は高価です。この効果を達成できる別の方法はありますか?

最も簡単な方法は、水の上部に画像を使用して前後に移動させることですが、動的ではありません..

Tiny Wings をプレイしたことがある場合、そこの水はチュートリアルでまったく同じ方法で実装されています。

わかりません。SpriteKit で作成できないか、方法がわからないだけです。

4

0 に答える 0