2
我正在嘗試創建一個小遊戲,並且在那裏有一個管道,我想將該管道與y軸一起移動,我想爲手指做薄輕掃,我試着用觸摸做到這一點開始,但它並沒有外表光滑,所以我試圖用uipangesture,但在不熟悉的精靈融合,嘗試在y軸上移動/拖動SKSpriteNode
有人可以幫助我實現這一目標
class GameScene: SKScene,SKPhysicsContactDelegate {
var pipeTextureUp:SKTexture!
var pipeTextureDown:SKTexture!
var pipeUp:SKSpriteNode!
var pipeDown:SKSpriteNode!
var circleTouch: UITouch?
var verticalPipeGap:Double = 60.0
var moveStatus: Bool!
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backgroundColor = SKColor.whiteColor()
//set the gravity
self.physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
self.physicsWorld.contactDelegate = self
//first create the pipe in a random location
pipeTextureUp = SKTexture(imageNamed: "PipeUp")
pipeTextureUp.filteringMode = .Nearest
pipeTextureDown = SKTexture(imageNamed: "PipeDown")
pipeTextureDown.filteringMode = .Nearest
pipeUp = SKSpriteNode(texture: pipeTextureUp)
pipeUp.setScale(2.0)
pipeUp.name = "pipeUp"
pipeUp.position = CGPoint(x: size.width * 0.9, y: 0.0)
pipeDown = SKSpriteNode(texture: pipeTextureDown)
pipeDown.setScale(2.0)
pipeDown.name = "pipeDown"
pipeDown.position = CGPoint(x: Double(size.width) * 0.9 , y: Double(pipeDown.size.height) + verticalPipeGap)
//addChild(pipeDown)
addChild(pipeUp)
}
func didBeginContact(contact: SKPhysicsContact) {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown")
{
print("touches started")
moveStatus = true
circleTouch = touch
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
print(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown")
{
//let fingerPoint = CGPoint(x: size.width * 0.9, y: 0.3)
let actionUpMove:SKAction = SKAction.moveToY(positionInScene.y, duration: 1.0)
pipeUp.runAction(actionUpMove)
pipeUp.position.y = positionInScene.y
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch:UITouch = touches.first!
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if (name == "pipeUp" || name == "pipeDown")
{
//let fingerPoint = CGPoint(x: size.width * 0.9, y: 0.3)
circleTouch = nil
}
}
}
}
但我該如何移動我的pipeUp對象在swipedUp函數中,?? –
依靠你的遊戲,如果你使用物理,最好使用addImpulse來移動你的對象,或者更正速度,否則如果你有一個靜態對象,你可以使用moveTo和目標點。 –
我的對象操作系統是靜態的,但moveTo不會給我一個平滑的過渡,我已經嘗試過它,想象它是一個管道,並且我將管道沿y軸水平移動,我可以輕鬆使用addImpulse嗎? –