2

How to make a sprite sit on a moving sprite and travel with it. I have made the red box jump with impulse and when it falls on the black block down which is moving, the red box stays were it dropped slips the moving object like there is no friction. Conditions gravity on, friction 1.0 in both even tried increasing the mass, but nothing worked. Please give me any details how to make it work ? thanks override func didMoveToView(view: SKView) { /* Setup your scene here */

    self.physicsWorld.gravity = CGVectorMake(0.0, -9.8)
    SquareOne.fillColor = UIColor.orangeColor()
    SquareOne.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    SquareOne.physicsBody = SKPhysicsBody(rectangleOfSize: SquareOne.frame.size)
    SquareOne.physicsBody?.friction = 1.0
    SquareOne.physicsBody?.restitution = 0.0

    addChild(SquareOne)


    SquareTwo.fillColor = UIColor.greenColor()
    SquareTwo.position = CGPoint(x: self.frame.midX, y: self.frame.midY - 100)
    SquareTwo.physicsBody = SKPhysicsBody(rectangleOfSize: SquareTwo.frame.size)
    SquareTwo.physicsBody?.dynamic = false
    SquareTwo.physicsBody?.affectedByGravity = false
    SquareTwo.physicsBody?.friction = 1.0
    SquareTwo.physicsBody?.restitution = 0.0
    addChild(SquareTwo)

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        SquareTwo.runAction(SKAction.moveByX(-100, y: 0.0, duration: 3.0))
    }
}

How i want it to work

  • To make this more simple if there are just two blocks red and black. Red is affected by gravity and black is not affected my gravity. On touchesBegan function once i touch the black starts to move in x direction. When it moves the red block slips. How does to make it attached to the black block. – vaidhya nathan Sep 11 '15 at 05:34
  • 1
    You have the right idea. You just need to use physics to move the black blocks. Here's how http://stackoverflow.com/questions/31590885/how-to-move-platform-with-velocity – 0x141E Sep 11 '15 at 07:01

4 Answers4

2

In a physics simulation, you should apply velocity and forces for the simulation to work correctly. Physics cannot be simulated correctly using SKAction.

First the SquareTwo needs to be made dynamic for it to be effected by forces. Make the mass of SquareTwo big so that the body is not affected by the other SquareOne colliding with it.

SquareTwo.physicsBody?.dynamic = true
SquareTwo.physicsBody?.affectedByGravity = false
// Make the mass big so that the body is not affected by the other body colliding with it.
SquareTwo.physicsBody?.mass = 1000000

Then inside touchesBegan you can set a velocity to SquareTwo

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        SquareTwo.physicsBody?.velocity = CGVectorMake(-10, 0)
     }
}
rakeshbs
  • 24,392
  • 7
  • 73
  • 63
0

You can remove the red block then add it as a child node to the moving block:

var redBlockPosition = childNodeWithName("redBlockName")?.position //find where red block is in self
var movingBlockPosition = childNodeWithName("movingBlockName")?.position //find where moveing block is in self
var redBlockPositionFromMovingBlock = CGPointMake(redBlockPosition.x + movingBlockPosition.x, redBlockPosition.y - movingBlockPosition.y) //find where red block is from moving block's origin
childNodeWithName("redBlockName")?.removeFromParent() //remove red block
redBlock.position = redBlockPositionFromMovingBlock //change redBlock's position
childNodeWithName("movingBlockName")?.addChild(redBlock) //add red block as a child node of moving block

Hope this helped and good luck.

Michael Berk
  • 705
  • 7
  • 23
  • I can't remove the redblock as it the main block and it keeps jumping from one black block to another. So i need to remove and add red block for each moving black block. Which i don't feel is an effective way. – vaidhya nathan Sep 11 '15 at 05:23
  • Try increasing the friction to the maximum so that the block cannot slide. – Michael Berk Sep 11 '15 at 21:16
0

Try redBox.physicsBody.allowsRotation = NO,

A Boolean value that indicates whether the physics body is affected by angular forces and impulses applied to it.

And adjust redBox.physicsBody.restitution between 0.0 to 1.0,

The bounciness of the physics body.

WangYudong
  • 4,335
  • 4
  • 32
  • 54
  • @vaidhyanathan If you provide a sample project, I will take a look. – WangYudong Sep 11 '15 at 06:04
  • I have uploaded in google drive https://drive.google.com/file/d/0BwsEelZ-x-VKbnJCeXc3ZXhsWDA/view please let me – vaidhya nathan Sep 11 '15 at 06:32
  • @vaidhyanathan Hmm, I didn't catch your meaning, so my code wouldn't help. But you will definitely find something useful in rakeshbs's answer and 0x141E's comment. Good luck! – WangYudong Sep 11 '15 at 11:07
0

If you have an skaction that has a duration, it keeps the object moving until the duration is over, creating the object to not stay on another object. So rather than using a action to move the object, update its position with the update function, and make it stop moving when it touches a block.

Nav
  • 1