首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使雪碧坐在移动的雪碧上

如何使雪碧坐在移动的雪碧上
EN

Stack Overflow用户
提问于 2015-09-11 00:48:49
回答 4查看 199关注 0票数 2

如何使一个精灵坐在移动的雪碧上,并与它一起旅行。我让红色盒子冲动地跳起来,当它落在正在移动的黑色方块上时,如果它掉下来,红色盒子就停留在移动的物体上,就像没有摩擦力一样。在重力条件下,摩擦力1.0甚至都试着增加质量,但都没有效果。请给我详细的说明如何使它工作?谢谢覆盖func didMoveToView(视图: SKView) { /*在这里设置场景*/

代码语言:javascript
复制
    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))
    }
}

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-09-11 07:30:43

在物理模拟中,你应该应用速度和力使模拟正确地工作。使用SKAction无法正确地模拟物理。

首先,需要使SquareTwo成为动态的,这样它才能受到力量的影响。使SquareTwo的质量变大,使身体不受其他SquareOne与之碰撞的影响。

代码语言:javascript
复制
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

然后在touchesBegan内部你可以设定一个速度到SquareTwo

代码语言:javascript
复制
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)
     }
}
票数 2
EN

Stack Overflow用户

发布于 2015-09-11 01:45:02

您可以删除红色块,然后将其作为子节点添加到移动块中:

代码语言:javascript
复制
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

希望这能帮上忙,祝你好运。

票数 0
EN

Stack Overflow用户

发布于 2015-09-11 02:44:11

试试redBox.physicsBody.allowsRotation = NO

一个布尔值,表示物理物体是否受施加于它的角力和脉冲的影响。

调整redBox.physicsBody.restitution在0.0到1.0之间,

物理身体的弹性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32513802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档