gpt4 book ai didi

ios - didBeginContact,访问和更改对象的变量

转载 作者:行者123 更新时间:2023-11-30 13:34:34 24 4
gpt4 key购买 nike

这里是 swift(和编程)菜鸟。我正在 swift spriteKit 中制作一个简单的突破游戏。它正在工作,但现在我想为某些 block 添加“健康”,但遇到了一些问题。我搜索过谷歌,但什么也没找到。

首先,这是我的类(class):

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());

生成 block 的函数:

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 属性。有人知道怎么做吗?

最佳答案

更好的构建方法是子类化 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!)
}

}

关于ios - didBeginContact,访问和更改对象的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36227028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com