- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
(我仍在快速学习,请耐心等待)。我目前正在尝试编写一个名为“Ninjumper”的简单 2D 平台游戏。我在我的 GameScene 中设置了实际的平台游戏,我目前正在创建一个带有“播放”按钮的主菜单,该按钮会将您带到 GameScene。我的 MenuScene 上只有一个颜色 Sprite (带有纹理“播放按钮”),我以编程方式将其制作成一个按钮。但是,当单击此按钮时,我的程序崩溃并出现标题中显示的错误。
这是我的 MenuScene 中的代码:
//
// MenuScene.swift
// Ninjumper
import SpriteKit
class MenuScene: SKScene {
var playButton = SKSpriteNode(texture: SKTexture(imageNamed: "playbutton"))
override func didMove(to view: SKView) {
addPlayButton()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.name == "playButton" {
// Call the function here.
let scene = GameScene(fileNamed: "GameScene")!
let transition = SKTransition.moveIn(with: .right, duration: 1)
self.view?.presentScene(scene, transition: transition)
}
}
}
func addPlayButton() {
playButton.position = CGPoint(x: 0, y: -156)
playButton.name = "playButton"
playButton.size = CGSize(width: 280.0, height: 100.0)
playButton.isHidden = false
addChild(playButton)
}
}
我想让它在您点击按钮时,场景从 MenuScene 切换到 GameScene。但是,我当前使用的代码会导致错误:无法将“Ninjumper.GameScene”类型的值转换为“SKSpriteNode”。我应该如何解决这个问题? (或者如果你有其他方法可以制作按钮切换场景,请告诉我)
最佳答案
试试这个。将 touchesBegan
的内容替换为以下内容:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
现在创建一个名为 touchDown
的新函数:
func touchDown(atPoint pos : CGPoint) { /* ... */ }
我们新功能的内部:
if playButton.contains(pos) {
if let scene = GameScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFit
let t = SKTransition.moveIn(with: .right, duration: 1)
self.view?.presentScene(scene, transition: t)
}
}
祝你好运!我会关注这个问题,所以如果你遇到同样的问题请告诉我:)
关于swift - 错误 : Could not cast value of type 'Ninjumper.GameScene' to 'SKSpriteNode' when trying to transition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58085205/
尝试添加动画以替换 fragment 时抛出该异常。根据这个答案,stackoverflow ,过渡框架不能应用于 fragment ,但这篇文章表明你可以 How to use Material T
还有其他一些类似的问题,但都涉及 Android 支持库。我正在使用新的 androidx 库。 将 androidx 生命周期依赖项从 2.2.0-alpha03 更新到 2.2.0-alpha04
这个问题在这里已经有了答案: What is an optional value in Swift? (15 个答案) When two optionals are assigned to an i
Tailwind 提供了多个实用程序来控制哪些 CSS 属性转换,在这些属性中有 transition 和 transition-all。 我去检查了两个类的 CSS 属性,这里它们的顺序相同。 tr
Tailwind 提供了多个实用程序来控制哪些 CSS 属性转换,在这些属性中有 transition 和 transition-all。 我去检查了两个类的 CSS 属性,这里它们的顺序相同。 tr
这是一个由两部分组成的问题: 哪种应用样式优先:CSS 中的 -webkit-transition 规则或 jQuery 中类似的 $.css() 方法? 如果我还在 jQuery 中使用 .css
我想知道为什么在 FireFox 中使用 transition: all 0.5s ease-out; 时我会看到元素上的闪烁效果(截至本文撰写时的最新版本)。很难解释,但请在此处查看实际示例:htt
我对 css3 过渡属性的渲染速度有疑问。 假设我有一些元素: div, span, a {transition: all} div {margin: 2px} span {opacity: .5}
我对 css3 过渡属性的渲染速度有疑问。 假设我有一些元素: div, span, a {transition: all} div {margin: 2px} span {opacity: .5}
我目前正在用 Html 制作我的第一个网站,javascript 作为前端将首先出现。 我正在尝试创建一个折叠侧边栏,当我单击顶部导航右侧的按钮时会出现该侧边栏,但我对过渡效果有疑问,因为它会将所有内
我有一个 Activity ,一次托管一个 fragment ,并在两个 fragment 之间交换。 fragment A 使用按钮启动 fragment B, fragment B 使用按钮或后退
我正在尝试创建一种效果: 光标悬停在一个框上 横杆滑出 随着滑动 Action 缓和,标题出现 光标离开方框 随着栏向后滑动,标题开始消失 横杆完成向后滑动 但是,当光标离开框时,会再次调用延迟,因此
让我提供一些背景信息:我正在构建一个选项卡式应用程序,允许用户查找和查看我们服务器上托管的一些视频。每个选项卡都以不同的方式对视频进行分组,导航栏中有一个分段控件,用户可以使用该控件更精确地对列表进行
原始问题...更新了以下工作代码: 我有一个在 ajax 加载事件期间出现的加载图像。图像通过向 body 元素添加或删除“加载”类来显示/隐藏。目前,加载图像将背景大小设置为从 0% 到 100%
我对 css3 过渡属性的渲染速度有疑问。 假设我有一些元素: div, span, a {transition: all} div {margin: 2px} span {opacity: .5}
当我继续学习前端开发和练习 Sass 优化我的 CSS 代码时,我又遇到了另一种情况。 在互联网上进行研究和教程后,我在 Sass 中设置了名为“transition”的全局混合。代码如下所示: @m
关于 Chrome 和 css 转换的问题太多了,我不确定以前是否有人问过这个问题。 我的情况是这样的:我的布局基本上是左右分页。右侧的一框内容需要显示在左侧的内容之上(这个我没有发言权,桌面和移动之
我在两个 Activity 之间的共享元素中使用了自定义 Transition。 我的转换不起作用,因为 TransitionValues 参数在 captureStartValues 和 这两种方法
我在 google Chrome 和 Safari 上运行我的应用程序,两者都兼容 webkit。我正在使用 -webkit-transition 构建一个滚动的 div。 当我将文档类型指定为 HT
以下代码在 chrome 中产生了预期的结果,即;直接向所有元素添加过渡延迟。 $('.front-nav .sub-menu').each(function() { var transiti
我是一名优秀的程序员,十分优秀!