jeudi 9 avril 2015

Swift ScoreLabel didBeginContact Xcode 6


Vote count:

0




I'm trying to make it so when my invisible wall "ground3" collides with the "walls" that are being generated coming towards it a score gets added + 1 every time it collides with the invisible wall "ground3". Am I doing something wrong or is there anything I can do to make that possible? I'm pretty new to this coding.



import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var movingGround: CSMovingGround!
var hero: CSHero!
var cloudGenerator: CSCloudGenerator!
var wallGenerator: CSWallGenerator!
var wall: CSWall!
var scoreLabel = SKLabelNode()
var score: Int = 0
var isStarted = false

var heroCategory: UInt32 = 1<<1
var wallCategory: UInt32 = 1<<2
var groundCategory: UInt32 = 1<<2
var invisCategory: UInt32 = 1<<1
let walls = CSWall()



override func didMoveToView(view: SKView) {
backgroundColor = UIColor(red: 159.0/255.0, green: 201.0/255, blue: 244.0/255.0, alpha: 1.0)

/*
let backgroundTexture = SKTexture(imageNamed: "background.png")
let backgroundImage = SKSpriteNode(texture: backgroundTexture, size: view.frame.size)
backgroundImage.position = view.center
addChild(backgroundImage)
*/



// add ground
movingGround = CSMovingGround(size: CGSizeMake(view.frame.width, kCSGroundHeight))
movingGround.position = CGPointMake(0, view.frame.size.height/2)

self.addChild(movingGround)

// add hero
hero = CSHero()
hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
hero.physicsBody = SKPhysicsBody(rectangleOfSize: hero.size)
hero.physicsBody?.dynamic = true
hero.physicsBody?.allowsRotation = false
hero.physicsBody!.collisionBitMask = heroCategory | wallCategory
hero.physicsBody!.contactTestBitMask = wallCategory | heroCategory | groundCategory
self.addChild(hero)
hero.breathe()


// add cloud generator
cloudGenerator = CSCloudGenerator(color: UIColor.clearColor(), size: view.frame.size)
cloudGenerator.position = view.center
addChild(cloudGenerator)
cloudGenerator.populate(7)
cloudGenerator.startGeneratingWithSpawnTime(5)

// add wall generator

wallGenerator = CSWallGenerator(color: UIColor.clearColor(), size: view.frame.size)
wallGenerator.position = view.center
wallGenerator.physicsBody = SKPhysicsBody(edgeLoopFromRect : wallGenerator.frame)
wallGenerator.physicsBody?.dynamic = false
wallGenerator.physicsBody!.collisionBitMask = wallCategory | heroCategory

self.addChild(wallGenerator)


let ground1 = SKSpriteNode(color: UIColor.clearColor(), size: CGSizeMake(view.frame.size.width, 20))
ground1.position = view.center
ground1.physicsBody = SKPhysicsBody(rectangleOfSize: ground1.size)
ground1.physicsBody!.dynamic = false
ground1.physicsBody!.affectedByGravity = false
ground1.physicsBody!.categoryBitMask = groundCategory
ground1.physicsBody!.collisionBitMask = groundCategory | heroCategory
self.addChild(ground1)

let ground2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(view.frame.size.width, 20))
ground2.position = CGPointMake(284, 98)
ground2.physicsBody = SKPhysicsBody(rectangleOfSize: ground2.size)
ground2.physicsBody!.dynamic = false
ground2.physicsBody!.affectedByGravity = false
ground2.physicsBody!.categoryBitMask = groundCategory
ground2.physicsBody!.collisionBitMask = groundCategory | heroCategory
self.addChild(ground2)

let ground3 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(5, 500))
ground3.position = CGPointMake(100, 100)
ground3.physicsBody = SKPhysicsBody(rectangleOfSize: ground3.size)
ground3.physicsBody!.dynamic = false
ground3.physicsBody!.affectedByGravity = false
ground3.physicsBody!.categoryBitMask = invisCategory
ground3.physicsBody!.collisionBitMask = invisCategory | heroCategory
self.addChild(ground3)

physicsWorld.contactDelegate = self

scoreLabel.fontName = "score"
scoreLabel.fontColor = UIColor.blackColor()
scoreLabel.fontSize = 40
scoreLabel.text = "\(score)"
scoreLabel.position = CGPoint(x: 50, y: 275)
self.addChild(scoreLabel)



}


func start() {
isStarted = true
hero.stop()
hero.startRunning()
movingGround.start()

}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if !isStarted {
start()
score = 0
scoreLabel.text = "0"
wallGenerator.startGeneratingWallsEvery(0.5)
} else {
hero.flip()
}


}


func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
var thirdBody = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}

if (thirdBody.categoryBitMask & UInt32(invisCategory)) != 0 && (secondBody.categoryBitMask & UInt32(wallCategory)) != 0 {
score++
scoreLabel.text = "\(score)"
}

if (firstBody.categoryBitMask & UInt32(heroCategory)) != 0 && (secondBody.categoryBitMask & UInt32(wallCategory)) != 0 {
wallGenerator.removeFromParent()
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(scene, transition: reveal)
}


}



override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */

}
}


Code to CSWall (Wall)



import Foundation
import SpriteKit

class CSWall: SKSpriteNode {

let WALL_WIDTH: CGFloat = 30.0
let WALL_HEIGHT: CGFloat = 50.0
let WALL_COLOR = UIColor.blackColor()

override init() {
super.init(texture: nil, color: WALL_COLOR, size: CGSizeMake(WALL_WIDTH, WALL_HEIGHT))
startMoving()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func startMoving() {
let moveLeft = SKAction.moveByX(-300, y: 0, duration: 1)
runAction(SKAction.repeatActionForever(moveLeft))
}


}


Code that Generates the Wall in game



import SpriteKit

class CSWallGenerator: SKSpriteNode {

var generationTimer: NSTimer?

func startGeneratingWallsEvery(seconds: NSTimeInterval) {
generationTimer = NSTimer.scheduledTimerWithTimeInterval(seconds, target: self, selector: "generateWall", userInfo: nil, repeats: true)


}

func generateWall() {
var scale: CGFloat
let rand = arc4random_uniform(2)
if rand == 0 {
scale = -1.0
} else {
scale = 1.0
}

let wall = CSWall()
wall.position.x = size.width/2 + wall.size.width/2
wall.position.y = scale * (kCSGroundHeight/2 + wall.size.height/2)
wall.physicsBody = SKPhysicsBody(rectangleOfSize: wall.size)
wall.physicsBody?.dynamic = false
addChild(wall)

}

}


asked 40 secs ago







Swift ScoreLabel didBeginContact Xcode 6

Aucun commentaire:

Enregistrer un commentaire