0

我对游戏开发非常陌生,我需要开发像NinjaJump这样的游戏。

我创建了一个 CCParallaxNode 来设置滚动背景,并添加了 CCPhysicsNode 来设置物理世界。我创建了如下所示的播放器对象。

// Add a sprite
    _sprite = [CCSprite spriteWithImageNamed:@"Icon.png"];
    _sprite.position  = ccp(self.contentSize.width/2,100);
    _sprite.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _sprite.contentSize} cornerRadius:0.0];
    _sprite.physicsBody.friction = 0.0f;
    _sprite.physicsBody.collisionGroup = @"player";
    _sprite.physicsBody.collisionType = @"Player";
    //_sprite.physicsBody.collisionMask = 0;
    //[self addChild:_sprite];
    [foreground addChild:_sprite];

foreground只是添加到 CCScene 中的一个节点,用于轻松管理焦点玩家。

// code for physics world
_physicsWorld = [CCPhysicsNode node];
    _physicsWorld.gravity = ccp(0,-100);
    _physicsWorld.debugDraw = YES;
    //_physicsWorld.collisionDelegate = self;
    [self addChild:_physicsWorld];

    _foreground = [CCNode node];
    //[self addChild: _foreground];
    [_physicsWorld addChild: _foreground];

为了让玩家始终可见,我们将update方法实现为

- (void) update:(CFTimeInterval)currentTime {
    // Calculate player y offset
    if (_player.position.y > 200.0f) {
        //_midgroundNode.position = CGPointMake(0.0f, -((_player.position.y - 200.0f)/4));
        _foreground.position = CGPointMake(0.0f, -(_player.position.y - 200.0f));
    }
}

我无法理解,但播放器无论如何都会滚出屏幕。代码使用 Cocos2d v3 编写。

我还设置了一个演示项目来展示我实现的内容:https ://www.dropbox.com/s/5s55d00kk80wun4/HumptyJump-Example.zip?dl=0

任何形式的帮助表示赞赏。提前致谢。

4

2 回答 2

0

我无法运行您的示例代码,但有一件事,我可以肯定地告诉您这里不需要物理引擎。

您只需将播放器保持在特定高度,然后将对象从右向左移动。应用移动视差背景图像和对象,以使您的角色有一种向上或向下移动的感觉。

作为参考,您可以查看类似Swing Drop的游戏,它们采用与上述相同的方法制作。

于 2014-10-12T15:44:43.457 回答
0

我已经实现了 2 个固定视图,在物理体下降时改变它们的位置;)。

在行动中看到相同的

@interface TestScene () {
    CCNode *_background;
    CCNode *_foreground;

    NSArray *grounds; // Keeps track of each of the 2 views
    CCPhysicsNode *_physicsWorld;
}

@implementation TestScene

- (id)init {
    self = [super init];
    if (!self) return(nil);

    // Enable touch handling on scene node
    self.userInteractionEnabled = YES;

    // Physics world setup
    _physicsWorld = [CCPhysicsNode node];
    _physicsWorld.gravity = ccp(0,-100);
    _physicsWorld.debugDraw = YES;
    _physicsWorld.collisionDelegate = self;
    [self addChild:_physicsWorld];

    // Foreground node in which platforms are there and moves downwards as player goes up
    _foreground = [CCNode node];

    // Adding background images
    CCSprite *default1 = [CCSprite spriteWithImageNamed: @"Default.png"];
    [default1 setAnchorPoint: ccp(0, 0)];
    CCSprite *default2 = [CCSprite spriteWithImageNamed: @"Default.png"];
    [default2 setAnchorPoint: ccp(0, 0)];
    [default2 setPosition: ccp(0, default1.contentSize.height)];
    [_foreground addChild: default1];
    [_foreground addChild: default2];

    // Adding into array
    grounds = @[default1, default2];
    // Adding into physics world
    [_physicsWorld addChild: _foreground];

    // creating player
    _player = [CCSprite spriteWithImageNamed: @"Assets.atlas/Player.png"];
    [_player setPosition: ccp(160.0f, 160.0f)];
    _player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:0]; // 1
    _player.physicsBody.collisionGroup = @"playerGroup"; // 2
    _player.physicsBody.collisionType = @"player";
    //[_physicsWorld addChild:_player];
    [_foreground addChild: _player];

    // Multiple platforms can be added into body, they are static entities only
    PlatformNode *platform = (PlatformNode *)[PlatformNode node];
    [platform createPlatformAtPosition:CGPointMake(110, 50) ofType: PLATFORM_NORMAL];
    [_foreground addChild: platform];

    return self;
}

- (void) update:(CFTimeInterval)currentTime {
    // Take background and physics world down, 163 was Y position of the player
    _physicsWorld.position = ccp(_physicsWorld.position.x, 163 - _player.position.y);

    // loop the ground
    for (CCNode *ground in grounds) {
        // Get the world position
        CGPoint groundWorldPosition = [_physicsWorld convertToWorldSpace: ground.position];
        // Get screen position
        CGPoint groundScreenPosition = [self convertToNodeSpace: groundWorldPosition];
        NSLog(@"Positioning ground ---> world: (%f, %f) & screen: (%f, %f)", groundWorldPosition.x, groundWorldPosition.y, groundScreenPosition.x, groundScreenPosition.y, nil);
        if (groundScreenPosition.y <= -ground.contentSize.height) {
            ground.position = ccp(ground.position.x, ground.position.y + 2 * ground.contentSize.height);
            break;
        }
    }
}

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    // Take the player upside
    [_player.physicsBody applyImpulse: ccp(0, 215)];
}

@end

这就是我编码背景的方式:)。

于 2014-10-23T11:25:45.877 回答