快速移动多个物体

时间:2019-01-29 21:47:07

标签: ios swift sprite

我正在尝试制作一个具有五个简单对象的游戏,用户可以触摸和移动这些对象,并在其上施加物理作用,但是很难让它们分别移动,将不胜感激。

这是我到目前为止所拥有的:

import SpriteKit

class GameScene: SKScene {
    let starSprite = SKSpriteNode(imageNamed: "starSprite")
    let circleSprite = SKSpriteNode(imageNamed: "circleSprite")
    let squareSprite = SKSpriteNode(imageNamed: "squareSprite")
    let triangleSprite = SKSpriteNode(imageNamed: "triangleSprite")
    let moonSprite = SKSpriteNode(imageNamed: "moonSprite")

    var touchLocation = CGPoint()

    override func didMove(to view: SKView) {
        layoutScene()
    }

    func layoutScene() {
        backgroundColor = UIColor(red: 44/255, green: 62/255, blue: 
        80/255, alpha: 1.0)

        starSprite.size = CGSize(width: 350, height: 350)
        starSprite.position = CGPoint(x: frame.midX, y: frame.midY)
        starSprite.physicsBody = SKPhysicsBody(texture: 
        starSprite.texture!, size: starSprite.size)

         addChild(starSprite)

        circleSprite.size = CGSize(width: 350, height: 350)
        circleSprite.position = CGPoint(x: 0, y: 100)
        circleSprite.physicsBody = SKPhysicsBody(texture: 
        circleSprite.texture!, size: circleSprite.size)
        addChild(circleSprite)

        squareSprite.size = CGSize(width: 350, height: 350)
        squareSprite.position = CGPoint(x: 0, y: 200)
        squareSprite.physicsBody = SKPhysicsBody(texture: 
        squareSprite.texture!, size: squareSprite.size)
        addChild(squareSprite)

        triangleSprite.size = CGSize(width: 350, height: 350)
        triangleSprite.position = CGPoint(x: 0, y: 300)
        triangleSprite.physicsBody = SKPhysicsBody(texture: 
        triangleSprite.texture!, size: triangleSprite.size)
        addChild(triangleSprite)

        moonSprite.size = CGSize(width: 350, height: 350)
        moonSprite.position = CGPoint(x: 0, y: 400)
        moonSprite.physicsBody = SKPhysicsBody(texture: 
        moonSprite.texture!, size: moonSprite.size)
        addChild(moonSprite)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            touchLocation = touch.location(in: self)
            moonSprite.position.x = (touchLocation.x)
            moonSprite.position.y = (touchLocation.y)
            starSprite.position.x = (touchLocation.x)
            starSprite.position.y = (touchLocation.y)
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您应该首先检测touchDown中选择了哪个对象,然后在touchesMoved中更改位置,然后在touchUp中释放该对象!

将值定义为临时值并使用它!

此外,您不需要使用此

moonSprite.position.x = (touchLocation.x)
moonSprite.position.y = (touchLocation.y)

更好

 moonSprite.position = touchLocation

修改后的代码

    //
//  GameScene.swift
//  aa
//
//  Created by ehsan on 1/30/19.
//  Copyright © 2019 ehsan. All rights reserved.
//

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    private var spinnyNode1 = SKSpriteNode(imageNamed: "1")
    private var spinnyNode2 = SKSpriteNode(imageNamed: "2")
    private var movingSprite:SKSpriteNode?



    override func didMove(to view: SKView) {

        addChild(spinnyNode1)
        addChild(spinnyNode2)

    }

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

        for t in touches {

            let location = t.location(in: self)
            let node = self.atPoint(location)
            if node is SKSpriteNode
            {
                movingSprite = node as? SKSpriteNode
            }


        }

    }

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

            if movingSprite != nil

            {

                movingSprite!.position = touchLocation
            }

    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        movingSprite = nil
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches {



        }
    }


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