简体   繁体   English

图片不适合SKSpriteNode框架

[英]Image doesn't fit SKSpriteNode frame

I'm trying to create a player node on my scene. 我正在尝试在场景中创建一个播放器节点。 But the image "player.png" doesn't fit the physics body. 但是图像“ player.png”不适合物理身体。 They are both separate. 他们都是分开的。

PhysicsBody is in the ground and the image is floating on the air. PhysicsBody在地上,图像漂浮在空中。 What's wrong with my code? 我的代码有什么问题? How do I make them both together? 如何使它们在一起?

#import "Player.h"

@implementation Player
- (instancetype)init {
    self = [super initWithImageNamed:@"player.png"];
    self.name = @"player";
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 50)];
    self.physicsBody.dynamic = YES;
    self.physicsBody.allowsRotation = NO;
    self.physicsBody.affectedByGravity = YES;

    self.zPosition = 100;
    self.anchorPoint = CGPointMake(0.5, 0);

    return self;
}

@end
// myScene.h
-(void)createSceneContents {
    self.currentBackground = [Background generateBackground];
    [self addChild: self.currentBackground];
    self.physicsWorld.gravity = CGVectorMake(0, _gravity);
    self.physicsWorld.contactDelegate = self;

    Player *player = [[Player alloc]init];
    player.position = CGPointMake([UIScreen mainScreen].applicationFrame.size.width/2, 50);
    [self addChild:player];
}

Anchor point is the center of your physics body also here. 锚点也是您物理身体的中心。 Your point 0.5, 0.0 means center x and zero y. 您的点0.5、0.0表示中心x和零y。 So, the center of your physics body is at the bottom edge of your sprite node. 因此,物理物体的中心位于精灵节点的底部边缘。 Likely the bottom center of the image. 图像的底部中心。 But the physics body extends downward from there. 但是物理体从那里向下延伸。 This is because of the method you used to create the physics body. 这是因为您使用了创建物理物体的方法。

Anchor points are confusing. 锚点令人困惑。 They play dual roles sometimes. 他们有时扮演双重角色。 They include a lot of poorly documented implicit behaviors. 它们包括许多文献记载不充分的隐式行为。

Unless you have logic relying on the anchor point, it's best to shy away from changing them. 除非您有依靠锚点的逻辑,否则最好不要更改它们。 With a physics body, what matters is where the body is in the physics world. 对于物理物体,重要的是该物体在物理世界中的位置。 Keep your model of the sprite as simple as possible. 保持精灵的模型尽可能简单。 Refine constantly towards the simplest model to get the job done. 不断优化最简单的模型以完成工作。 It will simplify your game logic. 它将简化您的游戏逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM