【发布时间】:2018-02-01 18:42:11
【问题描述】:
所以我在这里尝试做的是能够移动四个精灵节点中的两个,彼此独立。我使用枚举来检测触摸了哪些幻灯片,然后使用该信息移动正确的节点,我知道我可以使用节点的名称,但似乎更适合我正在尝试做的事情。到目前为止一切正常,除了我一次只能移动一个节点。我希望能够移动...比如说,leftSlide 向上,同时向右移动向下。
感谢您的任何建议或cmets,欢迎任何反馈..
var selectedSlides: [SKSpriteNode] = []
var theSlideTouched: SlideTouched = .nothing
enum SlideTouched
{
case left
case right
case bottom
case top
case nothing
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
for touch in touches
{
if leftSlide.contains((touch.location(in: self)))
{
selectedSlides.append(leftSlide)
theSlideTouched = .left
}
if rightSlide.contains((touch.location(in: self)))
{
selectedSlides.append(rightSlide)
theSlideTouched = .right
}
if bottomSlide.contains((touch.location(in: self)))
{
selectedSlides.append(bottomSlide)
theSlideTouched = .bottom
}
if topSlide.contains((touch.location(in: self)))
{
selectedSlides.append(topSlide)
theSlideTouched = .top
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
{
for touch in touches
{
let location = touch.location(in: self)
switch theSlideTouched
{
case .bottom:
if selectedSlides.contains(bottomSlide)
{
bottomSlide.color = UIColor.white
bottomSlide.position.x = location.x
}
case .top:
if selectedSlides.contains(topSlide)
{
topSlide.color = UIColor.white
topSlide.position.x = location.x
}
case .left:
if selectedSlides.contains(leftSlide)
{
leftSlide.color = UIColor.white
leftSlide.position.y = location.y
}
case .right:
if selectedSlides.contains(rightSlide)
{
rightSlide.color = UIColor.white
rightSlide.position.y = location.y
}
case .nothing: break
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
switch theSlideTouched
{
case .bottom: bottomSlide.color = UIColor.clear
case .top: topSlide.color = UIColor.clear
case .left: leftSlide.color = UIColor.clear
case .right: rightSlide.color = UIColor.clear
case .nothing: break
}
theSlideTouched = .nothing
selectedSlides.removeAll()
}
【问题讨论】:
标签: swift sprite-kit multi-touch uitouch touchesbegan