ios - Sprite 跟随不同的 Sprite 延迟

标签 ios swift sprite-kit sprite skspritenode

我做了我的第一个应用程序,问题是如何使一个对象以一定的速度跟随另一个对象,试图找到问题的答案但没有找到。 这就是我所做的,但毫不拖延

编辑:gif 如下。

创建英雄和枪支。

 func createHero() {
 
    hero = SKSpriteNode(texture: heroTexture)
    
    //Anim hero
    heroTextureArray = [SKTexture(imageNamed: "hero.png"), SKTexture(imageNamed: "Fly1.png"), SKTexture(imageNamed: "Fly2.png"), SKTexture(imageNamed: "Fly3.png"), SKTexture(imageNamed: "Fly4.png")]
    let heroFlyAnimation = SKAction.animate(with: heroTextureArray, timePerFrame: 0.1)
    let flyHero = SKAction.repeatForever(heroFlyAnimation)
    hero.run(flyHero)
    
    hero.position = position
    hero.size.height = 60
    hero.size.width = 65
    
    hero.physicsBody?.isDynamic = true
    hero.physicsBody?.allowsRotation = false
    hero.zPosition = 1
    
    heroObject.addChild(hero)
}


 func createGuns(){
    
    gunsTexture = SKTexture(imageNamed: "guns")
    
    guns = SKSpriteNode(texture: gunsTexture)
    
    guns.zPosition = 1
    guns.position = CGPoint(x: self.frame.width/2, y: 0)
    guns.physicsBody?.isDynamic = true
    guns.size.height = 35
    guns.size.width = 40
    guns.zRotation = CGFloat(.pi/2.0)

    
    gunsObject.addChild(guns)
    
}

主人公和пгты的运动

var flag = false

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch = touches.first
        let location = touch?.location(in: self)
    
        if (physicsWorld.body(at: location!) != nil) {
            
           flag = true
        
    }
}

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch = touches.first
     let touchLocation = touch!.location(in: self)
    let previousLocation = touch!.previousLocation(in: self)
    
    
    let hero = self.hero
    let guns = self.guns
    var heroX = hero.position.x + (touchLocation.x - previousLocation.x)
    
    heroX = max(heroX, hero.size.width / 2)
    heroX = min(heroX, size.width - hero.size.width / 2)
    
    var heroY = hero.position.y + (touchLocation.y - previousLocation.y)
    
    heroY = max(heroY, hero.size.height / 2 + 20)
    heroY = min(heroY, size.height - hero.size.height / 2)
    hero.position = CGPoint(x: heroX, y: heroY)
    guns.position = CGPoint(x: heroX, y: guns.position.y)
    guns.position = CGPoint(x: heroX, y: 20)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    flag = false
}

enter image description here

最佳答案

有必要添加SKAction

let moveGuns = SKAction.moveTo (x: heroX, duration: 3) 
let moveBgForever = SKAction.repeatForever (SKAction.sequence ([moveGuns]))
guns.run (moveBgForever)

关于ios - Sprite 跟随不同的 Sprite 延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46345253/

相关文章:

iphone - 当应用程序在后台运行时,是否可以从我的应用程序运行该应用程序?

ios - 我可以使用 PhoneGap 创建和提交 iBook

ios - UIDevice.current.setValue(value, forKey : "orientation") does not work if phone held in landscape and tilted 45 - 90 degrees

swift - 在 SpriteKit SWIFT 中获取 SKReferenceNode 的子节点

xcode - 如何让 SKSpriteNode 来回遵循相同的路径?

multidimensional-array - Swift、SpriteKit 和带有 CGPoint 的多维数组

objective-c - 将 NSString 与常量进行比较 #define string : isEqualToString or ==?

ios - 在 Collection View 中垂直和水平滚动 - 嵌入式流布局与自定义布局

ios - 在 Xcode 上运行第一个 singleview ios 应用程序

ios - 在 UIWebView 的本地 HTML 文件中加载本地镜像