Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIScreen.mainScreen().bounds.size.height返回正确的数字,但SKSpriteNode未显示在顶部_Ios_Swift_Sprite Kit - Fatal编程技术网

Ios UIScreen.mainScreen().bounds.size.height返回正确的数字,但SKSpriteNode未显示在顶部

Ios UIScreen.mainScreen().bounds.size.height返回正确的数字,但SKSpriteNode未显示在顶部,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我正在学习SpriteKit并尝试使用SKAction.moveToY将节点从屏幕上向上移动。我想首先让它接近顶部(我假设一旦到达节点的顶部,它仍会在屏幕上,我必须进行调整,但这不应该是一个问题) 问题是我已经尝试使用了UIScreen.mainScreen().bounds.size.height和didMoveToView方法中的SKView的bounds.size.height。如果Iprintln两者都返回正确的顶部值(5s返回568.0)。当我将该值添加到SKAction.moveTo

我正在学习
SpriteKit
并尝试使用
SKAction.moveToY
将节点从屏幕上向上移动。我想首先让它接近顶部(我假设一旦到达节点的顶部,它仍会在屏幕上,我必须进行调整,但这不应该是一个问题)

问题是我已经尝试使用了
UIScreen.mainScreen().bounds.size.height
didMoveToView
方法中的
SKView
的bounds.size.height。如果I
println
两者都返回正确的顶部值(5s返回568.0)。当我将该值添加到
SKAction.moveToY(screenHeightVariable,duration:2)
中时,它可以正常工作并移动节点,但它会停止75%的移动到5s的顶端,90%的移动到6plus的顶端。是否有一种不同的方法,我应该得到屏幕顶部的Y值?这是我的一些代码(试着忽略一些更不合逻辑的东西,比如红色ufo,因为这更像是学习和反复试验):

覆盖func didMoveToView(视图:SKView){
physicsWorld.contactDelegate=self
println(view.bounds.size.height)
xVal=self.frame.width/2
ufoNode=ufo(图像名为:“ufo”)
ufoNode!.position=CGPoint(x:xVal!,y:30)
ufoNode!.physicsBody?.categoryBitMask=charCat
ufoRedNode=障碍物(图像名为:“ufo红色”)
ufoRedNode!.position=CGPoint(x:(self.frame.width/2-50),y:30)
ufoNode!.physicsBody?.categoryBitMask=sceneCat
self.addChild(ufoRedNode!)
self.addChild(ufoNode!)
}
覆盖func TOUCESBERTED(触摸:NSSet,withEvent事件:UIEvent){
/*当触摸开始时调用*/
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
如果位置.x
默认情况下,
SKSpriteNode
位置
是精灵的中点。因此,将
节点
移动到
屏幕高度
,将精灵的中点移动到
屏幕高度
节点的下半部分仍在屏幕内

如果要完全删除它,可以尝试通过
singleBullet.size.height/2
偏移最终位置

var bulletAction: SKAction = SKAction.moveToY(screenHeight + singleBullet.size.height/2, duration: 2)
您还可以重新配置更改
节点的
锚定点
属性
,以更改spriteNode内的哪个点移动到spriteNode的
位置
属性

主播点:定义精灵中与主播点相对应的点 节点的位置。可以在单位中指定此属性的值 坐标空间。默认值为(0.5,0.5),这意味着 精灵以它的位置为中心

同时将
NSTimer
替换为
SKActions
n计时器
SpriteKit
不兼容。比如说

let action = SKAction.runBlock { () -> Void in
    self.fireBullet(self)
}

let timerAction = SKAction.sequence([action,SKAction.waitForDuration(0.1)])
let foreverTimer = SKAction.repeatActionForever(timerAction)
self.runAction(foreverTimer)

不幸的是,与顶部的距离不接近节点高度的一半(节点只有8.5点高),与顶部的距离只有几百点。我尝试了singleBullet.size.height/2,也尝试了singleBullet.size.height*2作为良好的度量,但两者都不接近。另外,您不应该在SpriteKit中使用
NSTimers
。您可以使用
SKAction.repeatForever
SKAction.waitDuration
创建一个重复动作。只需在didMoveToView方法中使用self.frame.height来解决这个问题,并将它设置到我需要的位置。
let action = SKAction.runBlock { () -> Void in
    self.fireBullet(self)
}

let timerAction = SKAction.sequence([action,SKAction.waitForDuration(0.1)])
let foreverTimer = SKAction.repeatActionForever(timerAction)
self.runAction(foreverTimer)