- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在制作一个非常像四个正方形的游戏,除了在选择一个正方形之前你有 20 个 Sprite 要放置。在执行正方形决策之前,我只需要有关如何通过点击屏幕 20 次(每次都是不同的 Sprite )来添加多个不同的 Sprite 的帮助。到目前为止,我的代码只适用于一个 Sprite ,但会立即决定选择哪个方 block 。如果有人可以提供帮助,我们将不胜感激!
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var character:SKSpriteNode!
var count:Int = 0
var number:Int!
var countDownLabel:SKLabelNode!
var scoreLabel:SKLabelNode!
var loss:Int = 0
var score:Int = 0 {
didSet {
scoreLabel.text = "Score: \(score)"
}
}
override func didMove(to view: SKView) {
character = SKSpriteNode()
scoreLabel = SKLabelNode(text: "Score: 0")
scoreLabel.position = CGPoint(x: 300, y: 620)
scoreLabel.fontSize = 36
scoreLabel.fontColor = .black
self.addChild(scoreLabel)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let location = touch.location(in: self)
character = SKSpriteNode(imageNamed: "shuttle")
character.position = location
self.addChild(character)
number = Int(arc4random_uniform(4)+1)
if (location.x < 0 && location.y > 0 && number == 1){
character.removeFromParent()
print("you lose")
loss += 1
} else if (location.x > 0 && location.y > 0 && number == 2){
character.removeFromParent()
print("you lose")
loss += 1
} else if (location.x < 0 && location.y < 0 && number == 3){
character.removeFromParent()
print("you lose")
loss += 1
} else if (location.x > 0 && location.y < 0 && number == 4){
character.removeFromParent()
print("you lose")
loss += 1
} else {
print("you win")
score += 1
}
}
最佳答案
问题是您正在定义一个单个 character
节点,如下所示:
var character:SKSpriteNode!
要获得 20 个 Sprite ,您可以做的是用这样的东西代替:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var count:Int = 0
var number:Int!
var countDownLabel:SKLabelNode!
var scoreLabel:SKLabelNode!
var loss:Int = 0
var score:Int = 0 {
didSet {
scoreLabel.text = "Score: \(score)"
}
}
override func didMove(to view: SKView) {
scoreLabel = SKLabelNode(text: "Score: 0")
scoreLabel.position = CGPoint(x: 300, y: 620)
scoreLabel.fontSize = 36
scoreLabel.fontColor = .black
self.addChild(scoreLabel)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
count = count + 1
let touch = touches.first!
let location = touch.location(in: self)
let character = SKSpriteNode()
character.name = "character\(count)
character = SKSpriteNode(imageNamed: "shuttle")
character.position = location
self.addChild(character)
number = Int(arc4random_uniform(4)+1)
if (location.x < 0 && location.y > 0 && number == 1){
character.removeFromParent()
print("you lose")
loss += 1
} else if (location.x > 0 && location.y > 0 && number == 2){
character.removeFromParent()
print("you lose")
loss += 1
} else if (location.x < 0 && location.y < 0 && number == 3){
character.removeFromParent()
print("you lose")
loss += 1
} else if (location.x > 0 && location.y < 0 && number == 4){
character.removeFromParent()
print("you lose")
loss += 1
} else {
print("you win")
score += 1
}
}
如果您需要再次访问字符
,请执行以下操作:
let number = //the id of the character you want to access
if let character = self.childNode(withName: "character\(number)") {
//enter code here
}
希望对您有所帮助!如果有任何需要澄清的地方,请发表评论。
我是 stackoverflow 的新手,所以如果我的格式不完美,我深表歉意。
关于swift - 生成多个 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52339691/
我正在创建一个游戏,用户可以在场景中移动一些水果。我希望用户只能移动场景中的水果,而不能移动场景中的任何其他 SKSpriteNode,因此我编写了下面的代码来实现它。然而,代码无法正常工作,因为我似
我正在尝试制作一款游戏,您可以倾斜手机并尝试将球保持在边界内。我似乎无法弄清楚如何让球不通过边界。我有倾斜来移动球,但它只是穿过我的所有边界,我似乎无法弄清楚如何让球在接触到边界时停止。这是我的代码:
我的代码遇到了一些问题。我正在尝试使用以下代码让僵尸跟随我的玩家: class GameScene: SKScene, SKPhysicsContactDelegate { func Enem
我正在尝试将一个 sprite 节点旋转到另一个。它旋转但不好。我该如何解决? 我试过: let blaster = self.childNode(withName: blaster) let cur
我如何获取一个 SKSpriteNode 及其所有子 SKSpriteNode 并将它们合并为一个没有子节点的扁平化 SKSpriteNode? 谢谢。 最佳答案 创建节点的纹理并从该纹理创建一个新的
对于我正在创建的游戏,SKSpriteNode 逐渐向下移动到用户屏幕上。屏幕底部附近还有另一个 SKSpriteNode(位置是静态的),只为原始 SKSpriteNode 留出一点空间。我需要检测
使用 swift,我试图让一个 SKSpriteNode(没有物理体)跟随另一个有物理体的 SKSpriteNode。当 Sprite 与一个物理体和另一个物理体接触时,我希望这样做。我阅读了有关设置
我有一条拿着蛋的龙。 EggNode 是 DragonNode 的子节点。 龙水平飞过屏幕。 当龙到达屏幕最右侧时,龙会重置到屏幕最左侧。 我已经实现了龙将鸡蛋扔到地面炮塔的位置。龙使用这个函数在to
/image/JQ9r6.png 在上面您可以看到我的游戏的图像。我有敌人生成并沿 y 轴向下移动。玩家应该在敌人到达墙壁之前将其击落。现在,玩家正在旋转到触摸的位置。我想要的是让子弹的旋转方向与旋转
我试图弄清楚 SKSpriteNode 已完全迭代另一个 SKSpriteNode。 这是我迄今为止想出的代码, if (node.frame.maxY == player.frame.minY) {
正如标题所说,有没有办法做这种事情: 有什么办法吗?如果是这样,我如何将 SKAction rotateToAngle 对应到面向圆的一侧? 谢谢! 最佳答案 圆在任何给定点的切线垂直于绘制到该点的半
对于令人困惑的问题标题,我深表歉意,但我不太确定如何表述我的问题。我的最终目标是在创建 SKSpriteNode 一定时间后删除它。但是,有一个小并发症。使用以下代码 func remove() {
我正在制作一个球必须遵循特定路径的游戏。如果它偏离正轨,他们就输了。问题是我不知道如何测试球是否偏离了路径。我尝试使用 node.intersectsNode(background) 但由于背景覆盖了
就问标题问,我想知道这是否完全可能。大多数 SKCropNode 示例使用纹理或形状。我想要完成的是自定义形状的面具。如果有办法,请告诉我! 最佳答案 您可以通过为节点提供一个子节点来屏蔽节点,其中子
这里的代码子类化 SKSpriteNode 并初始化它接受 SKScene import SpriteKit class Spaceship: SKSpriteNode{ var spaceship:
我的目标:让一个 SKSpriteNode 移动到另一个 SKSpriteNodes 的位置。 (为我的 sprite kit 游戏创建磁铁) 嗨,我正在尝试让多个同名的 SKSpriteNode 移
我正在使用 Xcode 开发应用程序,目前遇到有关 SKSpriteNodes 的问题。当有多个 SKSpriteNode 且该节点被触摸时,被触摸的节点不会被移除,但另一个未被触摸的节点会被移除。我
我有一个 SKScene,我要在其中添加一个 SKSpriteNode。我已经子类化 SKSpriteNode 类来创建这个节点。在子类中,我在 Sprite 上定义了某些 SKActions。我想要
我需要在我的项目中检测接触,但我有 9 个方 block 要检测接触。有没有一种方法可以在不创建 9 个不同的物理体或类似物理体阵列的情况下检测接触。此外,每当圆圈接触到一个正方形时,该正方形就会变色
我的游戏中有一个角色,它是一个带有几个子 SKSpriteNodes 的 SKSpriteNode,因此我可以为角色的各个部分(手、脚等)设置动画,它还有 1 个 SKSpriteNode(尝试用 S
我是一名优秀的程序员,十分优秀!