gpt4 book ai didi

swift - 将 SKSpriteNode 向另一个节点的相同方向移动

转载 作者:行者123 更新时间:2023-11-30 13:54:12 26 4
gpt4 key购买 nike

当我以与另一个按钮相同的方向触摸按钮但速度更快时,我想移动 SKSpriteNode。我暂时有这个

override func didMoveToView(view: SKView) {

Vaisseau = SKSpriteNode(texture: VaisseauTexture)
Vaisseau.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
Vaisseau.physicsBody = SKPhysicsBody(rectangleOfSize: Vaisseau.size)
Vaisseau.physicsBody?.affectedByGravity = false
Vaisseau.physicsBody?.categoryBitMask = PhysicsCategories.Vaisseau
Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Bonus
Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Meteorites
Vaisseau.physicsBody?.dynamic = false
self.addChild(Vaisseau)

BoutonMissile = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
BoutonMissile.center = CGPointMake(self.frame.size.width / 4 - 120,self.frame.size.height / 4 + 240)
BoutonMissile.addTarget(self, action: Selector("TireMissile"), forControlEvents: UIControlEvents.TouchUpInside)
BoutonMissile.setBackgroundImage(ImageBoutonMissile, forState: .Normal)
self.view?.addSubview(BoutonMissile)

}

func TireMissile() {
let missile = SKSpriteNode(imageNamed: "Missile")
missile.position = CGPointMake(Vaisseau.position.x,Vaisseau.position.y)
self.addChild(missile)


}

我希望当我按下按钮 buttonMissile 时,它会创建并沿与我用手指移动的 Vaisseau 相同的方向移动。

编辑:

我搜索如何转换向量中的弧度,我发现了这个。这是完美的工作,但我的导弹总是在我的Vaisseau

的右侧
func TireMissile() {

let missile = SKSpriteNode(imageNamed: "Missile")
missile.position = CGPointMake(Vaisseau.position.x,Vaisseau.position.y)
missile.zRotation = Vaisseau.zRotation
missile.zPosition = -1
self.addChild(missile)
let Dx = cos(Vaisseau.zRotation)
let Dy = sin(Vaisseau.zRotation)
print(cos(Vaisseau.zRotation))
print(sin(Vaisseau.zRotation))
missile.runAction(SKAction.moveBy(CGVector(dx: Dx * 4000 , dy: Dy * 4000), duration: 10))

}

编辑:

我找到了解决方案:我替换

let Dx = cos(Vaisseau.zRotation)
let Dy = sin(Vaisseau.zRotation)

let Dx = cos(Vaisseau.zRotation + CGFloat(M_PI/2))
let Dy = sin(Vaisseau.zRotation + CGFloat(M_PI/2))

最佳答案

查看SKActions,您要做的就是改变到达目的地所需的时间,或者称为持续时间:

例如

missle.runAction = SKAction.moveByX(100, y: missle.position.y, duration:1) //will move 100 pixels in 1 second
self.runAction = SKAction.moveByX(100, y:self.position.y, duration:2) //will move 100 pixels in 2 seconds

关于swift - 将 SKSpriteNode 向另一个节点的相同方向移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33858362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com