ios – 在SpriteKit中移动相机

前端之家收集整理的这篇文章主要介绍了ios – 在SpriteKit中移动相机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// UPDATE
已更新的代码已经按照我的预期工作.请参阅以下更新代码中的didSimulatePhysics方法.在我的情况下,我只关心在x轴上向左或向右移动一个字符,x轴上的0是绝对的左边,而在x轴上是一个可配置的值.苹果冒险游戏真的也很有帮助.

// ORIGINAL POST BELOW

我正在与苹果SpriteKit合作,我正在努力实现一个相机,就像我想要的那样.我在代码中所做的是在开始时加载一个精灵字符,两个按钮和一个红色框,该对话框在视图外部.我想要做的是用按钮移动角色,一旦播放器到达屏幕的中间或结尾,相机将重新调整,以发现视图中看不到的内容.所以向右移动应该最终显示一旦玩家到达那里,最初在视图之外的红色框.但是,使用下面的代码,我无法让相机跟随并将坐标调整到主角色.我看过苹果公司的高级场景处理文档以及一些其他的堆栈溢出帖子,但似乎无法正确.如果任何人可以提供一些建议,将不胜感激.

#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,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)创建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和精灵按钮.

猜你在找的iOS相关文章