我正在尝试使用相同类型/父级的多个精灵节点,但是当我尝试生成另一个节点时,我收到错误。 'NSInvalidArgumentException',原因:'尝试添加已有父级的SKNode'
这是我的代码:
<form action="" method="post" enctype="multipart/form-data">
<table align="center" width="700" >
<tr align="center">
<td colspan="5"><h2>SHOPPING CART</h2></td>
</tr>
<tr align="center">
<th>Remove</th>
<th>Product (s)</th>
<th>Quantity</th>
<th> Price</th>
<th> Total Price</th>
</tr>
<?php
global $con;
$total = 0;
$ip = getIp();
$sel_price = "select * from cart where ip_add='$ip'";
$run_price = mysqli_query($con,$sel_price);
while($p_price = mysqli_fetch_array($run_price)){
$pro_id = $p_price['p_id'];
$pro_price = "select * from products where product_id = '$pro_id'";
$run_pro_price = mysqli_query($con, $pro_price);
while($pp_price = mysqli_fetch_array($run_pro_price)){
$product_price = array($pp_price['product_price']);
$product_id = $pp_price['product_id'];
$product_title = $pp_price['product_title'];
$product_image = $pp_price['product_image'];
$single_price = $pp_price['product_price'];
$values = array_sum($product_price);
$total += $values;
?>
<tr align = "center">
<td><input type="checkbox" name="remove[]" value=" <?php global $con; echo $pro_id; ?>"/></td>
<td><span style="color: white;"><?php echo $product_title; ?></span>
<br>
<img src="admin_area/product_images/<?php echo $product_image; ?>" width="100px" height="100px"></td>
<td><input type="number" size="4" name="qty" /></td>
<?php
global $con;
global $Stotal;
$Stotal = $single_price;
if(isset($_POST['update_quantity'])){
$qty = $_POST['qty'];
$update_qty = "update cart set qty='$qty' ";
$run_qty = mysqli_query($con, $update_qty);
$Stotal = $single_price * $qty;
}
?>
<td><?php echo "KSh. " . $single_price; ?></td>
<td><?php echo "KSh. " . $Stotal; ?></td>
</tr>
<?php }} ?>
<tr align="right">
<td colspan="5"><b>Total:</b> <?php echo "KSh. " . $total; ?></td>
</tr>
<tr align="center">
<td><input type="submit" name="update_cart" value="Update Cart" /></td>
<td><input type="submit" name="continue" value="Continue Shopping" /></td>
<td><input type="submit" name="update_quantity" value="Update Quantity" /></td>
<td><button><a href="checkout.php" style="text-decoration: none; color: black;"> Checkout </a></button></td>
</tr>
</table>
</form>
<?php
global $con;
$ip = getIp();
if(isset($_POST['update_cart'])){
foreach($_POST['remove'] as $remove_id){
$delete_pro = "delete from cart where p_id = '$remove_id' and ip_add='$ip'";
$run_delete = mysqli_query($con, $delete_pro);
if($run_delete){
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
if(isset($_POST['continue'])){
echo "<script>window.open('index.php','_self')</script>";
}
?>
答案 0 :(得分:1)
您需要实例化另一个SKSpriteNode
。在现有代码中,您创建一个火球并将其添加到场景中;当你尝试再次添加相同的火球时,你的程序会崩溃。
首先,删除您的let fireball = SKSpriteNode...
行。将其移至spawnFireball()
方法内部,如下所示:
func spawnFireball(point: CGPoint) {
let fireball = SKSpriteNode(imageNamed: "fireball")
//Insert all customization here (your existing code should mostly work)
self.addChild(fireball)
}
因为fireball
变量是局部变量,所以每次调用函数时都可以实例化一个新变量。现在,只需将update()
方法更改为正确使用enumerateChildrenWithName()
,方法是将每self.fireball
更改为node
。
这样,代码将遍历当前在场的每个现有火球,而不是当前的代码,只允许您创建一个火球。