- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有 3 个节点数组,每个数组有 5 个节点。本例中的节点是正方形。
我想使用 touchesBegan 和 touchesEnded 移动它们,保存用户触摸的数组,然后保存手指从屏幕上移开时的位置。我已经知道如何使用节点来做到这一点。
我的问题是我不知道如何告诉我的代码要移动哪个数组,因为我不能使用 array.name 之类的东西来区分差异我怎么能做这样的事情?
例如,如果我触摸我的 Array1,他会检测到它是我的 Array1,然后当我移开手指时,他会执行 SKAction 以移动我的 Array1 内的节点。
我尝试使用 array.description 但没有用。
谢谢。
最佳答案
由于 Sprite Kit 提供了访问场景节点树中 sprite 的便捷方法,因此几乎没有理由使用数组来管理您的 sprite 节点。在这种情况下,您可以为 Sprite 添加一组 SKNode
,因为您可以使用 node = sprite.parent
轻松访问 Sprite 所在的“容器”。然后,您可以通过遍历 node.children
来迭代该容器中的 Sprite 。以下是如何执行此操作的示例:
var selectedNode:SKSpriteNode?
override func didMoveToView(view: SKView) {
scaleMode = .ResizeFill
let width = view.frame.size.width
let height = view.frame.size.height
let colors = [SKColor.redColor(), SKColor.greenColor(), SKColor.blueColor()]
// Create 3 container nodes
for i in 1...3 {
let node = SKNode()
// Create 5 sprites
for j in 1...5 {
let sprite = SKSpriteNode(imageNamed:"Spaceship")
sprite.color = colors[i-1]
sprite.colorBlendFactor = 0.5
sprite.xScale = 0.125
sprite.yScale = 0.125
// Random location
let x = CGFloat(arc4random_uniform(UInt32(width)))
let y = CGFloat(arc4random_uniform(UInt32(height)))
sprite.position = CGPointMake(x, y)
// Add the sprite to a container
node.addChild(sprite)
}
// Add the container to the scene
addChild(node)
}
}
选择要在 touchesBegan
中移动的 Sprite
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
selectedNode = node as? SKSpriteNode
}
}
移动选中的 Sprite
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
selectedNode?.position = location
}
}
旋转包含所选 Sprite 的节点中的所有 child
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
if let parent = selectedNode?.parent?.children {
for child in parent {
let action = SKAction.rotateByAngle(CGFloat(M_PI*2.0), duration: 2.0)
child.runAction(action)
}
}
selectedNode = nil
}
关于ios - TouchBegan/TouchEnded 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31328887/
这是我的代码,以防 touchend 事件发生: $('body').on('click touchend', '.typeSelect', function(){ var Classes =
基本上我有这个移动菜单Link (请调整窗口大小以查看移动菜单)。问题在于,在移动设备上,用户通常会向下滑动来查看内容。手机上的主菜单。松开手指而不是第一次触摸时,有什么方法可以激活菜单的“点击”?问
对于在 touchend 事件期间衡量一个元素是否在另一个元素(视觉上)的最佳方法感到困惑。本质上,我正在实现触摸友好的拖放操作。 我看过一些关于 .elementFromPoint() 和 even
这是我第一次处理触摸事件。我正在制作一个按钮,用于在响应式网络上滑动侧面菜单。该按钮应该只显示 touchstart 事件存在,并在 touchend 事件触发后 500 毫秒消失。 这是我的代码:
在 HTML5、CSS3 和 jQuery 的帮助下创建的示例移动应用程序。在应用程序中列出了带有右箭头标记的用户详细信息数量。箭头标记有 touchend 事件。哪个触摸端处理程序导航到下一个屏幕。
我正在运行一个简单的 OpenGL:ES 应用程序(它是一个游戏)。游戏加载并向用户显示“新游戏按钮”,然后您就进入游戏了。我使用 TouchBegan/TouchEnded 来处理触摸。然后我获取坐
是否可以删除touchend 的active 状态?我想通过点击链接来设置链接的事件状态。当然,在我的移动设备中滚动时,我触摸的每个元素都会获得 :active 状态。因此,滚动后所有元素都会突出显示
我想做两件事...当我们触摸屏幕和从屏幕上释放手指时做一些事情。但是下面的 touchend 函数甚至在我释放手指之前就会触发警报框。我错在哪里? $(window).on('touchend', f
我注意到触摸事件中有一个奇怪的行为。情况是这样的:我有一个#pointercapture div,默认情况下是隐藏的。 touchstart 处理程序附加到主体,这会导致 pointercapture
我有一个带有 div 的简单列表(在 iOS 上)。我在列表元素上使用 touchend 事件来执行操作。我这样做是因为 click 事件有大约 500 毫秒的延迟,这让 UI 感觉迟钝。 但是,当我
我有 3 个节点数组,每个数组有 5 个节点。本例中的节点是正方形。 我想使用 touchesBegan 和 touchesEnded 移动它们,保存用户触摸的数组,然后保存手指从屏幕上移开时的位置。
我将 AngularJS 与 Ionic Framework 结合使用。我正在开发实时通信应用程序。 我有一个范围 slider ionic docu .如果我使用 ng-change 每一步都会调用
我有一些代码可以更改表的类。在手机上,有时表格对于屏幕来说太宽了,用户会拖动/滚动来查看内容。但是,当他们触摸并拖动表格时,它会在每次拖动时触发 touchend。 如何测试触摸端是否是触摸拖动的结果
看完这篇event_touchcancel文档: The touchcancel event occurs when the touch event gets interrupted. Differe
我在 ul 的 li 之间使用拖放操作。代码在桌面版本上运行良好。 A A C D E 但是对于移动版本,我得到了多个触摸端 li.addEventListener(
我试图为我为 iPhone 开发的绘图应用程序实现手掌拒绝功能,并注意到触摸事件中有一些奇怪的行为。当我将手掌放在屏幕上并不断抬起手的某个区域然后再次放下时,我会收到很多 touchBegin 事件,
我构建了一个处理 touchstart 的 slider , touchend和 touchmove事件。 它在 Android 和 iOS 上运行良好。 只有当我将 iOS 上的手指慢慢移出屏幕时,
如果元素“#menu”被触摸,我想获取 touchstart 和 touchend 位置。 我写了这段代码,但事件似乎根本没有触发: var lastLoc = 0;
最近我开始在 javascript 中玩触摸事件,我遇到了一个奇怪的 touchend 事件问题(可能是很明显的问题,但我太笨了,无法理解)。所以基本上,这是我的代码: function send(e
我一直在为我的主页开发一个移动网站,但在连接到移动触摸事件时遇到了问题。基本上我想完成以下任务: 用户向下滚动 触发 touchend 事件 --> 调用一个函数来计算滚动后隐藏在上方的文档的数量(如
我是一名优秀的程序员,十分优秀!