我正在用一个在屏幕上移动的Sprite进行游戏,我使用self.physicsBody = SKPhysicsBody(edgeLoopFromRect:self.frame)创建了边缘碰撞。这似乎适用于屏幕的顶部和底部但是Sprite的左右两侧离开了屏幕。我不确定为什么会这样,感谢任何有关初学者的建议。我为我设置的滚动背景添加了代码。我还有其他敌人的精灵在屏幕上产生并进入视野,这可能会影响边界?
class GameScene: SKScene, SKPhysicsContactDelegate{
struct PhysicsCategory {
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let Edge : UInt32 = 0b1
static let Spaceman: UInt32 = 0b10
static let Ship: UInt32 = 0b11
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody?.categoryBitMask = PhysicsCategory.Edge
physicsWorld.contactDelegate = self
// Repeating Background
var bgTexture = SKTexture(imageNamed: "spacebackground")
var movebg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
var replacebg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var moveForever = SKAction.repeatActionForever(SKAction.sequence([movebg, replacebg]))
for var i:CGFloat=0; i<3; i++ {
var wave = SKSpriteNode(texture: bgTexture)
wave.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
wave.size.height = self.frame.height
wave.runAction(moveForever)
self.addChild(wave)
}
Spaceman.texture = (Texture1)
Spaceman.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
Spaceman.setScale(0.4)
Spaceman.physicsBody=SKPhysicsBody(rectangleOfSize: TheBat.size)
Spaceman.physicsBody?.affectedByGravity = true
Spaceman.physicsBody?.dynamic = true
Spaceman.physicsBody?.allowsRotation = false
Spaceman.physicsBody?.categoryBitMask = PhysicsCategory.Spaceman
Spaceman.physicsBody?.contactTestBitMask = PhysicsCategory.Ship
Spaceman.physicsBody!.collisionBitMask = PhysicsCategory.Edge
self.addChild(Spaceman)
答案 0 :(得分:1)
检查以确保您的场景视图设置为viewcontroller的边界,这应该在您将场景添加到视图的地方完成,应该是其中之一
/* Set the scale mode to scale to fit the window */
scene.size = skView.bounds.size
scene.scaleMode = .ScaleToFit;
/* Set the scale mode to scale to fit the window */
scene.size = skView.bounds.size
scene.scaleMode = .AspectFit;
如果是.AspectFill,你将超出屏幕范围。
在SKView中也可以使用showPhysics = true来显示碰撞边界。