gpt4 book ai didi

ios - 水平边界在 SpriteKit 中不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:22 26 4
gpt4 key购买 nike

import SpriteKit

class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
/* let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!";
myLabel.fontSize = 45;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

self.addChild(myLabel)*/ let sprite = SKSpriteNode(imageNamed:"Beach Ball-100")
self.addChild(sprite)
let bb = SKPhysicsBody(edgeLoopFromRect: self.frame)
bb.friction = 0
self.physicsBody = bb

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */

for touch in touches {
let location = touch.locationInNode(self)

let sprite = SKSpriteNode(imageNamed:"Beach Ball-100")
self.addChild(sprite)

sprite.xScale = 0.15
sprite.yScale = 0.15

sprite.position = location
sprite.color = UIColor.redColor()
//sprite.physicsBody = SKPhysicsBody(edgeLoopFromRect:self.frame)
sprite.physicsBody = SKPhysicsBody(circleOfRadius: 2.5)
//sprite.physicsBody?.usesPreciseCollisionDetection = true
sprite.physicsBody?.affectedByGravity = true
sprite.physicsBody?.allowsRotation = false
sprite.physicsBody?.restitution = 0.5
sprite.physicsBody?.linearDamping = 0
sprite.physicsBody?.friction = 0
//self.physicsWorld.

self.physicsWorld.gravity = CGVectorMake(0,-2.5)

//self.physicsWorld.gravity = CGVectorMake(0.25, -0.25)
//var action2 = SKAction.rotateByAngle(180.0, duration:5)
//sprite.runAction( action2)
//action = SKAction.applyTorque(2, duration: 3)
//sprite.runAction(SKAction.repeatActionForever(action))

//let boundaries = UICollisionBehavior(items: [sprite])
}
}
}

水平边界似乎不起作用。如果我从同一点放下球,因为摩擦力设置为零,它们会一直移出屏幕。请帮助我了解问题所在。

最佳答案

这可能是因为您的场景大小与 View 大小不同。默认情况下,场景是从 .sks 文件加载的,它的大小为 1024x768。

要解决此问题,您可以在 GameViewController 中进行以下更改:

override func viewDidLoad() {
super.viewDidLoad()

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true

/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true

/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill

//Make scene's size same as view's size
scene.size = skView.bounds.size
skView.presentScene(scene)
}
}

希望这对您有所帮助!

关于ios - 水平边界在 SpriteKit 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33660023/

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