0

저는 게임 개발에 매우 ​​신선하고 NinjaJump과 같은 게임을 개발해야합니다.Cocos2D v3 CCParallaxNode 스크롤로 플레이어의 초점을 유지할 수 없습니다.

스크롤 배경을 설정하기 위해 CCParallaxNode를 만들고 Physics world를 설정하기 위해 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]; 

는 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]; 

우리가 내가 어쨌든 화면 밖으로하지만 선수 스크롤을 이해할 수 없다

- (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)); 
    } 
} 

으로 update 방법을 구현 한 플레이어가 항상 볼 수 있도록합니다. 이 코드는 Cocos2d v3로 작성되었습니다. 도움의 어떤 종류의 감사 https://www.dropbox.com/s/5s55d00kk80wun4/HumptyJump-Example.zip?dl=0

:

는 내가 구현 무엇을 보여줄 수있는 데모 프로젝트도 설치해야합니다. 미리 감사드립니다.

답변

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 

에서 동일한를 참조하십시오.

0

샘플 코드를 실행할 수 없지만 여기에는 물리 엔진이 필요 없음을 알려 드릴 수 있습니다.

플레이어를 특정 높이로 유지하면 개체를 오른쪽에서 왼쪽으로 이동할 수 있습니다. 움직이는 시차 배경 이미지와 오브젝트를 적용하여 캐릭터가 위 또는 아래로 움직이는 느낌을줍니다.

위와 같은 방법으로 만들어진 Swing Drop과 같은 게임을 참고하면 쉽게 볼 수 있습니다.

+0

감사합니다. 그렇지만 내 플레이어가 전투를 통해 물리 엔진을 필요로하는 장애물을 구현할 것입니다. 하지만 당신이 말한 해결책을 구현하려고 노력할 것입니다. –

+0

물리 엔진을 충돌 감지에만 사용하는 경우 물리 엔진을 사용하지 않는 것이 좋습니다. 스프라이트 경계를 단지 비교함으로써 충돌 탐지를 할 수 있습니다. 내가했던 레퍼런스 게임도 똑같이했다. 심지어 당신도 물리 엔진을 사용할 수 있지만 적어도 지금은 당신이하는 것처럼. 포 그라운드 노드를 움직이기 시작하면 PlatformNode가 플레이어를 매우 강하게 밀어 내고 화면 밖으로 나옵니다. – Shoaib

관련 문제