gpt4 book ai didi

ios - 快速移动多个物体

转载 作者:行者123 更新时间:2023-11-28 13:52:09 25 4
gpt4 key购买 nike

我正在尝试制作一款游戏,其中包含五个用户可以触摸和移动的简单对象,这些对象具有物理特性,但无法让它们单独移动,我们将不胜感激。

这是我目前所拥有的:

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)
}
}
}

最佳答案

你应该先在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
}
}

关于ios - 快速移动多个物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54430088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com