Scope
-
Hi, I'm doing a lesson in the utub, there's a way to move to another level, but when I'm on it, all the images are missing, I think it's a size thing, how can we fix that?
This is GameScene.swift:
struct PhysicsCategory { static let En: UInt32 = 1 static let Bullet: UInt32 = 2 static let Player: UInt32 = 3
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var Player = SKSpriteNode(imageNamed: "Image")
var score = Int()
var Scorelbl = UILabel()
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = selfself.scene?.size = CGSize(width: self.frame.size.width, height: self.frame.size.height)
Player.position = CGPointMake(self.size.width / 2, self.size.height / 5)
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCategory.Player
Player.physicsBody?.contactTestBitMask = PhysicsCategory.En
Player.physicsBody?.dynamic = falsevar Timer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("Spaw"), userInfo: nil, repeats: true)
var EnTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("SpawEn"), userInfo: nil, repeats: true)
self.addChild(Player)
Scorelbl.text = "(score)"
Scorelbl = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
Scorelbl.backgroundColor = UIColor.whiteColor()
Scorelbl.textColor = UIColor.blackColor()
self.view?.addSubview(Scorelbl)
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody: SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCategory.En) && (secondBody.categoryBitMask == PhysicsCategory.Bullet) || (firstBody.categoryBitMask == PhysicsCategory.Bullet) && (secondBody.categoryBitMask == PhysicsCategory.En)){
collisionBullet(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)
}
else if ((firstBody.categoryBitMask == PhysicsCategory.En) && (secondBody.categoryBitMask == PhysicsCategory.Player) || (firstBody.categoryBitMask == PhysicsCategory.Player) && (secondBody.categoryBitMask == PhysicsCategory.En)){
collisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode)
}
}
func collisionBullet(En: SKSpriteNode, Bullet: SKSpriteNode){
En.removeFromParent()
Bullet.removeFromParent()
score++
Scorelbl.text = "(score)"}
func collisionWithPerson(En: SKSpriteNode, Person: SKSpriteNode){
En.removeFromParent()
Person.removeFromParent()
view?.presentScene(Next())
Scorelbl.removeFromSuperview()
}func Spaw(){
var Bullet = SKSpriteNode(imageNamed: "Image-1")
Bullet.zPosition = -5
Bullet.position = CGPointMake(Player.position.x, Player.position.y)
let action = SKAction.moveToY(self.size.height + 30, duration: 1.0)
let actionDone = SKAction.removeFromParent()
Bullet.runAction(SKAction.sequence([action, actionDone]))
Bullet.runAction(SKAction.repeatActionForever(action))
Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCategory.Bullet
Bullet.physicsBody?.contactTestBitMask = PhysicsCategory.En
Bullet.physicsBody?.affectedByGravity = false
Bullet.physicsBody?.dynamic = falseself.addChild(Bullet)
}func SpawEn(){
var En = SKSpriteNode(imageNamed: "Image-2")
var minVal = self.size.width / 8
var maxVal = self.size.width - 20
var Point = UInt32(maxVal - minVal)
En.position = CGPoint(x: CGFloat(arc4random_uniform(Point)), y: self.size.height)
En.physicsBody = SKPhysicsBody(rectangleOfSize: En.size)
En.physicsBody?.categoryBitMask = PhysicsCategory.En
En.physicsBody?.contactTestBitMask = PhysicsCategory.Bullet
En.physicsBody?.affectedByGravity = false
En.physicsBody?.dynamic = truelet action = SKAction.moveToY(-60, duration: 3.0)
En.runAction(SKAction.repeatActionForever(action))
let actionDone = SKAction.removeFromParent()En.runAction(SKAction.sequence([action, actionDone]))
self.addChild(En)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins /
for touch in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func update(currentTime: CFTimeInterval) {
/ Called before each frame is rendered /
}
}Next GameSceneController.swift:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"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 = .AspectFillskView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}When the enemy enters the plane, we go to the next stage where the switch to the next level is next.swift:
class Next: SKScene{
var RestartB : UIButton!override func didMoveToView(view: SKView) {
scene?.backgroundColor = UIColor.redColor()RestartB = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 3, height: 30))
RestartB.center = CGPointMake(view.frame.size.width / 2, view.frame.size.width / 7)
RestartB.setTitle("Restart", forState: UIControlState.Normal)
RestartB.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
RestartB.addTarget(self, action: Selector("Restart"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(RestartB)}
func Restart(){
self.view?.presentScene(Start(), transition: SKTransition.flipHorizontalWithDuration(0.3))RestartB.removeFromSuperview()
}
}
struct PhysicssCategory {
static let En: UInt32 = 1
static let Bullet: UInt32 = 2
static let Player: UInt32 = 3}
And the final document, Start.swift, is basically the next level, as long as it contains everything the same as the first.
-
The stage is being created, but you don't transfer the file on the basis of which it should be created. That's what you need in the arts:
func Restart(){
let scene = Start(fileNamed: "GameScene")! scene.scaleMode = .AspectFill self.view?.presentScene(scene, transition: SKTransition.flipHorizontalWithDuration(0.3)) RestartB.removeFromSuperview()
}