didBeginContact,访问和更改对象

时间:2016-03-25 19:52:21

标签: ios swift sprite-kit

Noob in swift(和编程)在这里。我正在swift spriteKit中制作一个简单的突破游戏。它正在工作,但现在我想为某些块增加“健康”,但遇到了一些问题。我搜索过谷歌,但一无所获。

首先,这是我的班级:

class targets {

var _target: SKShapeNode;
var color: SKColor;
var life: Int;
var positionX: CGFloat = 100;
var positionY: CGFloat = 200;
var isDynamic: Bool = false;
var _name: String = "";


init(life:Int, target:SKShapeNode, name:String, color: SKColor) {
    self.life = life;
    self._target = target
    self._name = name;
    self.positionX = 100;
    self.positionY = 200;
    self.isDynamic = false;
    self.color = color;
}


func spawnTargets() ->SKShapeNode {

    let target = SKShapeNode(rectOfSize: CGSize(width: 20, height: 10));
    target.name=_name;
    target.fillColor=color;
    target.position=CGPoint(x: positionX, y: positionY);
    target.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 20, height: 10));
    target.physicsBody?.dynamic=isDynamic;
    target.physicsBody?.friction=0;
    target.physicsBody?.categoryBitMask=physicsCategory.target;
    target.physicsBody?.collisionBitMask = physicsCategory.ball;
    target.physicsBody?.contactTestBitMask=physicsCategory.ball;
    target.physicsBody?.usesPreciseCollisionDetection=true;
    return target;
}

}

我在GameScene类中创建了两个'目标'实例:

var target1: targets = targets(life: 1, target: SKShapeNode(rectOfSize: CGSize(width: 20, height: 10)), name: "1", color: SKColor.blackColor());
var target2: targets = targets(life: 2, target: SKShapeNode(rectOfSize: CGSize(width: 20, height: 10)), name: "2", color: SKColor.yellowColor());

产生块的功能:

func spawnTarget(){
    var count: Int = 0;
    target1.isDynamic=false;
    target1.positionX=100;
    target1.positionY=200;

    for(var i = 0; i<60; i++){
        if(count==20){ target1.positionY=target1.positionY+11; target1.positionX=100; count=0; }
        target1.positionX=target1.positionX+21;
        let targetOneLives = target1.spawnTargets();
        addChild(targetOneLives);
        count++;
    }
    count=0;

    target2.color=SKColor.yellowColor();
    target2.isDynamic=false;
    target2.positionX=100;

    target2.positionY=target1.positionY+11;
    for(var i = 0; i<60; i++){
        if(count==20){ target2.positionY=target2.positionY+11; target2.positionX=100; count=0; }
        target2.spawnTargets();
        target2.positionX=target2.positionX+21;
        let targetTwoLives = target2.spawnTargets();
        addChild(targetTwoLives);
        count++;
    }
 }

这是我的didBeginContact函数:

func didBeginContact(contact: SKPhysicsContact) {

    let firstBody = contact.bodyA;
    let secondBody = contact.bodyB;
    let firstNode = firstBody.node as! SKShapeNode;
    let secondNode = secondBody.node as! SKShapeNode;
    let node;         

    if(firstBody.categoryBitMask == physicsCategory.ball && secondBody.categoryBitMask == physicsCategory.target){
        node = secondNode; 
    }
    else if(firstBody.categoryBitMask == physicsCategory.target && secondBody.categoryBitMask == physicsCategory.ball){
        node = firstNode; 
    }
  //This is what i want to do!
  //node.life = node.life - 1
  //if(node.life == 0){ node.removeFromParent(); }
}

我想更改'targets'实例的.life属性。有人知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

建立这个的更好方法是继承SKShapeNode。每个目标应该是这个的一个实例。这样,在didBeginContact中,您可以将节点作为对象进行投射。

要在不更改体系结构的情况下修复问题,在实例化目标时,可以将它们存储到本地数组中。

var myArray: [targets] = [] // this should be in the scope of the class that all your logic is in
...
var target1: targets = targets(life: 1, target: SKShapeNode(rectOfSize: CGSize(width: 20, height: 10)), name: "1", color: SKColor.blackColor());
var target2: targets = targets(life: 2, target: SKShapeNode(rectOfSize: CGSize(width: 20, height: 10)), name: "2", color: SKColor.yellowColor());

myArray.append(target1)
myArray.append(target2)

然后在didBeginContact中,您可以遍历数组并检查节点是否与targets对象的节点匹配。

func didBeginContact(contact: SKPhysicsContact) {

    let firstBody = contact.bodyA;
    let secondBody = contact.bodyB;
    let firstNode = firstBody.node as! SKShapeNode;
    let secondNode = secondBody.node as! SKShapeNode;
    var node: SKShapeNode?
    // reference to target that was hit
    var myTarget: targets?
    // index of target for removal if needed
    var targetIndex: Int?

    if(firstBody.categoryBitMask == physicsCategory.ball && secondBody.categoryBitMask == physicsCategory.target){
        node = secondNode;
    }
    else if(firstBody.categoryBitMask == physicsCategory.target && secondBody.categoryBitMask == physicsCategory.ball){
        node = firstNode;
    }

    // bail out if not a match
    guard node != nil else { return }

    for i in 0..<myArray.count {
        let target = myArray[i]
        if target._target == node {
            // found
            myTarget = target
            targetIndex = i
            break;
        }
    }

    // verify we found the target
    guard myTarget != nil else { return }

    myTarget!.life = myTarget!.life - 1
    if myTarget!.life == 0 {
        // remove node from scene
        myTarget!._target.removeFromParent()
        // remove target from array
        myArray.removeAtIndex(targetIndex!)
    }

}