我正试图让元素在cercle的边缘移动.
var base = SKShapeNode(circleOfRadius: 200 ) // Size of Circle
base.position = CGPointMake(frame.midX, frame.midY)
base.strokeColor = SKColor.blackColor()
base.glowWidth = 1.0
base.fillColor = UIColor(hue:1, saturation:0.76, brightness:0.94, alpha:1)
base.zPosition = 0
self.addChild(base)
Run Code Online (Sandbox Code Playgroud)
main = SKSpriteNode(imageNamed:"Spaceship")
main.xScale = 0.15
main.yScale = 0.15
main.zPosition = 1
let circle = UIBezierPath(roundedRect: CGRectMake((self.frame.width/2) - 200, CGRectGetMidY(self.frame) - 200,400, 400), cornerRadius: 200)
let circularMove = SKAction.followPath(circle.CGPath, duration: 5.0)
main.runAction(SKAction.repeatAction(circularMove,count: 2))
self.addChild(main)
Run Code Online (Sandbox Code Playgroud)
宇宙飞船的第一次旋转(见下图)完全遵循圆的边缘,但第二次迭代改变了宇宙飞船的位置并将其移出屏幕边界.这是正常的还是我做错了什么?
谢谢.
在新的游戏,我想建立,我想知道,当用户触摸BOTH权和屏幕的左侧做一些逻辑。
我的代码:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
var isRight : Bool = false
var isLeft : Bool = false
if(location.x < self.size.width/2){
isLeft = true
println("Left")
}
if(location.x > self.size.width/2){
//
isRight = true
println("Right")
}
if (isRight && isLeft){
println("Both touched")
// do something..
}
}
}
Run Code Online (Sandbox Code Playgroud)
安慰:
Right
Left
Run Code Online (Sandbox Code Playgroud)
我的代码似乎不起作用。我在这里做错了什么?有人有更好的解决方案吗?
我有一个函数可以向服务器发出 API 请求。我想循环它直到它返回false(没有更多数据)。
func getData(id: Int) -> Observable<Bool> {
return Observable.create { observer in
// Alamofire request
// parse data
// if can decode,
// return true and increment page's property
// otherwise false
// error, if there's a problem
}
}
Run Code Online (Sandbox Code Playgroud)
takeWhile,比如 : getData(id).takeWhile {$0}。它只迭代我的函数 1x 次。第二次尝试:使用范围。这里的问题是,即使我的getData函数出错,循环也不会停止,而是继续!
Observable.range(start: 1, count: 100)
.enumerated()
.flatMapLatest({ _ in
self.getData(someID)
})
.subscribe(onNext: { _ in
// save to DB
observer.onNext(true)
observer.onCompleted()
}, onError: …Run Code Online (Sandbox Code Playgroud)