根据其他精灵的位置在 x 轴和 y 轴上反射精灵

reflect sprite over x axis and y axis depending on position of other sprite

我想弄清楚为什么我在 y 轴上的反射不起作用

在我的 gameViewContoller 中,我设置了场景

scene.scaleMode = .AspectFill
scene.anchorPoint = CGPoint(x: 0, y: 0)
scene.size = skView.bounds.size

然后在我的游戏场景中

location2 = touch.locationInView(self.view)

var xloc : Int = Int( screenWidth / 2)
//output is 187
var yloc : Int = Int( screenHeight / 2)
//output is 333
var x: Int = Int(location2.x)
// from left to right is 1 - 374
var y: Int = Int(location2.y)
// from top to bottom is 1 - 666

然后我算一下

var zx1 = x - xloc
var x1 = xloc - zx1
   //output when position.x = 10 the x1 is 364    
var zy1 = y - yloc
var y1 = yloc - zy1
  // output when position.y = 10 the y1 is 656

然后我将它们转换为 cgpoints 并将一个精灵移动到相应的位置

var y2: CGFloat = CGFloat(y1)
var x2: CGFloat = CGFloat(x1)


bot2.position = CGPointMake(x1, y2)

机器人总是反映在 y 轴上,但由于某种原因它不会反映在 x 轴上所有数字加起来但在机器人位置 y 期间出现问题

感谢您的帮助

SKScene 中的 touch location 应该使用 locationInNode 而不是 locationInView 来计算。这是因为SKSceneorigin在左下角,UIView的原点在左上角。

var location2 = touch.locationInNode(self)