Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

ios - how to have a enemy be added in every 10 seconds and have this enemy track your location and follow it

im currently working on an app in which your player which is a small round ball, gets dragged around the screen by your finger. Then every 10 seconds a enemy ball gets added in which automatically tracks your ball and follows it until it runs into it. this is the code I have for this so far. you can drag your ball around the screen, bu the enemy ball which is only showing up as a red x even though its file is in the assets gets spawned every frame and they just move to the position of where your ball was when it was spawned. is there any way I can fix this, any help is appreciated. the contact between the balls I will add later on.

import SpriteKit
import GameplayKit

class GameScene: SKScene {


var me = SKSpriteNode()
let enemy = SKSpriteNode (imageNamed: "ellipse 1")

override func didMove(to view: SKView) {
    me = self.childNode(withName: "me") as! SKSpriteNode

    let border = SKPhysicsBody (edgeLoopFrom: self.frame)
    border.friction = 0
    self.physicsBody = border
               }



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let location = touch.location(in: self)
        me.run(SKAction.moveTo(x: location.x, duration: 0))
        me.run(SKAction.moveTo(y: location.y, duration: 0))
    }

}


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let location = touch.location(in: self)
        me.run(SKAction.moveTo(x: location.x, duration: 0))
        me.run(SKAction.moveTo(y: location.y, duration: 0))
    }
}
override func update(_ currentTime: TimeInterval) {
        let enemy = SKSpriteNode (imageNamed: "ball 2")
    enemy.position = CGPoint(x:667, y: -200)
    enemy.run(SKAction.moveTo(x: me.position.x, duration: 1.5))
    enemy.run(SKAction.moveTo(y: me.position.y, duration: 1.5))
    enemy.zPosition = +1
    addChild(enemy)


}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So the problem you're having is that your enemy ball images aren't loading, and you're seeing a red X instead?

If so you need to break your code that creates your sprite into steps. First load the image into a temporary variable, print it, and then use the image to create your sprite:

let enemyImage = imageNamed: "ellipse 1"
print("enemyImage = (enemyImage)")
let enemy = SKSpriteNode (enemyImage)

If enemyImage is nil, you need to figure out why that is.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...