如何抛出SKSpriteNode? [英] how to throw SKSpriteNode?

查看:168
本文介绍了如何抛出SKSpriteNode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上在我的游戏中我需要抛掷或抛出一个物体。到目前为止,我有一个精灵,它可以被拖动,但它不能被抛出。游戏的想法是抛出精灵与另一个精灵碰撞。我想要我们将调用testNode的精灵,移动用户抛出它的方式

So basically in my game i need to toss or throw an object. So far i have a sprite, It can be dragged but it cannot be thrown. the games idea is to throw a sprite to collide with another sprite. I want the sprite which we will call testNode, To move how where the user had thrown it

意思是,当我释放精灵时,我希望它继续朝着我感动了我花了很多年时间试图解决这个问题,但我不能。我正在使用适用于iOS 8+的SpriteKit。如果有人可以提供帮助请。

meaning, when i release the sprite i want it to continue in the direction that i moved it . I have spent ages trying to work this out but I just can't. I am using SpriteKit for iOS 8+. If anybody can help please do.

我看过一个视频,但是GameSalad是另一个故事。你可以看看

Theres a video I saw but it was with GameSalad which is another story. You can have a look


https ://www.youtube.com/watch?v = nn3lWa5jUGE

(如果您需要进一步联系,请回复)

(Reply if you may need further contact)

import Foundation;
import SpriteKit;

class Level:SKScene {

TestNode:Test? //Test is a class I made

//Removed the update and touches began due to it being irrelevant to what I need help with. 

  override func didMoveToView(view: SKView){
    testNode = Fruit(imageNamed: "apple_idle")
    testNode?.position = CGPointMake(60, 294)
    testNode?.xScale = 0.5
    testNode?.yScale = 0.5
    self.addChild(testNode!)
  }
  override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

    var nodeTouched = SKNode()
    var currentNodeTouched = SKNode()

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        nodeTouched = self.nodeAtPoint(location)
        testNode?.position = location
    }
}

 override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

    var touch: UITouch = touches.anyObject() as UITouch
    var location: CGPoint = touch.locationInNode(self) as CGPoint


}


推荐答案

这里是我写的一个关于mov的简单例子通过在游戏循环中模拟其速度而不是直接设置位置来触摸精灵。这使得精灵更加动态(即你可以抛出它,并让它在拖动精灵时与其他物理体交互)。不需要角度计算,我只是计算在一段时间内将精灵移动到触摸位置所需的速度。在这种情况下,我将时间设置为1/60,以便瞬间应用运动,使对象显得非常敏感。

Here is a quick example I wrote of moving a sprite with touch by simulating its velocity in the game-loop as opposed to setting the position directly. This makes the sprite more dynamic (i.e. you can "throw" it, and let it interact with other physics bodies as your drag the sprite). No angle calculations are needed, i'm simply calculating the necessary velocity to move the sprite to the touch position over some interval of time. In this case I set the time as 1/60 so that the motion is applied instantaneously so the object appears very responsive.

import SpriteKit
class GameScene: SKScene {
    var sprite: SKSpriteNode!
    var touchPoint: CGPoint = CGPoint()
    var touching: Bool = false
    override func didMoveToView(view: SKView) {
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
        sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
        sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
        self.addChild(sprite)
    }
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch = touches.first as! UITouch
        let location = touch.locationInNode(self)
        if sprite.frame.contains(location) {
            touchPoint = location
            touching = true
        }
    }
    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch = touches.first as! UITouch
        let location = touch.locationInNode(self)
        touchPoint = location
    }
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        touching = false
    }

    override func update(currentTime: CFTimeInterval) {
        if touching {
            let dt:CGFloat = 1.0/60.0
            let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
            let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
            sprite.physicsBody!.velocity=velocity
        }
    }
}

您可以自己微调phyiscs计算,以获得所需的效果。但是这段代码应该足以让你入门。我能想到的一些改进可能会限制速度,因此物体在释放时不能移动得太快。在触摸拖动中添加延迟,以便移动精灵但随后意外地在末尾停止短时继续抛出对象。

You can fine-tune the phyiscs calculations yourself to get the desired effect you are looking for. But this code should be enough to get you started. Some enhancements I could think of are possibly capping the velocity so the object can't move too fast when released. Adding a delay to the touch-drag so that moving the sprite but then accidentally stopping short at the end continues to throw the object.

这篇关于如何抛出SKSpriteNode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆