简体   繁体   English

Spritekit坐标系意外定位

[英]Spritekit Coordinate System unexpected positioning

I have the classes AttackArea, Player and GameScene. 我有AttackArea,Player和GameScene类。 I want to instantiate a new AttackArea object and place it near the Player, depending on the Players facing. 我想实例化一个新的AttackArea对象,并将其放置在Player附近,具体取决于Player所面对的对象。 Now I have problems with the right positioning. 现在我在正确定位上遇到了问题。 If I add the AttackArea as a child of GameScene, the positioning works as expected. 如果我将AttackArea添加为GameScene的子代,则定位将按预期工作。 But If I do that, the AttackArea isn't moving with the Player. 但是,如果我这样做,则AttackArea不会随播放器一起移动。 Otherwise if I add the AttackArea as a child of the Player, it's moving with the Player. 否则,如果我将AttackArea添加为Player的子代,它将随Player一起移动。 That's exactly what I want. 那正是我想要的。 The problem here is that the positioning of the AttackArea is now far away from the Player. 这里的问题是,AttackArea的位置现在远离播放器。 This is the code in the Player class: 这是Player类中的代码:

func attack(){
    let attack = AttackArea(color: .red, size: CGSize(width: self.frame.width, height: self.frame.height / 2))
    var animation = ""
    switch playerFacing{
    case .back:
        attack.position = CGPoint(x: self.position.x, y: self.position.y + 40)
        animation = Constants.Actions.playerAttackBack
    case .front:
        attack.position = CGPoint(x: self.position.x, y: self.position.y - 40)
        animation = Constants.Actions.playerAttackFront
    case .left:
        attack.position = CGPoint(x: self.position.x - 40, y: self.position.y)
        animation = Constants.Actions.playerAttackLeft
    case .right:
        attack.position = CGPoint(x: self.position.x + 40, y: self.position.y)
        animation = Constants.Actions.playerAttackRight
    case .none:
        break
    }
    attack.zPosition = self.zPosition + 1
    attack.setup()
    if animation != ""{
        self.run(SKAction(named: animation)!)
    }
    self.addChild(attack)
}

The first picture shows the situation when the AttackArea is a child of GameScene. 第一张图片显示AttackArea是GameScene的子代时的情况。 The positioning is fine but I want it to be a child of Player. 位置很好,但我希望它是Player的子代。

在此处输入图片说明

The second picture shows the positioning when the AttackArea is a child of Player. 第二张图片显示了AttackArea是Player的子代时的位置。 The red square in the top right corner is the AttackArea and the red circle is the Player. 右上角的红色方块是AttackArea,红色圆圈是Player。

在此处输入图片说明

Why is the AttackArea so far away from the Player in this case? 在这种情况下,为什么AttackArea与玩家如此遥远? How can I archieve the same result as in the first picture with the only exception, that the AttackArea is child of the Player? 如何保存与第一张图片相同的结果,唯一的例外是AttackArea是Player的子代?

what happens if you change the positioning to 如果将定位更改为

switch playerFacing{
    case .back:
        attack.position = CGPoint(x: 0, y: 0 + 40)
        animation = Constants.Actions.playerAttackBack
    case .front:
        attack.position = CGPoint(x: 0, y: 0 - 40)
        animation = Constants.Actions.playerAttackFront
    case .left:
        attack.position = CGPoint(x: 0 - 40, y: 0)
        animation = Constants.Actions.playerAttackLeft
    case .right:
        attack.position = CGPoint(x: 0 + 40, y: 0)
        animation = Constants.Actions.playerAttackRight
    case .none:
        break
    }

I suspect that your player (for example) might be at pos 500, 500 and you are re-adding those values to the attack position so now it's position is 1000, 1040 我怀疑您的玩家(例如)可能位于pos 500,500,并且您正在将这些值重新添加到攻击位置,所以现在它的位置是1000,1040

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM