Spritekit场景锚点影响子节点定位
Posted
技术标签:
【中文标题】Spritekit场景锚点影响子节点定位【英文标题】:Spritekit scene anchorpoint affecting child node positioning 【发布时间】:2014-01-09 09:57:50 【问题描述】:我创建了我的 SKScene 子类,它设置锚点,然后为世界添加一个 SKSpriteNode,世界有多个 SKSpriteNodes 用于障碍物、玩家等。我也以
为中心我遇到的问题是,当我将场景的锚点设置为 (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);
【问题讨论】:
【参考方案1】:如果要将世界精灵的原点设置在左下方,只需设置它的锚点即可。
world.anchorPoint = CGPointMake(0,0);
这样,世界精灵的坐标系将与场景的默认坐标系一样。
确保删除该行:
self.anchorPoint = CGPointMake(0.5, 0.5);
【讨论】:
【参考方案2】:替换这一行:
world.position = CGPointMake(0, 0);
通过这个:
world.position = CGPointMake(-CGRectGetMidX(self.frame), -CGRectGetMidY(self.frame));
(0,0)
是场景的中心,因为您将SKScene
的锚点设置为 (0.5,0.5)
【讨论】:
这似乎不会影响世界 skspritenode 中节点的定位 - 请参阅 dropbox.com/s/grqx20o1lc461m7/…> - 目前游戏对象的位置设置为 0,0 但默认为中心 通过替换:[self addGameObject:CGPointMake(0, 0)];与: CGFloat theX = [playerX floatValue] - CGRectGetMaxX(self.frame); CGFloat theY = [playerY floatValue] - CGRectGetMaxY(self.frame); [self addGameObject:CGPointMake(theX, theY)];这似乎可行,虽然我还需要调整精灵的大小以上是关于Spritekit场景锚点影响子节点定位的主要内容,如果未能解决你的问题,请参考以下文章
iOS7 Sprite Kit如何在更改锚点时计算节点位置?
SpriteKit:场景中的静态背景与移动的 SKCameraNode