gpt4 book ai didi

ios - 触摸并按住 SpriteKit SKSpriteNode 速度

转载 作者:搜寻专家 更新时间:2023-11-01 06:49:13 25 4
gpt4 key购买 nike

我的 SKSpriteNode 由于 self.physicsWorld.gravity = CGVectorMake(0.0, -4.0); 而被迫掉到地上。当用户点击显示并按住它时,我希望 SKSpriteNode 飞起来,当用户停止触摸后,它再次落到地上。

我试过改变速度,但它只会产生很小的反弹,比如 applyImpulse 方法...:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */

for touch: AnyObject in touches {

let location = touch.locationInNode(self)

flyingObject.physicsBody.velocity = CGVectorMake(0, 100)
}
}

编辑:

这是我的场景初始化代码

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

// Physics
self.physicsWorld.gravity = CGVectorMake(0.0, -4.0);

// flyingObject
var flyingObjectTexture = SKTexture(imageNamed:"flyingObject")
flyingObject.filteringMode = SKTextureFilteringMode.Nearest

flyingObject = SKSpriteNode(texture: flyingObjectTexture)
flyingObject.setScale(1)
flyingObject.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)

flyingObject.physicsBody = SKPhysicsBody(circleOfRadius:flyingObject.size.height/2.0)
flyingObject.physicsBody.dynamic = true
flyingObject.physicsBody.allowsRotation = false

self.addChild(flyingObject)

// Ground
var groundTexture = SKTexture(imageNamed:"Ground")

var sprite = SKSpriteNode(texture:groundTexture)
sprite.setScale(0.5)
sprite.position = CGPointMake(self.size.width/2 - 100, sprite.size.height)

self.addChild(sprite)

var ground = SKNode()

ground.position = CGPointMake(0, groundTexture.size().height / 2)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.width, groundTexture.size().height * 0.5))

ground.physicsBody.dynamic = false
self.addChild(ground)

}

最佳答案

感谢 Code Monkey,我能够想出解决方案,这比我想象的要容易:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
isTouching = true
}

override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
/* Called when a touch begins */
isTouching = false
}

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if isTouching {
flyingObject.physicsBody.applyImpulse(CGVectorMake(0, 5))
}
}

关于ios - 触摸并按住 SpriteKit SKSpriteNode 速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24580510/

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