我已经搜索了互联网的深度,找到一些关于在SKSpritenodes上实施SKWarpGeometry的教程无济于事。
我有一个Trampoline 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吗?
答案 0 :(得分: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))
}