【问题标题】:SpriteKit: Making stickman bounceSpriteKit:让火柴人弹跳
【发布时间】:2014-11-10 10:10:20
【问题描述】:

我仍然是 spritekit 的超级菜鸟,并试图在这里和那里获得一些教程..

我正在尝试制作这样的火柴人形象 跳到高楼,碰到地面节点弹跳

当节点是带有圆物理体的球时,它可以正常工作

但是当尝试矩形的物理体时..它永远不会反弹..我什至尝试过这段代码

SKSpriteNode * victim = [SKSpriteNode spriteNodeWithImageNamed:@"victim_base"];
    victim.position = CGPointMake(5, 500);
    victim.zPosition = 2;
    victim.name = @"victim";
    victim.xScale=0.7;
    victim.yScale=0.7;
    [self addChild:victim];

    CGFloat offsetX = victim.frame.size.width * victim.anchorPoint.x;
    CGFloat offsetY = victim.frame.size.height * victim.anchorPoint.y;

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 13 - offsetX, 59 - offsetY);
    CGPathAddLineToPoint(path, NULL, 43 - offsetX, 58 - offsetY);
    CGPathAddLineToPoint(path, NULL, 38 - offsetX, 3 - offsetY);
    CGPathAddLineToPoint(path, NULL, 7 - offsetX, 3 - offsetY);

    CGPathCloseSubpath(path);
    victim.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
    victim.physicsBody.friction = 0.0f;
    // 4
    victim.physicsBody.restitution = 1.0f;
    // 5
    victim.physicsBody.linearDamping = 0.0f;
    // 6
    victim.physicsBody.allowsRotation = NO;

    [victim.physicsBody applyImpulse:CGVectorMake(10.0f, -10.0f)];

重力世界的设置是

self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
// 1 Create an physics body that borders the screen
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
// 2 Set physicsBody of scene to borderBody
self.physicsBody = borderBody;
// 3 Set the friction of that physicsBody to 0
self.physicsBody.friction = 0.0f;

但他在跳跃,只是在地上不动,没有弹跳

我真的不知道这里出了什么问题。任何帮助将不胜感激。

【问题讨论】:

  • 设置 scene.showsPhysics = TRUE 以在屏幕上渲染physicsBodies。您可以检查物理体的结果。
  • 你为什么关掉重力?在设置self.physicsBody 属性时尝试检查self.frame 的值。
  • 我正在按照本教程关闭它raywenderlich.com/49721/… 让他永远弹跳
  • 根据您上面的描述,我猜您提供的教程链接不是您想要做的......在那里物理学更像是空间物理学,没有任何重力,所以你无法获得地球上火柴人跳跃后弹跳的效果。尽量不要将重力设置为 0,0 并使用施加在火柴人身上的冲动,并尝试将 y 值设置为正值。

标签: ios objective-c sprite-kit


【解决方案1】:

查看此示例

#import "milkhuntMyScene.h"

@implementation milkhuntMyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];

        myLabel.text = @"";
        myLabel.fontSize = 30;
        myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                       CGRectGetMidY(self.frame));

        [self addChild:myLabel];
        self.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        self.physicsWorld.gravity=CGVectorMake(0.0, -9.8);

        SKSpriteNode *node=[SKSpriteNode spriteNodeWithImageNamed:@"dummy"];
        node.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(node.size.width, node.size.height)];
        node.position=CGPointMake(200, 50);
        node.physicsBody.affectedByGravity=FALSE;
        node.physicsBody.dynamic=TRUE;
        node.physicsBody.restitution=1;
        node.physicsBody.allowsRotation=FALSE;
        node.physicsBody.mass=9999999;

        [self addChild:node];

    }
    return self;
}
//




//
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
        sprite.xScale=sprite.yScale=0.5;
        sprite.position = location;
        sprite.physicsBody.allowsRotation=FALSE;
        sprite.physicsBody.restitution=1.0;
          sprite.physicsBody.angularDamping=0.0;
        sprite.physicsBody.linearDamping=0.0;
      //  SKAction *action = [SKAction rotateByAngle:M_PI duration:1];

        //[sprite runAction:[SKAction repeatActionForever:action]];
        sprite.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(sprite.size.width, sprite.size.height)];
        [self addChild:sprite];
    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多