Objective c 以主角问题为中心的精灵套装
我一直在试图让一个相机在雪碧工具包工作,从文件。当我在didSimulatePhysics中调用centerOnNode方法时,我的角色仍然在屏幕上移动,而不是停留在中心。谁能告诉我我做错了什么。我在这里看到过类似的问题,但一直无法发现我做错了什么,我是新的雪碧套件Objective c 以主角问题为中心的精灵套装,objective-c,ios7,sprite-kit,Objective C,Ios7,Sprite Kit,我一直在试图让一个相机在雪碧工具包工作,从文件。当我在didSimulatePhysics中调用centerOnNode方法时,我的角色仍然在屏幕上移动,而不是停留在中心。谁能告诉我我做错了什么。我在这里看到过类似的问题,但一直无法发现我做错了什么,我是新的雪碧套件 @implementation MyScene -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) {
@implementation MyScene
-(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];
self.anchorPoint = CGPointMake (0.5,0.5);//centralise the scenes anchor point
//self.size = CGSizeMake(1000,1000);
//create a world node to represent the scrolling world
SKNode *myWorld = [SKNode node];
[self addChild:myWorld];
SKNode *camera = [SKNode node];
camera.name = @"camera";
[myWorld addChild:camera];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"character.png"];
sprite.name = @"player";
sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[myWorld addChild:sprite];
SKAction *myAction = [SKAction moveTo:CGPointMake(50, 50) duration:7];
[sprite runAction:myAction];
}
return self;
}
- (void)didSimulatePhysics{
[self centerOnNode: [self childNodeWithName: @"player"]];
}
- (void) centerOnNode: (SKNode *) node{
NSLog(@"in centerOnNode");
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x,
node.parent.position.y - cameraPositionInScene.y);
}
@end
enter code here
我找到了我需要的解决办法。我没有将摄影机和播放器节点拉入didSimulatePhysics,然后将摄影机节点位置设置为播放器节点位置。下面的例子实现了我想要的,但如果有人有更好的方法,请建议:
#import "MyScene.h"
@implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
self.anchorPoint = CGPointMake (0.5,0.5);//centralise the scenes anchor point
self.size = CGSizeMake(2000,2000);//make scene bigger to see if world scrolls
//create a world node to represent the scrolling world
SKNode *myWorld = [SKNode node];
myWorld.name = @"world";
[self addChild:myWorld];
//create a camera node
SKNode *camera = [SKNode node];
camera.name = @"camera";
[myWorld addChild:camera];
//create two sprites, one with a moveTo action
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"character.png"];
sprite.name = @"player";
sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[myWorld addChild:sprite];//add sprite to world node
SKAction *myAction = [SKAction moveTo:CGPointMake(500, 500) duration:10];
[sprite runAction:myAction];
SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithImageNamed:@"character.png"];
sprite2.name = @"player";
sprite2.position = CGPointMake(CGRectGetMidX(self.frame)+50, CGRectGetMidY(self.frame)+50);
[myWorld addChild:sprite2];//add sprite to world node
}
return self;
}
- (void)didSimulatePhysics{
//pull in camera and player sprites
SKNode *camera = [self childNodeWithName: @"//camera"];
SKNode *playr = [self childNodeWithName: @"//player"];
//position camera where player is
camera.position = CGPointMake(playr.position.x, playr.position.y);
//center world on the camera
[self centerOnNode: camera];
}
- (void) centerOnNode: (SKNode *) node{
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent];
node.parent.position = CGPointMake(node.parent.position.x - cameraPositionInScene.x,
node.parent.position.y - cameraPositionInScene.y);
}
@end
你的相机节点在哪里?对不起,我在创建和添加我的世界节点之后创建了这个。我已经在上面的问题中编辑了代码,但它仍然不起作用,当我将@“camera”传递给centerOnNode函数时,它不起作用。那么有人知道为什么我们需要使用@”//camera和@”//player而不是@“camera”和@“player”?//是什么意思?我猜这是因为摄影机不是场景的孩子。相机是世界的孩子,也是场景的孩子。