Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...