gpt4 book ai didi

Xcode 6 : Sprite node does not come in contact with other sprite node when tapped? 掉出屏幕?

转载 作者:行者123 更新时间:2023-11-30 10:19:18 25 4
gpt4 key购买 nike

我正在使用 Swift 工作,并创建了一个 SKSpriteNode 作为我游戏中的基础。我已将其设置到正确的位置并使物理体与屏幕一样宽:

//Ground
var groundTexture = SKTexture(imageNamed: "groundBar")
var groundBar = SKSpriteNode(texture: groundTexture)
groundBar.setScale(2)
groundBar.position = CGPointMake(self.size.width/2, self.size.height/2)
groundBar.hidden = true

self.addChild(groundBar)

var ground = SKNode()

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

ground.physicsBody?.dynamic = false



self.addChild(ground)

然后我有另一个 SKSpriteNode 这是我游戏中的角色,当点击时,我让这个角色向上并向右跳:

//Savior character
var saviorTexture = SKTexture(imageNamed: "character")
saviorTexture.filteringMode = SKTextureFilteringMode.Nearest

savior = SKSpriteNode(texture: saviorTexture)
savior.setScale(1)
savior.position = CGPoint(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.4)

savior.physicsBody = SKPhysicsBody(circleOfRadius: savior.size.height/2)
savior.physicsBody?.dynamic = true
savior.physicsBody?.allowsRotation = false

self.addChild(savior)




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

for touch: AnyObject in touches {

let location = touch.locationInNode(self)

savior.physicsBody?.velocity = CGVectorMake(0, 0) //pt it returns to?
savior.physicsBody?.applyImpulse(CGVectorMake(5, 30)) //direction/height that it jumps }

在点击之前,角色位于地面节点上,因此不会从屏幕上掉下来。然而,当点击时,角色会跳起来并从屏幕上掉下来。就像没有接触地面一样。

我尝试过调整地面大小、重新定位地面,但角色仍然从屏幕上掉下来。请注意,当向量为 (0,30) 并且它只是直接向上跳跃时,它不会从屏幕上掉下来。

这里发生了什么?

最佳答案

在 View Controller 中,您可以执行 skView.showsPhysics = true 并查看物理体是否位于您认为的位置。另外,请确保它们的 physicalsBody.collisionBitMask 不相同。

关于Xcode 6 : Sprite node does not come in contact with other sprite node when tapped? 掉出屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28141394/

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