我有一个工作太空射击游戏。基本设置。玩家控制左右移动的船只和子弹自动生成。我想在你达到一定分数后添加“老板”。
为了解决这个问题,如果得分== 5,我就可以使用,让精灵出现并移动
它没有工作,所以我把分数设为0,它产生并移动到它的位置。
我被困在我的身上。我的猜测是Boss Sprite和EnemyShips在它们产生时碰撞很多并且没有让Boss Sprite Spawn。
Boss精灵命名为tj.JPG
所以我添加了一个CollisonBitMask并将其设置为0,但这也没有用。
这是EnemyTimer的问题吗?我甚至设置了repeat = false来测试它并设置得分== 1并且它也没有任何影响。
任何帮助将不胜感激。我很想学会在达到所需分数后如何正确添加老板。
这是我的GameScene
import SpriteKit
import UIKit
struct PhysicsCatagory {
static let Enemy : UInt32 = 1 //000000000000000000000000000001
static let Bullet : UInt32 = 2 //00000000000000000000000000010
static let Player : UInt32 = 3 //00000000000000000000000000100
static let tj: UInt32 = 4
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var Highscore = Int()
var Score = Int()
var Player = SKSpriteNode(imageNamed: "player.png")
var ScoreLbl = UILabel()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var HighscoreDefault = NSUserDefaults.standardUserDefaults()
if (HighscoreDefault.valueForKey("Highscore") != nil){
Highscore = HighscoreDefault.valueForKey("Highscore") as! NSInteger
}
else {
Highscore = 0
}
physicsWorld.contactDelegate = self
self.scene?.backgroundColor = UIColor.darkGrayColor()
self.scene?.size = CGSize(width: 640, height: 1136)
self.addChild(SKEmitterNode(fileNamed: "SparkParticle")!)
Player.position = CGPointMake(self.size.width / 2, self.size.height / 5)
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCatagory.Player
Player.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
Player.physicsBody?.dynamic = false
var Timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: Selector("SpawnBullets"), userInfo: nil, repeats: true)
var EnemyTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("SpawnEnemies"), userInfo: nil, repeats: true)
// --------------------------
if Score == 1 {
let TJ = SKSpriteNode(imageNamed: "tj.JPG")
TJ.physicsBody = SKPhysicsBody(rectangleOfSize: TJ.size)
TJ.physicsBody?.categoryBitMask = PhysicsCatagory.tj
TJ.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
TJ.physicsBody?.affectedByGravity = false
TJ.physicsBody?.dynamic = false
// TJ.physicsBody?.collisionBitMask = 0
TJ.position = CGPoint(x: self.size.width / 2, y: self.size.height + 30)
let action = SKAction.moveToY(self.size.height / 2, duration: 2.0)
TJ.runAction(SKAction.sequence([action]))
self.addChild(TJ)
}
// ------------------
self.addChild(Player)
ScoreLbl.text = "\(Score)"
ScoreLbl = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
ScoreLbl.backgroundColor = UIColor(red: 0.6, green: 0.1, blue: 0.1, alpha: 0.3)
ScoreLbl.textColor = UIColor.whiteColor()
self.view?.addSubview(ScoreLbl)
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if (((firstBody.categoryBitMask == PhysicsCatagory.Enemy) && (secondBody.categoryBitMask == PhysicsCatagory.Bullet)) ||
((firstBody.categoryBitMask == PhysicsCatagory.Bullet) && (secondBody.categoryBitMask == PhysicsCatagory.Enemy))){
CollisionWithBullet(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)
}
else if ((firstBody.categoryBitMask == PhysicsCatagory.Enemy) && (secondBody.categoryBitMask == PhysicsCatagory.Player) ||
(firstBody.categoryBitMask == PhysicsCatagory.Player) && (secondBody.categoryBitMask == PhysicsCatagory.Enemy)){
CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode)
}
}
func CollisionWithBullet(Enemy: SKSpriteNode, Bullet:SKSpriteNode){
Enemy.removeFromParent()
Bullet.removeFromParent()
Score++
ScoreLbl.text = "\(Score)"
print(Score)
}
func CollisionWithPerson(Enemy:SKSpriteNode, Person: SKSpriteNode){
let ScoreDefault = NSUserDefaults.standardUserDefaults()
ScoreDefault.setValue(Score, forKey: "Score")
ScoreDefault.synchronize()
if (Score > Highscore){
let HighscoreDefault = NSUserDefaults.standardUserDefaults()
HighscoreDefault.setValue(Score, forKey: "Highscore")
}
Enemy.removeFromParent()
Person.removeFromParent()
self.view?.presentScene(EndScene())
ScoreLbl.removeFromSuperview()
}
func SpawnBullets(){
let Bullet = SKSpriteNode(imageNamed: "papercup.png")
Bullet.zPosition = -5
Bullet.position = CGPointMake(Player.position.x, Player.position.y)
let action = SKAction.moveToY(self.size.height + 30, duration: 0.8)
let actionDone = SKAction.removeFromParent()
Bullet.runAction(SKAction.sequence([action, actionDone]))
Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCatagory.Bullet
Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
Bullet.physicsBody?.affectedByGravity = false
Bullet.physicsBody?.dynamic = false
self.addChild(Bullet)
}
func SpawnEnemies(){
let Enemy = SKSpriteNode(imageNamed: "Dispenser.png")
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 150
let SpawnPoint = UInt32(MaxValue - MinValue)
Enemy.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
Enemy.physicsBody = SKPhysicsBody(rectangleOfSize: Enemy.size)
Enemy.physicsBody?.categoryBitMask = PhysicsCatagory.Enemy
Enemy.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
Enemy.physicsBody?.affectedByGravity = false
Enemy.physicsBody?.dynamic = true
let action = SKAction.moveToY(-70, duration: 2.0)
let actionDone = SKAction.removeFromParent()
Enemy.runAction(SKAction.sequence([action, actionDone]))
self.addChild(Enemy)
// if Score == 5
// {
// Enemy.removeFromParent()
// }
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
这是问题所在的地方
if Score == 1 {
let TJ = SKSpriteNode(imageNamed: "tj.JPG")
TJ.physicsBody = SKPhysicsBody(rectangleOfSize: TJ.size)
TJ.physicsBody?.categoryBitMask = PhysicsCatagory.tj
TJ.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
TJ.physicsBody?.affectedByGravity = false
TJ.physicsBody?.dynamic = false
// TJ.physicsBody?.collisionBitMask = 0
TJ.position = CGPoint(x: self.size.width / 2, y: self.size.height + 30)
let action = SKAction.moveToY(self.size.height / 2, duration: 2.0)
TJ.runAction(SKAction.sequence([action]))
self.addChild(TJ)
}