快速跳转方法碰撞对象和地面

时间:2015-09-02 13:12:43

标签: ios swift sprite-kit game-physics skphysicsbody

我正在开发一款iPhone游戏。基本上如果英雄遇到了障碍就放弃了。现在我想为我的英雄添加跳跃能力。但是当我使用“physicsBody = SKPhysicsBody(rectangleOfSize:size)”作为地面时,一旦游戏开始并且英雄出现在地面上“func didBeginContact(contact:SKPhysicsContact){ 游戏结束() “方法让游戏结束。这是我的英雄,地面和游戏的实现。你们能帮助我让英雄跳跃并降落在地面上。提前致谢。

//  AKHero.swift
//  Run Hero

  import Foundation
  import SpriteKit
  class AKHero: SKSpriteNode{

var body:SKSpriteNode!
var arm: SKSpriteNode!
var leftFoot: SKSpriteNode!
var rightFoot: SKSpriteNode!
var leftpupil: SKSpriteNode!
var rightpupil: SKSpriteNode!

var isUpsideDown = false

init(){
    let size = CGSizeMake(35, 45)
    super.init(texture: nil, color: UIColor.clearColor(), size: size)

    loadAppearance()
    loadPhysicsBoadyWithsize(size)

}

   func loadPhysicsBoadyWithsize(size: CGSize){

    physicsBody = SKPhysicsBody(rectangleOfSize: size)

    physicsBody?.categoryBitMask = heroCategory
    physicsBody?.contactTestBitMask = obstacleCategory
    physicsBody?.usesPreciseCollisionDetection = true
    physicsBody?.affectedByGravity = false
}


   func jump(){

    self.physicsBody?.applyImpulse(CGVectorMake(0, 15))
    physicsBody?.affectedByGravity = true


}
//  GameScene.swift
//  Run Hero

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var movingGround: AKMovingGround!
var hero: AKHero!
var cloudGenerator: AKCloudGenerator!
var obstacleGenerator: AKObstacleGenerator!
var coinGenerator: AKCoinGenerator!
var isStarted = false
var isGameOver = false

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

 func addMovingGround(){
     movingGround = AKMovingGround(size: CGSizeMake(view!.frame.width, kAKGroundHeight))
    movingGround.position = CGPointMake(0, view!.frame.size.height/2)
    addChild(movingGround)

}

//add hero
func addHero(){
    hero = AKHero()
    hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
    addChild(hero)
    hero.breathe()
}
func addPhysicsWorld() {
    physicsWorld.contactDelegate = self

}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    if isGameOver {

        restart()
    }

    else if !isStarted {

        start()

    }
    else{
       hero.flipHero()
      //hero.jump()

    }
}


// MARK: - SKPhysicsContactDelegate
func didBeginContact(contact: SKPhysicsContact) {

    if !isGameOver{
    println("didBeginContact")
    gameOver()
    }
}
//
//  AKMovingGround.swift
//  Run Hero
//
//  Created by Alican Karamil on 24/08/15.
//  Copyright (c) 2015 Alican Karamil. All rights reserved.
//

import Foundation
import SpriteKit
class AKMovingGround: SKSpriteNode{

    let NUMBER_OF_SEGMENTS = 20
    let COLOR_ONE = UIColor(red: 88.0/255.0, green: 148.0/255, blue: 87.0/255.0, alpha: 1.0)
    let COLOR_TWO = UIColor(red: 120.0/255.0, green:195.0/255.0, blue: 118.0/255.0, alpha: 1.0)

    init (size: CGSize){
        super.init(texture: nil, color: UIColor.brownColor(), size: CGSizeMake(size.width*2, size.height))
        anchorPoint = CGPointMake(0, 1.5)
        //anchorPoint = CGPointMake(0, 0.5)

        for var i = 0; i < NUMBER_OF_SEGMENTS; i++ {
            var segmentColor: UIColor!
            if i % 2 == 0{
            segmentColor=COLOR_ONE
            }else{
            segmentColor=COLOR_TWO
            }
            let segment = SKSpriteNode(color: segmentColor, size: CGSizeMake(self.size.width/CGFloat(NUMBER_OF_SEGMENTS), self.size.height))
            segment.anchorPoint = CGPointMake(0.0, 0.5)
            segment.position = CGPointMake(CGFloat(i)*segment.size.width, 0)
            addChild(segment)
        }

    }

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


    func start(){
        let moveleft = SKAction.moveByX(-frame.size.width/2, y: 0, duration: 1.0)
        let resetPosition = SKAction.moveToX(0, duration: 0.0)
        let moveSequence = SKAction.sequence([moveleft, resetPosition])
        //runAction(moveSequence, completion: nil)
        runAction(SKAction.repeatActionForever(moveSequence))

    }

    func stop(){

    removeAllActions()
   }


 }

1 个答案:

答案 0 :(得分:1)

您的问题是,didBeginContact()尚未分配特定联系人,因此任何所有的联系人都会被渲染到该功能中,包括玩家与地面之间。< / p>

要解决此问题,您必须创建特定的categoryBitMasks。有关详细信息,请查看Apple Docs。当您完成此操作后,您需要在播放器上指定要检测哪些categoryBitMasks。这样,如果它撞到地面,没有任何事情发生,但如果它遇到障碍,游戏结束。为此,您必须指定播放器physicsBody?.categoryBitMask,然后设置其physicsBody?.contactTestBitMask及其physicsBody?.collisionBitMask

didBeginContact()中,使用if-statements指定要查找的联系人。试着看看this Youtube video是否对您有所帮助。或者也许是this one