在SpriteKit中移动相机

时间:2014-05-25 01:46:11

标签: ios sprite-kit

//更新 已添加的更新代码按预期工作。请参阅下面更新的代码中的didSimulatePhysics方法。在我的例子中,我只关心在x轴上向左或向右移动一个字符,其中x轴上的0是x轴上的绝对左边和右边是可配置的值。苹果冒险游戏确实帮了很多。

//以下原始帖子

我正在使用Apple SpriteKit,我正在努力实现一个我希望它表现的相机。我在代码中所做的是加载精灵字符,两个按钮和一个红色框,它在开始时位于视图外部的右侧。我希望能够做的是用按钮移动角色,一旦玩家到达屏幕的中间或末端,摄像机将重新调整以发现视图中无法看到的内容。所以向右移动应该最终显示一旦玩家到达那里,最初在视图之外的红色框。但是,根据我在下面使用的代码,我无法让相机跟随并调整主角的坐标。我看过Apple的高级场​​景处理文档以及其他一些堆栈溢出帖子,但似乎无法做到正确。如果有人能提出一些建议,我们将不胜感激。

enter image description here

#define cameraEdge 150

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        /* Setup your scene here */
        //320 568


        self.backgroundColor = [SKColor whiteColor];

        myWorld = [[SKNode alloc] init];
        [self addChild:myWorld];

        mainCharacter = [SKSpriteNode spriteNodeWithImageNamed:@"0"];
        mainCharacter.physicsBody.dynamic = YES;
        mainCharacter.name = @"player";

        mainCharacter.position = CGPointMake(20, 20);

        CGRect totalScreenSize = CGRectMake(0, 0, 800, 320);

        SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(60, 60)];

          SKSpriteNode *boxTwo = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(60, 60)];
           SKSpriteNode *boxThree = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(60, 60)];
        boxThree.position = CGPointMake(40, 50);

        [myWorld addChild:boxThree];

        boxTwo.position = CGPointMake(1100, 50);

        box.position = CGPointMake(650, 50);

        [myWorld addChild:box];
        [myWorld addChild:boxTwo];


       self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:totalScreenSize];

        self.physicsWorld.gravity = CGVectorMake(0, -5);
        mainCharacter.name = @"mainCharacter";


        mainCharacter.physicsBody.linearDamping = 0;
        mainCharacter.physicsBody.friction = 0;
        mainCharacter.physicsBody.restitution = 0;
        mainCharacter.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:mainCharacter.size];

       [myWorld addChild:mainCharacter];

        [self addChild:[self buildLeftButton]];
        [self addChild:[self buildRightButton]];

    }

    return self;
}

- (void)didSimulatePhysics
{
    SKSpriteNode *hero = mainCharacter;

    if(hero)
    {
        CGPoint heroPosition = hero.position;
        CGPoint worldPosition = myWorld.position;

        NSLog(@"%f", heroPosition.x);

        CGFloat xCoordinate = worldPosition.x + heroPosition.x;

        if(xCoordinate < cameraEdge && heroPosition.x > 0)
        {
            worldPosition.x = worldPosition.x - xCoordinate + cameraEdge;
            self.worldMovedForUpdate = YES;
        }

        else if(xCoordinate > (self.frame.size.width - cameraEdge) && heroPosition.x < 2000)
        {
            worldPosition.x = worldPosition.x + (self.frame.size.width - xCoordinate) - cameraEdge;
            self.worldMovedForUpdate = YES;
        }

        myWorld.position = worldPosition;
    }
}

-(SKSpriteNode *)buildLeftButton
{
    SKSpriteNode *leftButton = [SKSpriteNode spriteNodeWithImageNamed:@"left"];
    leftButton.position = CGPointMake(20, 20);
    leftButton.name = @"leftButton";
    leftButton.zPosition = 1.0;
    return leftButton;
}

-(SKSpriteNode *)buildRightButton
{
    SKSpriteNode *leftButton = [SKSpriteNode spriteNodeWithImageNamed:@"right"];
    leftButton.position = CGPointMake(60, 20);
    leftButton.name = @"rightButton";
    leftButton.zPosition = 1.0;
    return leftButton;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if([node.name isEqualToString:@"leftButton"])
    {
             [mainCharacter.physicsBody applyImpulse:CGVectorMake(-120, 0)];
    }

    else if([node.name isEqualToString:@"rightButton"])
    {
       [mainCharacter.physicsBody applyImpulse:CGVectorMake(120, 10)];
    }
}

1 个答案:

答案 0 :(得分:22)

如果您希望视图始终以您的播放器位置为中心,请记住以下几点修改您的代码:

1)创建一个SKNode并将其命名为myWorld,worldNode或任何其他名称。

2)添加worldNode [self addChild:worldNode];

3)将所有其他节点添加到worldNode,包括您的播放器。

4)在didSimulatePhysics方法中,添加以下代码:

worldNode.position = CGPointMake(-(player.position.x-(self.size.width/2)), -(player.position.y-(self.size.height/2)));

您的视图现在将始终以您的玩家的位置为中心。

2015年5月更新:

如果您使用的是使用Tiled Map Editor创建的地图,则可以使用免费的SKAToolKit framework。功能包括玩家相机自动跟踪,测试播放器,测试HUD和精灵按钮。