Mon code est le suivant
class GameScene: SKScene, SKPhysicsContactDelegate {
var player: SKSpriteNode!
var player2: SKSpriteNode!
var initialplayerposition : CGPoint!
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx:0, dy:0)
self.physicsWorld.contactDelegate = self
addplayer()
addRow(type: .threeS)
}
func addRandomRow () {
let randomNumber = Int(arc4random_uniform(6))
switch randomNumber {
case 0 :
addRow(type: RowType(rawValue:0)!)
break
case 1:
addRow (type: RowType(rawValue: 1)!)
case 2 :
addRow(type: RowType(rawValue:2)!)
break
case 3 :
addRow(type: RowType(rawValue:3)!)
break
case 4 :
addRow(type: RowType(rawValue:4)!)
break
case 5 :
addRow(type: RowType(rawValue:5)!)
break
default:
break
}
}
var lastTimeIntervall = TimeInterval ()
var lastTimeYield = TimeInterval()
func updateWithTimeSinceLastUpdate (timeSinceLastUpdate:CFTimeInterval){
lastTimeYield += timeSinceLastUpdate
if (lastTimeYield > 0.6){
lastTimeYield = 0
addRandomRow()
}
}
override func update(_ currentTime: TimeInterval) {
var timeSinceLastUpdate = currentTime - lastTimeIntervall
lastTimeIntervall = currentTime
if timeSinceLastUpdate > 1 {
timeSinceLastUpdate = 1 / 60
lastTimeIntervall = currentTime
}
updateWithTimeSinceLastUpdate (timeSinceLastUpdate: timeSinceLastUpdate)
// Called before each frame is rendered
}
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.node?.name == "Player" {
print("gameover")
}
}}
et voici mes éléments créés dans un autre fichier
func addplayer() {
player = SKSpriteNode(color: UIColor.red, size:CGSize(width:50, height:50))
player.position = CGPoint(x:self.size.width / 2 , y: 350)
player.name = "PLAYER"
player.physicsBody?.isDynamic = false
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
player.physicsBody?.categoryBitMask = UInt32(CollisionBitMask.Player)
player.physicsBody?.collisionBitMask = 0
player.physicsBody?.contactTestBitMask = UInt32(CollisionBitMask.Obstacle)
et voici le code d'obstacle
func addObstacle(type:ObstacleType) -> SKSpriteNode {
let Obstacle = SKSpriteNode(color: UIColor.white, size: CGSize(width:0, height:30))
Obstacle.name = "OBSTACLE"
Obstacle.physicsBody?.isDynamic = true
switch type {
case .Small:
Obstacle.size.width = self.size.width * 0.2
break
case.Medium:
Obstacle.size.width = self.size.width * 0.35
break
case .Large:
Obstacle.size.width = self.size.width * 0.75
break
}
Obstacle.position = CGPoint(x:0, y : self.size.height + Obstacle.size.height)
Obstacle.physicsBody = SKPhysicsBody(rectangleOf: Obstacle.size)
Obstacle.physicsBody?.categoryBitMask = UInt32(CollisionBitMask.Obstacle)
Obstacle.physicsBody?.collisionBitMask = 0
return Obstacle}