gpt4 book ai didi

ios - 影响子节点定位的 Spritekit 场景 anchor

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:07:09 25 4
gpt4 key购买 nike

我创建了我的 SKScene 子类,它设置了 anchor ,然后为世界添加了一个 SKSpriteNode,世界有多个 SKSpriteNode 用于障碍物、玩家等。我也以

我遇到的问题是,当我将场景的 anchor 设置为 (0.5, 0.5) 时,我添加到世界的任何子节点的位置都从世界的中心开始。如何固定节点的位置,使 position = (0,0) 位于世界节点和添加到其中的任何子节点的左下角,而不是中心?

@implementation LevelScene

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {

NSLog(@"width:%f height:%f", self.view.bounds.size.width, self.view.bounds.size.height);


// set the physics body
self.physicsWorld.gravity = CGVectorMake(0,-5);
self.physicsWorld.contactDelegate = self;
self.anchorPoint = CGPointMake(0.5, 0.5);

NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LevelScene" ofType:@"plist"]];
NSString *backgroundImage = [plistDict objectForKey:@"background"];

// add a node that holds the background
background = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:backgroundImage] size:CGSizeMake(1024, 768)];
background.position = CGPointMake(0, 0);
[self addChild:background];

world = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(1024, 768)];
world.position = CGPointMake(0, 0); // this should be bottom-left
world.size = CGSizeMake(1024, 768);
world.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:world.frame];
world.physicsBody.categoryBitMask = worldCategory;
[self addChild:world];

// load in the game tiles (these are non-dynamic tiles the player can use)
[self loadInTiles];

// add in game object to the world skspritenode - this just creates a subclass of skspritenode and sets position to 0,0
[self addGameObject:CGPointMake(0, 0)];

...
}

// ...setup functions, input handling, etc

-(void)didSimulatePhysics {

// setup the player to move depending on their direction
[player updatePosition];

[self centreOnNode:player];
}

-(void)centreOnNode: (SKSpriteNode *)node {
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];

CGFloat x = node.parent.position.x - cameraPositionInScene.x;
CGFloat y = node.parent.position.y - cameraPositionInScene.y;
NSLog(@"camera x:%f y:%f", x, y);

NSLog(@"world frame origin x:%f y:%f", world.frame.origin.x, world.frame.origin.y);

node.parent.position = CGPointMake(x, y);
}

最佳答案

如果要将世界 Sprite 的原点设置为左下角,只需设置它的 anchor 即可。

world.anchorPoint = CGPointMake(0,0);

有了这个,世界 Sprite 的坐标系就和场景的默认坐标系一样了。

确保删除该行:

self.anchorPoint = CGPointMake(0.5, 0.5);

关于ios - 影响子节点定位的 Spritekit 场景 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21016863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com