简体   繁体   English

如何在Sprite Kit中移动对象?

[英]How to I move an object in Sprite Kit?

I have this code and I am trying to write a game. 我有此代码,我正在尝试编写游戏。 I want the car to go right one lane when there is a tap on the right side of the screen and left one lane when there is a tap on the left side of the screen. 我希望汽车在屏幕右侧有水龙头时向右行驶一个车道,在屏幕左侧有水龙头时向左行驶。 I wrote a function to move the car but I don't know how to get it to work. 我写了一个移动汽车的功能,但是我不知道如何使汽车运转。 This is my code: 这是我的代码:

 //
 //  GameScene.swift
 //  cargame
 //
 //  Created by Pranav Sharan on 3/21/17.
 //  Copyright © 2017 Pranav Sharan. All rights reserved.
 //

 import SpriteKit
 import GameplayKit

class GameScene: SKScene {



override func didMove(to view: SKView) {
    let player:SKSpriteNode = self.childNode(withName: "car") as! SKSpriteNode
}


func touchDown(atPoint pos : CGPoint) {
}

func touchMoved(toPoint pos : CGPoint) {

}

func touchUp(atPoint pos : CGPoint) {

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let position = touch.location(in: self)
        print(position.x)
        print(position.y)
        if position.x < 0 {
            moveLeft(object: player)
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}


override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}




func moveLeft(object: SKSpriteNode){
    let move = SKAction.moveTo(x: -20, duration: 0.5)
    object.run(move)
}

} }

Here is an example of how you can Accomplish this, Hope it helps. 这是一个如何完成此操作的示例,希望对您有所帮助。

     class GameScene: SKScene {

var player = SKSpriteNode()
override func didMove(to view: SKView) {

    player = SKSpriteNode(color: .purple, size: CGSize(width: 100, height: 100))
    player.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    self.addChild(player)

       }


func touchDown(atPoint position : CGPoint) {

    if position.x < self.frame.midX
    {
        let move = SKAction.moveTo(x: self.frame.minX + 200, duration: 0.3)
        player.run(move)

    } else if position.x > self.frame.midX {
    let move = SKAction.moveTo(x: self.frame.maxX - 200, duration: 0.3)
    player.run(move)
    }
}

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

    for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
}

If a touch is less than the middle of the screen, The middle of the screen defined by self.frame.midX. 如果触摸小于屏幕中间,则屏幕中间由self.frame.midX定义。

First thing I see is that you declare your player node in the didMove() function, which makes it invisible outside the scope of that function. 我首先看到的是您在didMove()函数中声明了播放器节点,这使该节点在该函数范围之外不可见。

Declare the player node as a property of your GameScene class to make it available everywhere inside and outside of your class. 将玩家节点声明为GameScene类的属性,以使其在您的班级内部和外部均可使用。 Then, you assign your scene object to it. 然后,将场景对象分配给它。 Your code would more likely look like that: 您的代码更有可能是这样的:

class GameScene: SKScene {

var player:SKSpriteNode

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM