2016-10-25 18 views
8

私は、インターネットの深さを調べて、SKWritGeometryをSKSpritenodesに実装するためのチュートリアルを無駄に探しました。Spritekit | Swift3 |ノードにSKWarpGeometryを適用する

私はトランポリンSKSpritenodeとボールSKSpritenodeを持っています。

ballNode = childNode(withName: "ballNode") as? SKSpriteNode 
    ballNode?.physicsBody = SKPhysicsBody(circleOfRadius: (ballNode?.frame.size.width)!/2) 
    ballNode?.physicsBody?.isDynamic = true 
    ballNode?.physicsBody?.usesPreciseCollisionDetection = true 
    ballNode?.zPosition = 3 
    ballNode?.physicsBody!.categoryBitMask = PhysicsCategory.ballCategory 
    ballNode?.physicsBody!.contactTestBitMask = PhysicsCategory.trampolineCategory 
    ballNode?.physicsBody!.collisionBitMask = PhysicsCategory.platformCategory 
    ballNode?.physicsBody?.velocity = CGVector(dx: 0.0, dy: 0.0) 
    ballNode?.move(toParent: _gameNode) 

enumerateChildNodes(withName: "trampoline") {trampolineNode,_ in 
    trampoline = trampolineNode as? SKSpriteNode 
    trampolineNode.physicsBody = SKPhysicsBody.init(rectangleOf: CGSize(width: (trampoline?.size.width)!, height: (trampoline?.size.height)!)) 
    trampolineNode.physicsBody?.categoryBitMask = PhysicsCategory.trampolineCategory 
    trampolineNode.physicsBody?.contactTestBitMask = PhysicsCategory.ballCategory 
    trampolineNode.physicsBody?.collisionBitMask = PhysicsCategory.ballCategory 
    trampolineNode.physicsBody?.affectedByGravity = false 
    trampolineNode.physicsBody?.isDynamic = false 
    trampolineNode.physicsBody?.usesPreciseCollisionDetection = true 
    trampolineNode.physicsBody?.restitution = 1 
    trampolineNode.move(toParent: _gameNode) 

ballNodeがトランポリンに当たったとき。私はトランポリンノードの上部を内側に曲げたい。しかし、次のコードを実行すると、トランポリンの画像が消えます。私は、グリッドを設定することができ

if collision == PhysicsCategory.ballCategory | PhysicsCategory.trampolineCategory { 
     let src = [float2(0.0), float2(1.0), float2(2.0), 
        float2(3.0), float2(4.0), float2(5.0), 
        float2(6.0), float2(7.0), float2(8.0)] 

     let dst = [float2(0.0), float2(0.0), float2(2.0), 
        float2(3.0), float2(1.0), float2(5.0), 
        float2(6.0), float2(7.0), float2(8.0)] 

     warpGrid = SKWarpGeometryGrid(columns: 2, rows: 2, 
             sourcePositions: src, 
             destinationPositions: dst) 
     // 
     trampoline?.warpGeometry = warpGrid  <---This line removes the trampoline image, so I could only assume its the way I set up the source and destination points. 

     let transform = SKAction.warp(to: warpGrid!, duration: 1) 
     let transformAction = SKAction.repeat(transform!, count: 1) 
     trampoline?.run(transformAction)    
    } 

は間違ったノードが、私を導くために何のチュートリアルが存在しないように私には見当もつかない。誰もまだSKGeometryを使用していますか?

+2

残念ながら私はあなたの「誰でもまだそれを使用していますか?」という質問には「いいえ」と答えていると思います。これは誰も(Sprite Kitが持っている15人のユーザーのうち)誰も来ないことを知らない機能の1つでした。そしてその目的はその特徴によって制限されているようです。たとえば、ワープの状態の間に緩和のタイミングはありません。草や木々が揺れ動くという単純な目的のために、半分以下の焼き付けをしたアイデアのようです。私が最初に彼らのプレゼンテーションを聞いたとき、私はそれを使って柔らかい体をすることに興奮しました。しかし、いくつかの研究の後、それは確かにそれのために装備されていません。 – Confused

+0

spritekitは広く使われていませんか?ワオ。私がやりたいことが理論的にはとてもシンプルなので、単純な例が必要です。 – user3482617

+2

私が知る限り、ゼロの例があります。私はあなたと同じように少し使ってみましたが、それを使っている人はいませんでした。しかし、私はそれが私の望む使い方を意図したものではないとも感じました。私がアップルのもので見つけたことは、何かのために明示的に設計されていない場合、あなたの気まぐれにそれを融合させようと試みることは、ただの不満と怒りであるということです。意図どおりに使用している可能性があります。しかし、Appleは1年に1回デモを行うだけであるので、知るのは難しい。 – Confused

答えて

8

解決しました。これが他の人に役立つことを願っています

//Create your SKSpritenodes 
ballNode = childNode(withName: "ballNode") as? SKSpriteNode 
ballNode?.physicsBody = SKPhysicsBody(circleOfRadius: (ballNode?.frame.size.width)!/2) 
ballNode?.physicsBody?.isDynamic = true 
ballNode?.physicsBody?.usesPreciseCollisionDetection = true 
ballNode?.zPosition = 3 
ballNode?.physicsBody!.categoryBitMask = PhysicsCategory.ballCategory 
ballNode?.physicsBody!.contactTestBitMask = PhysicsCategory.trampolineCategory 
ballNode?.physicsBody!.collisionBitMask = PhysicsCategory.platformCategory 
ballNode?.physicsBody?.velocity = CGVector(dx: 0.0, dy: 0.0) 
ballNode?.move(toParent: _gameNode) 

enumerateChildNodes(withName: "trampoline") {trampolineNode,_ in 
trampoline = trampolineNode as? SKSpriteNode 
trampolineNode.physicsBody = SKPhysicsBody.init(rectangleOf: CGSize(width: (trampoline?.size.width)!, height: (trampoline?.size.height)!)) 
trampolineNode.physicsBody?.categoryBitMask = PhysicsCategory.trampolineCategory 
trampolineNode.physicsBody?.contactTestBitMask = PhysicsCategory.ballCategory 
trampolineNode.physicsBody?.collisionBitMask = PhysicsCategory.ballCategory 
trampolineNode.physicsBody?.affectedByGravity = false 
trampolineNode.physicsBody?.isDynamic = false 
trampolineNode.physicsBody?.usesPreciseCollisionDetection = true 
trampolineNode.physicsBody?.restitution = 1 
trampolineNode.move(toParent: _gameNode) 

    // Declare the following SKWarpGeometryGrids 
    var trampolineWarpGrid: SKWarpGeometryGrid? 
    var trampolineNoWarpGrid: SKWarpGeometryGrid? 

    // For 2x2 grid - State the source positions 
    let sourcePositions: [vector_float2] = [ 
     vector_float2(0, 0), vector_float2(0.5, 0), vector_float2(1, 0), 
     vector_float2(0, 0.5), vector_float2(0.5, 0.5), vector_float2(1, 0.5), 
     vector_float2(0, 1), vector_float2(0.5, 1), vector_float2(1, 1) 
    ] 

    // For 2x2 grid - State the destination positions 
    // To make the dent, I changed row 3 column 2 value from (0.5, 1) to (0.5, 0.8) 
    let destinationPositions: [vector_float2] = [ 
     vector_float2(0, 0), vector_float2(0.5, 0), vector_float2(1, 0), 
     vector_float2(0, 0.5), vector_float2(0.5, 0.5), vector_float2(1, 0.5), 
     vector_float2(0, 1), vector_float2(0.5, 0.8), vector_float2(1, 1) 
    ] 

    //Create the 2x2 warp grid based on the source and destination positions 
    trampolineWarpGrid = SKWarpGeometryGrid(columns: 2, rows: 2, 
            sourcePositions: sourcePositions, 
            destinationPositions: destinationPositions) 

    //Create and assign a Grid to the SKSpritenode that has no warp effects 
    //Will be used to convert the trampoline back to original view 
    trampolineNoWarpGrid = SKWarpGeometryGrid(columns: 2, rows: 2) 
    trampoline?.warpGeometry = trampolineNoWarpGrid 

func didBegin(_ contact: SKPhysicsContact) { 
    let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask 

    if collision == PhysicsCategory.ballCategory | PhysicsCategory.trampolineCategory { 
    //Transform the trampoline grid to show dent 
    let transform = SKAction.warp(to: trampolineWarpGrid!, duration: 0.3) 
    //Warp the trampoline gird back to normal 
    let transformBack = SKAction.warp(to: trampolineNoWarpGrid!, duration: 0.3) 
    //Run Action 
    let transformAction = SKAction.sequence([transform!, transformBack!]) 
    trampoline?.run(SKAction.repeat(transformAction, count: 1)) 
} 
関連する問題