【发布时间】:2015-04-14 19:07:13
【问题描述】:
我正在制作一个带有在屏幕上移动的 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)
【问题讨论】:
标签: ios swift sprite-kit edge-detection