gpt4 book ai didi

ios - 用物理创建下雨动画

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:20 24 4
gpt4 key购买 nike

我目前正在开发一个 iOS 应用程序,该应用程序使用 SpriteKit 游戏技术以 Swift 编写。在这个游戏中,雨滴从屏幕顶部落到底部。当它们触及底部(由 physicsBody 表示)时,它们应该返回到屏幕顶部的原始位置,从而允许动画在游戏的生命周期内继续。

因为我只需要几滴,所以我这样创建它们:

let drop = SKSpriteNode(imageNamed: "drop")
drop.position = CGPointMake(x,y)
drop.physicsBody = SKPhysicsBody(rectangleOfSize: drop.size)
addChild(drop)

然后我创建位掩码:

drop.physicsBody!.categoryBitmask = DropCategory
bottom.physicsBody!.categoryBitmask = BottomCategory
drop.physicsBody!.contactTestBitmask = BottomCategory

我添加了 didBeginContact 函数,当下降到达底部时我会收到通知。然后我的控制台显示“hit ground”。

func didBeginContact(contact: SKPhysicsContact) {

var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}

if firstBody.categoryBitMask == BlockCategory && secondBody.categoryBitMask == GroundCategory {
println("hit ground")

firstBody.node?.removeFromParent()
let drop = SKSpriteNode(imageNamed: "drop")
drop.name = "testNode"
drop.position = CGPointMake(self.frame.width - self.frame.width/3*2, self.frame.height) // At start Position
drop.physicsBody = SKPhysicsBody(rectangleOfSize: drop.size)
drop.physicsBody?.categoryBitMask = BlockCategory
drop.physicsBody?.contactTestBitMask = GroundCategory
addChild(drop)
}
}

但是,水滴的位置不会改变。没有语法错误,所以我的问题是什么?

如果我替换它:

firstBody.node!.position = CGPointMake(x, y)

用这个:

firstBody.applyImpulse(CGVector)

它给了下降动力。我怎样才能使第一个选项也起作用?

最佳答案

您可以尝试在触及底部时移除节点,然后再次在顶部添加新节点。加载纹理以在单独的 SKTexture 变量中重复使用。像这样

let dropTexture = SKTexture(imageNamed: "drop.png")

这样使用

let drop = SKSpriteNode(texture: dropTexture)

然后在didBeginContact

里面
func didBeginContact(contact: SKPhysicsContact) {
println("collided")
var bodyA : SKPhysicsBody!
var bodyB : SKPhysicsBody!

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
{
bodyA = contact.bodyA!
bodyB = contact.bodyB!
}
else
{
bodyB = contact.bodyA!
bodyA = contact.bodyB!
}

if bodyA.categoryBitMask == bottomCategoryBitMask && bodyB.categoryBitMask == raindropCategoryBitMask
{
if bodyB.node?.parent != nil // Added line
{
bodyB.node?.removeFromParent()
let drop = SKSpriteNode(imageNamed: "1.png")
drop.name = "testNode"
drop.position = CGPointMake(self.frame.width - self.frame.width/3*2, self.frame.height) // At start Position
drop.physicsBody = SKPhysicsBody(rectangleOfSize: drop.size)
drop.physicsBody?.categoryBitMask = raindropCategoryBitMask
drop.physicsBody?.contactTestBitMask = bottomCategoryBitMask
addChild(drop)
}
}
}

关于ios - 用物理创建下雨动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28396475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com