SKSpriteNode position

旧巷老猫 提交于 2019-12-11 08:09:11

问题


I am trying to (learn how to) build a game using SpriteKit and i ran into a problem that has been bugging me for hours. I have a class named Tank with the following constructor:

+ (instancetype)tankAtPosition:(CGPoint)position {
    Tank *tank = [self spriteNodeWithImageNamed:@"tank_base_1"];
    tank.position = position;
    tank.anchorPoint = CGPointMake(0.5, 0.5);
    return tank;
}

In my scene i have the following constructor:

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"level_bg_1"];
        background.xScale = 0.40f;
        background.yScale = 0.40f;
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        [self addChild:background];

        Tank *tank = [Tank tankAtPosition:CGPointMake(CGRectGetMidX(self.frame), 512)];
        [self addChild:tank];
    }
    return self;
}

which compiled results in the following render:

Everything ok for now, however, if i change the y of the tank to 256:

Tank *tank = [Tank tankAtPosition:CGPointMake(CGRectGetMidX(self.frame), 256)];

I get this:

As far as i know, the bottom is y = 0 and and the middle point y = 512, so when i specify a y = 256 it should be centered in the bottom half of the screen. Why is is near the edge? The testing device is a ipad retina mini and in the Deployment Info > Devices i specified iPad. What am i missing? Thank you.


回答1:


i figured it out. the frame size was all messed up because i set my game to run in landscape only. solution: initialize the scene in viewDidLayoutSubviews instend of viewDidLoad

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [Level sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}


来源:https://stackoverflow.com/questions/25253654/skspritenode-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!