- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想知道是否有人可以回答我的问题。我在 Sprite-kit 中使用纹理图集创建了 3 种不同的敌人动画,我想知道是否有一种方法可以在游戏开始后立即随机选择一个敌人向玩家移动。
TextureAtlas = SKTextureAtlas(named: "zombies.atlas")
for i in 1...TextureAtlas.textureNames.count {
let Z = "z\(i).png"
zombieArray.append(SKTexture(imageNamed: Z))
}
TextureAtlas = SKTextureAtlas(named: "baldzomb.atlas")
for i in 1...TextureAtlas.textureNames.count {
let bald = "baldzomb_\(i).png"
baldArray.append(SKTexture(imageNamed: bald))
}
TextureAtlas = SKTextureAtlas(named: "crawler.atlas")
for i in 1...TextureAtlas.textureNames.count {
let Name = "crawler_\(i).png"
crawlerArray.append(SKTexture(imageNamed: Name))
}
最佳答案
你可以这样做:
class GameScene: SKScene {
let storage = [
SKSpriteNode(color:.brown, size:CGSize(width: 50, height: 50)),
SKSpriteNode(color:.white, size:CGSize(width: 50, height: 50)),
SKSpriteNode(color:.black, size:CGSize(width: 50, height: 50)),
]
func getRandomSprite(fromArray array:[SKSpriteNode])->SKSpriteNode{
return array[Int(arc4random_uniform(UInt32(array.count)))]
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let sprite = getRandomSprite(fromArray: storage)
if let location = touches.first?.location(in: self){
if let copy = sprite.copy() as? SKSpriteNode {
//sprite can have only one parent, so I add a copy, just because of better example
copy.position = location
addChild(copy)
}
}
}
}
所以基本上你有一个 Sprite /纹理/任何东西的数组,你根据数组长度生成随机索引,并返回一个随机元素。您也可以进行扩展,并执行类似 yourArray.random
的操作。
关于swift - 在 spritekit 中随机化不同的敌人向玩家移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41679121/
好的,这就是我的错误:'Enemy' 未在此范围内声明。错误在 map.h 文件中,即使 map.h 包含 enemy.h,如图所示 #ifndef MAP_H_INCLUDED #define MA
这个问题已经有答案了: How to make enemy follow player in pygame? (2 个回答) Enemy doesn't follow player (pygame)
我试图让我的 Canvas 游戏中的 Sprite 不断向玩家移动,直到它发生碰撞。执行此操作的相关函数是 update() 函数: Enemy.prototype.update = function
我在无休止的运行游戏中遇到了麻烦,主要是我创建的两种敌人的产卵,当我们调用函数“startDifficultyTimer”时,有时会产生两个敌人。 基本上,当我调用该函数时,游戏会在同一行生成 2 个
从性能角度来看,什么是最正确的 - 敌人和武器之间的交互算法(这里的子弹可能更正确)? 每个 Sprite 的每一个子弹都应该通过“collidesWith”方法检查碰撞情况,并在完整的敌人列表上进行
遇到了一个问题,即敌人会向玩家开火,但似乎总是开到高处或向玩家的一侧,即使玩家是静止的并且没有移动。我是不是在我的代码中做错了什么导致了这个疯狂的问题,或者它只是一个随机的烦人的错误? 为播放器使用相
我是新来的快速编程。我想使用类节点将敌人生成到随机位置。我试图搜索随机生成敌人的代码,但它似乎与我的代码无关。 这是我搜索随机生成的代码。 import SpriteKit class GameSce
我最近遇到了一些问题,只是在我正在制作的游戏中传递对对象/敌人的引用,我想知道我是否使用了错误的方法。 我的主要问题是处理敌人和物体,而其他敌人或玩家可能仍然与它们有联系。 例如,如果你有一只兔子和一
在我的 2d 游戏中,我有一个金币和铜币的概念如下: when normal enemies die ..they drop a random gold or copper coin. Now whe
我是新来的,所以我希望你能帮助我。 我正在学习制作简单的 cocos2d 游戏的教程 Ray Wenderlich's Tutorial 我在另一个游戏中实现了它,一个像涂鸦跳跃这样的跳跃游戏。 在上
我是一名优秀的程序员,十分优秀!