将对象移动到屏幕中心

时间:2015-12-13 19:29:15

标签: ios swift sprite-kit

我现在有编码问题。我有两个墙/块移动到屏幕的中心,但是当它们到达中心时,一旦到达中心,它们就不会停止。如何让它们在到达/触碰彼此/中心后停止。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */
    for touch in touches {
        let location = touch.locationInNode(self)
        if (playButton.containsPoint(location))
        {
            playButton.removeFromParent()
            title.removeFromParent()
            //Wall Timer
            wallTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: ("walls"), userInfo: nil, repeats: false)
            //Physics World
            self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
    } 
   else 
      {}        
  }
}

func didBeginContact(contact: SKPhysicsContact)
 {
    if contact.bodyA.node != nil && contact.bodyB.node != nil {
        let firstBody = contact.bodyA.node as! SKSpriteNode
        let secondBody = contact.bodyB.node as! SKSpriteNode
        if ((firstBody.name == "leftWall") && (secondBody.name == "rightWall")) {
            collisionWalls(firstBody, rightWall: secondBody)
          }
        else if ((firstBody.name == "rightWall") && (secondBody.name == "leftWall")) {
            collisionWalls(secondBody, rightWall: firstBody)
         }
    }
}

func collisionWalls(leftWall : SKSpriteNode, rightWall : SKSpriteNode) 
{
    leftWall.removeAllActions()
    rightWall.removeAllActions()
}


func walls() {
    let leftWall = SKSpriteNode(imageNamed: "blue background1")
    let rightWall = SKSpriteNode(imageNamed: "blue background1")
    //Left Wall Code
    leftWall.size = CGSizeMake(300, 90)
    leftWall.position = CGPoint(x: scene!.frame.width / 6, y: scene!.frame.height / 6)
    leftWall.zPosition = 1.0
    leftWall.physicsBody = SKPhysicsBody(rectangleOfSize: leftWall.size)
    leftWall.physicsBody?.affectedByGravity = false
    leftWall.physicsBody?.dynamic = false
    leftWall.name = "leftWall"

    leftWall.physicsBody?.categoryBitMask = PhysicsCatagory.leftWall
    leftWall.physicsBody?.collisionBitMask = PhysicsCatagory.rightWall
    leftWall.physicsBody?.contactTestBitMask = PhysicsCatagory.rightWall
    leftWall.removeFromParent()
    self.addChild(leftWall)

    //Right Wall Code
    rightWall.size = CGSizeMake(300, 90)
    rightWall.position = CGPointMake(self.size.width * 0.87, scene!.frame.height / 6)
    rightWall.zPosition = 1.0
    rightWall.physicsBody = SKPhysicsBody(rectangleOfSize: rightWall.size)
    rightWall.physicsBody?.affectedByGravity = false
    rightWall.physicsBody?.dynamic = false
    rightWall.name = "rightWall"

    rightWall.physicsBody?.categoryBitMask = PhysicsCatagory.rightWall
    rightWall.physicsBody?.collisionBitMask = PhysicsCatagory.leftWall
    rightWall.physicsBody?.contactTestBitMask = PhysicsCatagory.leftWall
    rightWall.removeFromParent()
    self.addChild(rightWall)

    //Right and Left Wall actions
    let moveLeft = SKAction.moveToX(scene!.frame.width * 1.35, duration: 5.0)
    let moveRight = SKAction.moveToX(self.size.width * -0.59, duration: 5.0)
    leftWall.runAction(SKAction.sequence([moveLeft]))
    rightWall.runAction(SKAction.sequence([moveRight]))
}

2 个答案:

答案 0 :(得分:0)

你是否测试了func didBeginContact(contact: SKPhysicsContact)是否被解雇了?

如果没有,那么这是我曾经遇到的问题,直到我意识到这个类(包含SKScene}必须设置为contactdelegate self.contactdelegate = self之后,它应该可以正常工作。
(如果不是,请说明)

或者在您的情况下:您应该确保import {bootstrap} from '../../node_modules/angular2/bootstrap.js';

答案 1 :(得分:0)

SKActions并未在屏幕中间停止。试试这个:

let moveLeft = SKAction.moveToX(scene!.frame.width / 2, duration: 5.0)
let moveRight = SKAction.moveToX(scene!.frame.width / 2, duration: 5.0)