gpt4 book ai didi

ios - SKPhysicsJointFixed.joint 减慢 Sprite

转载 作者:行者123 更新时间:2023-11-28 14:51:42 25 4
gpt4 key购买 nike

我有一个需要 SKPhysicsJointFixed.joint 的小游戏,它一切正常,除了一个事实,即关节处于事件状态时,即 10 秒,会使 Sprite 速度减慢,速度似乎是 Sprite 速度的一半,而没有联合活跃。

我已经尝试过恢复原状、密度甚至速度,但它们都奏效了。有人可以指导我正确的方向吗?我在这里或谷歌上找不到任何东西。

没有护盾的宇宙飞船图片:
Image of Spaceship without Shield

带护盾的宇宙飞船图片:
Image of Spaceship with Shield

带护盾和 showPhysics = true 的宇宙飞船图像:
Image of Spaceship with Shield and showPhysics = true

func activateShield() {

let shield1 = SKTexture(imageNamed: "shield-1")
let shieldImages = [shield1, SKTexture(imageNamed: "shield-2"), SKTexture(imageNamed: "shield-3"), SKTexture(imageNamed: "shield-4"), SKTexture(imageNamed: "shield-5"), SKTexture(imageNamed: "shield-6")]

let animateShield = SKAction.animate(with: shieldImages, timePerFrame: 0.10)
let animateRepeatShield = SKAction.repeatForever(animateShield)

shield = SKSpriteNode(texture: shield1)
shield.name = "ShieldActive"
shield.setScale(2.5)
shield.position = player.position
shield.zPosition = 3
shield.physicsBody = SKPhysicsBody(rectangleOf: shield.size)
shield.physicsBody!.affectedByGravity = false
shield.physicsBody!.categoryBitMask = PhysicsCategories.ShieldActive
shield.physicsBody!.collisionBitMask = PhysicsCategories.Enemy
shield.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy | PhysicsCategories.LifePu
shield.physicsBody!.isDynamic = true
shield.physicsBody!.density = 0
self.addChild(shield)
shield.run(animateRepeatShield)

let joint = SKPhysicsJointFixed.joint(withBodyA: player.physicsBody!, bodyB:shield.physicsBody!, anchor:player.position)
self.physicsWorld.add(joint)

//turns off the shield in between 5-10 seconds
shield.run(.wait(forDuration: 10, withRange: 0)) {
self.shield.removeFromParent()
}
}

最佳答案

我在@Ron 的帮助下解决了这个问题,方法是完全删除 PhysicsJoint,然后添加 shield.position.x += amountDragged,方法与为 Spaceship 所做的相同,并且效果很好。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch: AnyObject in touches{

let pointOfTouch = touch.location(in: self)
let previousPointOfTouch = touch.previousLocation(in: self)

let amountDragged = pointOfTouch.x - previousPointOfTouch.x

if currentGameState == gameState.inGame{
player.position.x += amountDragged
shield.position.x += amountDragged
}

if player.position.x > gameArea.maxX - player.size.width/2{
player.position.x = gameArea.maxX - player.size.width/2
}

if player.position.x < gameArea.minX + player.size.width/2{
player.position.x = gameArea.minX + player.size.width/2
}

if shield.position.x > gameArea.maxX - player.size.width/2{
shield.position.x = gameArea.maxX - player.size.width/2
}

if shield.position.x < gameArea.minX + player.size.width/2{
shield.position.x = gameArea.minX + player.size.width/2
}
}
}

关于ios - SKPhysicsJointFixed.joint 减慢 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49805491/

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