gpt4 book ai didi

swift - 如何在SpriteKit中制作移动障碍物

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

我刚刚完成了一个移动应用游戏的构建,现在想要尝试在游戏中左右移动障碍物(本例中为云)。有什么建议吗?

    func createClouds() -> [SKSpriteNode]{


var clouds = [SKSpriteNode]();

for _ in 0 ..< 2 {

let cloud1 = SKSpriteNode(imageNamed: "Cloud 1")
cloud1.name = "1"
let cloud2 = SKSpriteNode(imageNamed: "Cloud 2")
cloud2.name = "2"
let cloud3 = SKSpriteNode(imageNamed: "Cloud 3")
cloud3.name = "3"
let darkCloud = SKSpriteNode(imageNamed: "Dark Cloud")
darkCloud.name = "Dark Cloud"




cloud1.xScale = 0.9
cloud1.yScale = 0.9

cloud2.xScale = 0.9
cloud2.yScale = 0.9

cloud3.xScale = 0.9
cloud3.yScale = 0.9

darkCloud.xScale = 0.9
darkCloud.yScale = 0.9

cloud1.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: cloud1.size.width - 5, height: cloud1.size.height - 6));
cloud1.physicsBody?.affectedByGravity = false;
cloud1.physicsBody?.restitution = 0;
cloud1.physicsBody?.categoryBitMask = ColliderType.Cloud;
cloud1.physicsBody?.collisionBitMask = ColliderType.Player;

cloud2.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: cloud2.size.width - 5, height: cloud2.size.height - 6));
cloud2.physicsBody?.affectedByGravity = false;
cloud2.physicsBody?.restitution = 0;
cloud2.physicsBody?.categoryBitMask = ColliderType.Cloud;
cloud2.physicsBody?.collisionBitMask = ColliderType.Player;


cloud3.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: cloud3.size.width - 5, height: cloud3.size.height - 6));
cloud3.physicsBody?.affectedByGravity = false;
cloud3.physicsBody?.restitution = 0;
cloud3.physicsBody?.categoryBitMask = ColliderType.Cloud;
cloud3.physicsBody?.collisionBitMask = ColliderType.Player;

darkCloud.physicsBody = SKPhysicsBody(rectangleOf: darkCloud.size)
darkCloud.physicsBody?.affectedByGravity = false;
darkCloud.physicsBody?.categoryBitMask = ColliderType.DarkCloudAndCollectables;
darkCloud.physicsBody?.collisionBitMask = ColliderType.Player;

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

}

clouds = shuffle(cloudsArray: clouds);

return clouds


}

func arrangeCloudsInScene(scene: SKScene, distanceBetweenClouds: CGFloat, center: CGFloat, minX: CGFloat, maxX: CGFloat, player: Player, initialClouds: Bool) {

var clouds = createClouds()

if initialClouds{

while(clouds[0].name == "Dark Cloud") {
clouds = shuffle(cloudsArray: clouds);

}
}


var positionY = CGFloat();

if initialClouds {
positionY = center - 100;
} else {
positionY = lastCloudPositionY;
}

var random = 0;

for i in 0...clouds.count - 1 {

var randomX = CGFloat();

if random == 0 {
randomX = randomBetweenNumbers(firstNum: center + 90, secondNum: maxX);
random = 1;
} else if random == 1{
randomX = randomBetweenNumbers(firstNum: center - 90, secondNum: minX)
random = 0;
}

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

if !initialClouds {

if Int(randomBetweenNumbers(firstNum: 0, secondNum: 7)) >= 3 {

if clouds[i].name != "Dark Cloud" {
let collectable = collectableController.getCollectable();
collectable.position = CGPoint(x: clouds[i].position.x, y: clouds[i].position.y + 60);

scene.addChild(collectable);

}

}

}

如果需要,我可以提供更多代码示例,但我确信上面的示例有意义。提前谢谢大家!

最佳答案

初学者步骤:

使用SKAction.moveToSKAction.sequenceSKAction.repeatForever移动云

前进步骤:

GameplayKitAgentsGoals 结合使用,让您的云彩沿着路径移动,让您能够灵活地了解云彩的移动方式

关于swift - 如何在SpriteKit中制作移动障碍物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453090/

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