1

I have SKSpriteNode which is forced to fall to the ground because of self.physicsWorld.gravity = CGVectorMake(0.0, -4.0);. When user taps on display and holds it, I'd like the SKSpriteNode to fly up, and after user stops holding touch, it again falls down to the ground.

I have tried to change velocity, but it only makes small bounce, like applyImpulse method...:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {

            let location = touch.locationInNode(self)

            flyingObject.physicsBody.velocity = CGVectorMake(0, 100)
        }        
    }

edit:

here's my scene initialization code

override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        // Physics
        self.physicsWorld.gravity = CGVectorMake(0.0, -4.0);

        // flyingObject
        var flyingObjectTexture = SKTexture(imageNamed:"flyingObject")
        flyingObject.filteringMode = SKTextureFilteringMode.Nearest

        flyingObject = SKSpriteNode(texture: flyingObjectTexture)
        flyingObject.setScale(1)
        flyingObject.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)

        flyingObject.physicsBody = SKPhysicsBody(circleOfRadius:flyingObject.size.height/2.0)
        flyingObject.physicsBody.dynamic = true
        flyingObject.physicsBody.allowsRotation = false

        self.addChild(flyingObject)

        // Ground
        var groundTexture = SKTexture(imageNamed:"Ground")

        var sprite = SKSpriteNode(texture:groundTexture)
        sprite.setScale(0.5)
        sprite.position = CGPointMake(self.size.width/2 - 100, sprite.size.height)

        self.addChild(sprite)

        var ground = SKNode()

        ground.position = CGPointMake(0, groundTexture.size().height / 2)
        ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.width, groundTexture.size().height * 0.5))

        ground.physicsBody.dynamic = false
        self.addChild(ground)

    }
Adam Bardon
  • 3,829
  • 7
  • 38
  • 73
  • You should use the applyImpulse method instead of setting the velocity directly. From Apple's documentation, "When a body is in the simulation, it is more common for the velocity to be adjusted based on forces applied to the body." – 0x141E Jul 15 '14 at 10:01
  • this still just makes object bouncing, while I need it to be constantly going up until users stops touching – Adam Bardon Jul 15 '14 at 16:50
  • To do that, you can use the applyForce method using the following steps: 1) in touchesBegan, identify and set a pointer to the node that the user selected, 2) in update:(NSTimeInterval)currentTime, applyForce to selected node if a node is selected (i.e., pointer is not nil), 3) in touchesEnded, set pointer to nil. – 0x141E Jul 15 '14 at 18:49
  • See http://stackoverflow.com/a/28259980/2158465 – Epic Byte Jul 08 '15 at 02:51

2 Answers2

2

thanks to Code Monkey I was able to come up with solution, it's easier than I thought:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
        isTouching = true
    }

    override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
        /* Called when a touch begins */
        isTouching = false
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        if isTouching {
            flyingObject.physicsBody.applyImpulse(CGVectorMake(0, 5))
        }
    }
Adam Bardon
  • 3,829
  • 7
  • 38
  • 73
  • For some reason this isn't working for me. My boolean is initially set to 'false' which I believe makes sense. My node doesn't move at all. Could it be an issue with the "applyImpulse"? – TerranceW Jul 05 '15 at 18:48
1

touches began:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

flyingObject.physicsBody.velocity = CGVectorMake(0, 100)
}

touches ended:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

flyingObject.physicsBody.velocity = CGVectorMake(0, 0)

}

meisenman
  • 1,818
  • 1
  • 15
  • 25
  • I have same result with this - still just bouncing – Adam Bardon Jul 04 '14 at 21:18
  • weird... try removing gravity and setting touches ended to create a velocity of (0,-100). Set the velocity initially to (0,-100) to replicate gravity. – meisenman Jul 04 '14 at 21:33
  • again, still just bouncing... if I click ten times in a row, sprite is flying up... so there is probably issue with setting it for the actual "tap and hold"? – Adam Bardon Jul 05 '14 at 06:55
  • Try setting: flyingObject.physicsBody.dynamic = NO; when touchesBegan and set it back to YES when touches ended – meisenman Jul 05 '14 at 17:30
  • this disables flyingObject from flying... I'll edit my question and add scene initialization code, maybe there's something wrong, please check it – Adam Bardon Jul 06 '14 at 14:39