gpt4 book ai didi

swift - 我怎样才能在我的游戏场景中生成一个随机的(CLASS SPRITENODE)敌人

转载 作者:可可西里 更新时间:2023-11-01 02:18:25 30 4
gpt4 key购买 nike

我是新来的快速编程。我想使用类节点将敌人生成到随机位置。我试图搜索随机生成敌人的代码,但它似乎与我的代码无关。

这是我搜索随机生成的代码。

import SpriteKit

class GameScene: SKScene {

let player = SKSpriteNode(imageNamed:"spacemonkey_fly02")

override func didMoveToView(view: SKView) {

player.position = CGPoint(x:frame.size.width * 0.1, y: frame.size.height * 0.5)
addChild(player)
backgroundColor = SKColor.blackColor()

runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(spawnEnemy),
SKAction.waitForDuration(1.0)])))
}

func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(#min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}

func spawnEnemy() {
let enemy = SKSpriteNode(imageNamed: "boss_ship")
enemy.name = "enemy"
enemy.position = CGPoint(x: frame.size.width, y: frame.size.height * random(min: 0, max: 1))
addChild(enemy)
}

}

这是我制作的类,我想在我的游戏场景中随机生成

import SpriteKit


class Meteor: SKSpriteNode, GameSprite {
var textureAtlas:SKTextureAtlas = SKTextureAtlas(named:"meteor.atlas")
var meteorAnimation = SKAction()

func spawn(parentNode: SKNode, position: CGPoint, size: CGSize = CGSize(width: 30, height: 30)) {
parentNode.addChild(self)
meteorRotation()

self.size = size
self.position = position

self.texture = textureAtlas.textureNamed("meteor-1.png")
self.physicsBody = SKPhysicsBody(texture: textureAtlas.textureNamed("meteor-1.png"), size: size)

self.physicsBody = SKPhysicsBody(circleOfRadius: size.width / 2)

self.physicsBody?.affectedByGravity = true
self.runAction(meteorAnimation)
}

func meteorRotation() {

let meteorCycle = SKAction.rotateByAngle(4, duration: 2);
meteorAnimation = SKAction.repeatActionForever(meteorCycle)
}

func onTap() {
//self.physicsBody?.affectedByGravity = false
self.physicsBody?.dynamic = false
self.physicsBody?.categoryBitMask = 0

let crashAnimation = SKAction.group([
SKAction.fadeAlphaTo(0, duration: 0.2),
SKAction.scaleTo(1.5, duration: 0.2),
SKAction.moveBy(CGVector(dx: 0, dy: 25), duration: 0.2)
])
let resetAfterCrashed = SKAction.runBlock {
self.position.y = 5000
self.alpha = 1
self.xScale = 1
self.yScale = 1
}
let crashSequence = SKAction.sequence([
crashAnimation,
resetAfterCrashed
])

self.runAction(crashSequence)

}
}

是否可以使用我搜索类节点的代码?

最佳答案

我试图修改您发现的随机生成代码以使用您的 meteor 类。我删除了所有不涉及 meteor 的行,所以你需要添加你发给我的世界、地球和领域代码。试一试,如果成功请告诉我:

class GameScene: SKScene {

override func didMoveToView(view: SKView) {
backgroundColor = SKColor.blackColor()

runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(spawnEnemy),
SKAction.waitForDuration(1.0)])))
}

func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(#min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}

func spawnEnemy() {
let newMeteor = Meteor()
let meteorPosition = CGPoint(x: frame.size.width, y: frame.size.height * random(min: 0, max: 1))
newMeteor.spawn(world, meteorPosition)
}
}

关于swift - 我怎样才能在我的游戏场景中生成一个随机的(CLASS SPRITENODE)敌人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34166527/

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