gpt4 book ai didi

ios - Spritekit 在场景中添加节点但不移动?

转载 作者:行者123 更新时间:2023-11-30 12:44:08 25 4
gpt4 key购买 nike

我想创建很少有云从右到左移动的背景。但是当我运行这个时,我只能看到背景上创建的云,但它们没有移动。

我有以下类(class)来创建云,然后将它们添加到场景中。最后一个函数 moveBGcloud() 会将云从右向左移动。

import Foundation
import SpriteKit

import SpriteKit
class BGCloud :SKSpriteNode{

private func shuffle(cloudsArray: [SKSpriteNode]) -> [SKSpriteNode] {

var shuffledArray = cloudsArray;

for i in 0..<shuffledArray.count {
let j = Int(arc4random_uniform(UInt32(shuffledArray.count - i))) + i;
if i == j {continue}
swap(&shuffledArray[i], &shuffledArray[j]);
}

return shuffledArray;
}

private func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum);
}

func createClouds() -> [SKSpriteNode] {
var clouds = [SKSpriteNode]();

let cloud1 = SKSpriteNode(imageNamed: "Cloud 1");
cloud1.name = "BGCloud1";
let cloud2 = SKSpriteNode(imageNamed: "Cloud 2");
cloud2.name = "BGCloud2";
let cloud3 = SKSpriteNode(imageNamed: "Cloud 3");
cloud3.name = "BGCloud3"
let darkCloud = SKSpriteNode(imageNamed: "Dark Cloud");
darkCloud.name = "BGDarkCloud";

cloud1.xScale = 0.3;
cloud1.yScale = 0.3;

cloud2.xScale = 0.4;
cloud2.yScale = 0.4;

cloud3.xScale = 0.5;
cloud3.yScale = 0.5;

darkCloud.xScale = 0.2;
darkCloud.yScale = 0.2;


clouds.append(cloud1);
clouds.append(cloud2);
clouds.append(cloud3);
clouds.append(darkCloud);

clouds = shuffle(cloudsArray: clouds);

return clouds;
}

func arrangeBGCloudsInScene(scene: SKScene, minX: CGFloat, maxX: CGFloat,minY: CGFloat, maxY: CGFloat,speed:CGFloat) {

var clouds = createClouds();

for i in 0..<clouds.count {

var randomX = CGFloat();
var randomY = CGFloat();

randomX = randomBetweenNumbers(firstNum: minX, secondNum: maxX);
randomY = randomBetweenNumbers(firstNum: minY, secondNum: maxY);


clouds[i].position = CGPoint(x: randomX, y: randomY);
clouds[i].zPosition = 2;


scene.addChild(clouds[i]);

}

}

func moveBGcloud( speed:CGFloat){

let cloudSpeed = speed
self.position.x -= cloudSpeed

print("postion",self.position.x)


if self.position.x < -324 {
self.position.x = 332

}

}

}

然后在GameplayScene类中:(背景场景类)

我添加了 BGCloud 类的实例

    private var cloud1: BGCloud?
private var cloud2: BGCloud?
private var cloud3: BGCloud?
private var darkCloud:BGCloud?

private let BGCloud1Speed = 0.5
private let BGCloud2Speed = 0.7
private let BGCloud3Speed = 0.8

然后添加函数BGCloudController.arrangeBGCloudsInScene来初始化背景

private func initializeGame() {

BGCloudController.arrangeBGCloudsInScene(scene: self.scene!, minX: minX, maxX: maxX, minY: minY, maxY: maxY, speed: CGFloat(BGCloud1Speed))
getBackgrounds();

}

从场景中获取云:

private func getBackgrounds() {

cloud1 = self.childNode(withName: "BGCloud1") as? BGCloud!
cloud2 = self.childNode(withName: "BGCloud2") as? BGCloud!
cloud3 = self.childNode(withName: "BGCloud3") as? BGCloud!

}

新增CreateBGClouds函数,在更新函数中将背景云从右向左移动

override func update(_ currentTime: TimeInterval) {

createBGClouds()

}



private func createBGClouds(){

cloud1?.moveBGcloud(speed: CGFloat(BGCloud1Speed)) //only add cloud1 for testing
print("function called")

}

当我运行游戏时,我可以看到四个背景云随机出现在场景中,但它们没有移动。看起来像

BGCloud 类中的 moveBGcloud() 函数未被调用。 Cloud1,2,3都是nil取不到值,为什么?而且我没有使用场景编辑器。

你能帮我检查一下问题是什么或者我错过了什么吗?先感谢您。

最佳答案

正如我们在上面的评论中发现的那样,云实例化中存在某种拼写错误。

func createClouds() 中的分配更改为 BGCloud 可以修复该问题:

    let cloud1 = BGCloud(imageNamed: "Cloud 1");
cloud1.name = "BGCloud1";
let cloud2 = BGCloud(imageNamed: "Cloud 2");
cloud2.name = "BGCloud2";
let cloud3 = BGCloud(imageNamed: "Cloud 3");
cloud3.name = "BGCloud3"

关于ios - Spritekit 在场景中添加节点但不移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41854752/

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