Я занимаюсь разработкой 2D-игры со Spritekit.Мой код работает нормально, если скорость вращения узла выше, чем 0,25 секунды, но ниже этого числа просто вызваны мои функции gameOver.
Эта игра никогда не вызывает функцию gameOver, если не указано значение spinSpeed минимум 0,25 или выше.
Интересно, как получить точный расчет для функций didBeginContact.
Кодирую ли я что-то не так?
Буду признателен за любую помощь.
Вот код:
class EasyScene: SKScene, UIGestureRecognizerDelegate, SKPhysicsContactDelegate {
var leftSection = SKSpriteNode()
var rightSection = SKSpriteNode()
var bottomSection = SKSpriteNode()
var colorStick = SKSpriteNode()
let leftSectionCat:UInt32 = 0x1 << 0
let rightSectionCat:UInt32 = 0x1 << 1
let bottomSectionCat:UInt32 = 0x1 << 2
let stickSectionCat:UInt32 = 0x1 << 3
let borderCat : UInt32 = 0x1 << 8
var isOverOnLeft : Bool = false
var isOverOnRight : Bool = false
var isOverOnBottom : Bool = false
var starterTouch : Int = 0
var enteredOnLeft : Bool = false
var enteredOnRight : Bool = false
var enteredOnBottom : Bool = false
var zRotationValuesOfStick : [CGFloat] = []
var spinSpeed : CGFloat = 0.45
var stickLeftEquals : [String : String ] = ["stickBlue" : "leftBlue","stickOrange" : "leftOrange","stickRed" : "leftRed", "stickGreen" : "leftGreen", "stickYellow" : "leftYellow","stickGray" : "leftGray"]
var stickRightEquals : [String : String ] = ["stickBlue" : "rightBlue","stickOrange" : "rightOrange","stickRed" : "rightRed", "stickGreen" : "rightGreen", "stickYellow" : "rightYellow","stickGray" : "rightGray"]
var stickBottomEquals : [String : String ] = ["stickBlue" : "bottomBlue","stickOrange" : "bottomOrange","stickRed" : "bottomRed", "stickGreen" : "bottomGreen", "stickYellow" : "bottomYellow","stickGray" : "bottomGray"]
var leftSectionTextureDatabase : [String] = ["leftBlue", "leftOrange", "leftRed", "leftGreen", "leftYellow", "leftGray"]
var selectedLeftSectionTexture : String = ""
var rightSectionTextureDatabase : [String] = ["rightBlue", "rightOrange", "rightRed", "rightGreen", "rightYellow", "rightGray"]
var selectedRightSectionTexture : String = ""
var bottomSectionTextureDatabase : [String] = ["bottomBlue", "bottomOrange", "bottomRed", "bottomGreen", "bottomYellow", "bottomGray"]
var selectedBottomSectionTexture : String = ""
var stickTextureDatabase : [String] = ["stickBlue", "stickOrange", "stickRed", "stickGreen", "stickYellow", "stickGray"]
var selectedStickTexture : [String] = []
var currentStickColor : String = ""
var pickedColors : [Int] = []
var colorStickColors : [Int] = []
var isGameOver : Bool = false
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
pick3Colors()
assignColorToColorStick()
createStickInBetween()
makeBordersAvaliable()
assignSectionColors()
pickStartingStickColor()
someFunction()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if starterTouch == 0 {
let randomStarter = Int(arc4random_uniform(2))
var move = SKAction()
if randomStarter == 0 {
move = SKAction.rotate(byAngle: 1, duration: TimeInterval(spinSpeed))
}else{
move = SKAction.rotate(byAngle: -1, duration: TimeInterval(spinSpeed))
}
let repeatIt = SKAction.repeatForever(move)
colorStick.run(repeatIt)
starterTouch = starterTouch + 1
}else{
checkIfItIsRightMove()
changeColorOfStick()
turnOpposite()
}
}
override func update(_ currentTime: TimeInterval) {
if zRotationValuesOfStick.count > 2{
zRotationValuesOfStick.removeFirst()
}
let radians:CGFloat = colorStick.zRotation
let degrees = radians * 180 / CGFloat(Double.pi)
zRotationValuesOfStick.append(degrees)
}
func gameOver(){
isGameOver = true
colorStick.removeAllActions()
leftSection.physicsBody?.collisionBitMask = borderCat | rightSectionCat | bottomSectionCat | stickSectionCat
rightSection.physicsBody?.collisionBitMask = borderCat | leftSectionCat | bottomSectionCat | stickSectionCat
bottomSection.physicsBody?.collisionBitMask = borderCat | leftSectionCat | rightSectionCat | stickSectionCat
colorStick.physicsBody?.collisionBitMask = borderCat | leftSectionCat | rightSectionCat | bottomSectionCat
colorStick.physicsBody?.pinned = false
leftSection.physicsBody?.pinned = false
rightSection.physicsBody?.pinned = false
bottomSection.physicsBody?.pinned = false
colorStick.physicsBody?.affectedByGravity = true
leftSection.physicsBody?.affectedByGravity = true
rightSection.physicsBody?.affectedByGravity = true
bottomSection.physicsBody?.affectedByGravity = true
leftSection.physicsBody?.allowsRotation = true
rightSection.physicsBody?.allowsRotation = true
bottomSection.physicsBody?.allowsRotation = true
leftSection.physicsBody?.isDynamic = true
rightSection.physicsBody?.isDynamic = true
bottomSection.physicsBody?.isDynamic = true
}
func increaseSpeed (incomingSpeed : CGFloat){
if incomingSpeed <= 0.15{
// spinSpeed = spinSpeed - 0.005
print("MAX!")
}else{
spinSpeed = spinSpeed - 0.010
}
}
func checkIfItIsRightMove (){
if isOverOnLeft == true && isOverOnRight == false && isOverOnBottom == false && isGameOver == false{
if stickLeftEquals["\(currentStickColor)"] == selectedLeftSectionTexture{
enteredOnLeft = false
if enteredOnLeft == false{
increaseSpeed(incomingSpeed: spinSpeed)
}
acceptedMove()
}else if stickLeftEquals["\(currentStickColor)"] != selectedLeftSectionTexture{
gameOver()
}
}
if isOverOnLeft == false && isOverOnRight == true && isOverOnBottom == false && isGameOver == false{
if stickRightEquals["\(currentStickColor)"] == selectedRightSectionTexture{
enteredOnRight = false
if enteredOnRight == false{
increaseSpeed(incomingSpeed: spinSpeed)
}
acceptedMove()
}else if stickRightEquals["\(currentStickColor)"] != selectedRightSectionTexture{
gameOver()
}
}
if isOverOnLeft == false && isOverOnRight == false && isOverOnBottom == true && isGameOver == false{
if stickBottomEquals["\(currentStickColor)"] == selectedBottomSectionTexture{
enteredOnBottom = false
if enteredOnBottom == false{
increaseSpeed(incomingSpeed: spinSpeed)
}
acceptedMove()
}else if stickBottomEquals["\(currentStickColor)"] != selectedBottomSectionTexture{
gameOver()
}
}
}
func didBegin(_ contact: SKPhysicsContact) {
let collusion:UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collusion == leftSectionCat | stickSectionCat && collusion != rightSectionCat | stickSectionCat && collusion != bottomSectionCat | stickSectionCat {
if stickLeftEquals["\(currentStickColor)"] == selectedLeftSectionTexture{
enteredOnLeft = true
}
isOverOnLeft = true
isOverOnRight = false
isOverOnBottom = false
}else if collusion != leftSectionCat | stickSectionCat && enteredOnLeft == true && collusion == rightSectionCat | stickSectionCat {
if stickLeftEquals["\(currentStickColor)"] == selectedLeftSectionTexture{
if isGameOver == false{
gameOver()
}
}
}
else if collusion != leftSectionCat | stickSectionCat && enteredOnLeft == true && collusion == bottomSectionCat | stickSectionCat {
if stickLeftEquals["\(currentStickColor)"] == selectedLeftSectionTexture{
if isGameOver == false{
gameOver()
}
}
}
if collusion == rightSectionCat | stickSectionCat && collusion != leftSectionCat | stickSectionCat && collusion != bottomSectionCat | stickSectionCat {
if stickRightEquals["\(currentStickColor)"] == selectedRightSectionTexture{
enteredOnRight = true
}
isOverOnLeft = false
isOverOnRight = true
isOverOnBottom = false
}else if collusion != rightSectionCat | stickSectionCat && enteredOnRight == true && collusion == leftSectionCat | stickSectionCat {
if stickRightEquals["\(currentStickColor)"] == selectedRightSectionTexture{
if isGameOver == false{
gameOver()
}
}
}
else if collusion != rightSectionCat | stickSectionCat && enteredOnRight == true && collusion == bottomSectionCat | stickSectionCat {
if stickRightEquals["\(currentStickColor)"] == selectedRightSectionTexture{
if isGameOver == false{
gameOver()
}
}
}
if collusion == bottomSectionCat | stickSectionCat && collusion != leftSectionCat | stickSectionCat && collusion != rightSectionCat | stickSectionCat {
if stickBottomEquals["\(currentStickColor)"] == selectedBottomSectionTexture{
enteredOnBottom = true
}
isOverOnLeft = false
isOverOnRight = false
isOverOnBottom = true
}else if collusion != bottomSectionCat | stickSectionCat && enteredOnBottom == true && collusion == leftSectionCat | stickSectionCat {
if stickBottomEquals["\(currentStickColor)"] == selectedBottomSectionTexture{
if isGameOver == false{
gameOver()
}
}
}
else if collusion != bottomSectionCat | stickSectionCat && enteredOnBottom == true && collusion == rightSectionCat | stickSectionCat {
if stickBottomEquals["\(currentStickColor)"] == selectedBottomSectionTexture{
if isGameOver == false{
gameOver()
}
}
}
}
func acceptedMove(){
sessionScore = sessionScore + 1
sessionScoreNumber.text = "\(sessionScore)"
let movement = SKAction.scale(to: 1.2, duration: 0.5)
let loop = SKAction.repeat(movement, count: 1)
let movement2 = SKAction.scale(to: 1, duration: 0.5)
let loop2 = SKAction.repeat(movement2, count: 1)
let seq = SKAction.sequence([loop, loop2])
sessionScoreNumber.run(seq)
}
func createStickInBetween(){
let colorStickTexture = SKTexture(imageNamed: selectedStickTexture[0])
colorStick = SKSpriteNode(texture: colorStickTexture)
colorStick.position.x = 211.671
colorStick.position.y = -400.74
colorStick.size.width = 31.524
colorStick.size.height = 124.74
colorStick.anchorPoint.x = 0.5
colorStick.anchorPoint.y = 0
colorStick.zPosition = 2
let centerPoint = CGPoint(x:colorStick.size.width / 2 - (colorStick.size.width * colorStick.anchorPoint.x), y:colorStick.size.height / 2 - (colorStick.size.height * colorStick.anchorPoint.y))
colorStick.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 15, height: colorStick.size.height), center: centerPoint)
colorStick.name = "denemeColor"
colorStick.physicsBody?.isDynamic = true
colorStick.physicsBody?.pinned = true
colorStick.physicsBody?.allowsRotation = true
colorStick.physicsBody?.affectedByGravity = false
colorStick.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(colorStick)
colorStick.physicsBody?.categoryBitMask = stickSectionCat
}
func makeBordersAvaliable (){
let borderSideTexture = SKTexture(imageNamed: "sideBorders")
borderSide = SKSpriteNode(texture: borderSideTexture)
borderSide.physicsBody = SKPhysicsBody(texture: borderSideTexture, size: CGSize(width: borderSide.size.width, height: borderSide.size.height))
borderSide.position.x = 207
borderSide.position.y = -368
borderSide.size.width = 414
borderSide.size.height = 736
borderSide.anchorPoint.x = 0.5
borderSide.anchorPoint.y = 0.5
borderSide.zPosition = 5
borderSide.name = "bs"
borderSide.physicsBody?.isDynamic = false
borderSide.physicsBody?.allowsRotation = false
borderSide.physicsBody?.affectedByGravity = false
borderSide.alpha = 0
self.addChild(borderSide)
borderSide.physicsBody?.categoryBitMask = borderCat
}
func someFunction(){
leftSection.physicsBody?.contactTestBitMask = stickSectionCat
bottomSection.physicsBody?.contactTestBitMask = stickSectionCat
rightSection.physicsBody?.contactTestBitMask = stickSectionCat
colorStick.physicsBody?.collisionBitMask = 0x1 << 15
}
func turnOpposite(){
if isGameOver != true{
colorStick.removeAllActions()
let lastValue = zRotationValuesOfStick.last!
let getTheLocation = zRotationValuesOfStick.count - 2
let theOneBeforeTheLast = zRotationValuesOfStick[getTheLocation]
let whatIsTheDifference = lastValue - theOneBeforeTheLast
if whatIsTheDifference > 0 {
let turnCounterClockWise = SKAction.rotate(byAngle: -1, duration: TimeInterval(spinSpeed))
let turnCounterClockWiseLoop = SKAction.repeatForever(turnCounterClockWise)
colorStick.run(turnCounterClockWiseLoop)
}else if whatIsTheDifference != 0 && isGameOver == false{
let turnClockWise = SKAction.rotate(byAngle: 1, duration: TimeInterval(spinSpeed))
let turnClockWiseLoop = SKAction.repeatForever(turnClockWise)
colorStick.run(turnClockWiseLoop)
}
}
}
func changeColorOfStick(){
if isGameOver != true {
let randomNumber = Int(arc4random_uniform(3))
var colorViaRandomNumber = selectedStickTexture[randomNumber]
while colorViaRandomNumber == currentStickColor {
let rn = Int(arc4random_uniform(3))
colorViaRandomNumber = selectedStickTexture[rn]
}
currentStickColor = colorViaRandomNumber
colorStick.texture = SKTexture(imageNamed: currentStickColor)
}
}
func pickStartingStickColor(){
let randomNumber = Int(arc4random_uniform(3))
currentStickColor = selectedStickTexture[randomNumber]
colorStick.texture = SKTexture(imageNamed: currentStickColor)
}
func assignSectionColors(){
selectedLeftSectionTexture = leftSectionTextureDatabase[pickedColors[0]]
selectedRightSectionTexture = rightSectionTextureDatabase[pickedColors[1]]
selectedBottomSectionTexture = bottomSectionTextureDatabase[pickedColors[2]]
let leftSectionTexture = SKTexture(imageNamed: selectedLeftSectionTexture)
leftSection = SKSpriteNode(texture: leftSectionTexture)
leftSection.physicsBody = SKPhysicsBody(texture: leftSectionTexture, size: CGSize(width: leftSection.size.width, height: leftSection.size.height))
leftSection.position.x = 141.352
leftSection.position.y = -372.722
leftSection.size.width = 172.16
leftSection.size.height = 257.85
leftSection.anchorPoint.x = 0.5
leftSection.anchorPoint.y = 0.5
leftSection.zPosition = 1
leftSection.name = "leftDeneme"
leftSection.physicsBody?.isDynamic = false
leftSection.physicsBody?.allowsRotation = false
leftSection.physicsBody?.affectedByGravity = false
leftSection.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(leftSection)
let rightSectionTexture = SKTexture(imageNamed: selectedRightSectionTexture)
rightSection = SKSpriteNode(texture: rightSectionTexture)
rightSection.physicsBody = SKPhysicsBody(texture: rightSectionTexture, size: CGSize(width: rightSection.size.width, height: rightSection.size.height))
rightSection.position.x = 298.386
rightSection.position.y = -373.071
rightSection.size.width = 141.908
rightSection.size.height = 257.152
rightSection.anchorPoint.x = 0.5
rightSection.anchorPoint.y = 0.5
rightSection.zPosition = 1
rightSection.physicsBody?.isDynamic = false
rightSection.physicsBody?.allowsRotation = false
rightSection.physicsBody?.affectedByGravity = false
rightSection.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(rightSection)
let bottomSectionTexture = SKTexture(imageNamed: selectedBottomSectionTexture)
bottomSection = SKSpriteNode(texture: bottomSectionTexture)
bottomSection.physicsBody = SKPhysicsBody(texture: bottomSectionTexture, size: CGSize(width: bottomSection.size.width, height: bottomSection.size.height))
bottomSection.position.x = 212.141
bottomSection.position.y = -515.223
bottomSection.size.width = 242.19
bottomSection.size.height = 93.846
bottomSection.anchorPoint.x = 0.5
bottomSection.anchorPoint.y = 0.5
bottomSection.zPosition = 1
bottomSection.physicsBody?.isDynamic = false
bottomSection.physicsBody?.allowsRotation = false
bottomSection.physicsBody?.affectedByGravity = false
bottomSection.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(bottomSection)
leftSection.physicsBody?.categoryBitMask = leftSectionCat
rightSection.physicsBody?.categoryBitMask = rightSectionCat
bottomSection.physicsBody?.categoryBitMask = bottomSectionCat
}
func assignColorToColorStick(){
selectedStickTexture.append(stickTextureDatabase[pickedColors[0]])
selectedStickTexture.append(stickTextureDatabase[pickedColors[1]])
selectedStickTexture.append(stickTextureDatabase[pickedColors[2]])
}
func pick3Colors(){
while pickedColors.count < 3 {
let randomNumber = Int(arc4random_uniform(6))
let howManyElements = pickedColors.count
if howManyElements == 0 {
pickedColors.append(randomNumber)
}else if howManyElements == 1 {
if randomNumber != pickedColors[0]{
pickedColors.append(randomNumber)
}
}else if howManyElements == 2 {
if randomNumber != pickedColors[0] && randomNumber != pickedColors[1] {
pickedColors.append(randomNumber)
}
}
}
}
}