2016-03-25 4 views
0

Noobはここで迅速に(プログラミング)。私は素早くspriteKitで簡単なブレークアウトゲームを作っています。それは働いていますが、私はブロックのいくつかに "健康"を加えたいと思っていますが、いくつかの問題がありました。私はGoogleを検索しましたが、何も見つかりませんでした。didBeginオブジェクトのアクセス、変更、変数の変更

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クラスの 'ターゲット' の2つのインスタンスを作っ

:ブロックを起動する

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プロパティを変更したいと思います。誰でもこれを行う方法を知っていますか?

+0

問題はなんですか。 'didBeginContact'の最後にあなたのコードブロックを入れるとどうなりますか? – Chris

+0

さて、「SKPhysicsBodyにはlifeというプロパティはありません」というようなものがあります。しかし、それはポイントではない、ポイントは私がオブジェクトの内部変数にアクセスしたいです – OlaStein

答えて

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!) 
    } 

} 
+0

あなたの返信ありがとう! 'guard myTarget!= nil else {return}'のようになります。 – OlaStein

+0

その行にブレークポイントを設定し、 'myArray'を検査して2つのオブジェクトが含まれていることを確認します。あなたがそれをしている間、あなたのオブジェクトのシェイプノードを調べ、それらのメモリアドレスを 'target'のアドレスと比較して、それらが一致するかどうかを調べます。 – Chris

+0

配列にオブジェクトが含まれていて、メモリアドレスが一致します – OlaStein

関連する問題