2014-02-21 4 views
1

캐릭터가 영원히 오른쪽으로 실행되는 게임을 만들려고합니다 (게임은 가로 방향입니다). 지상에는 캐릭터가 뛰어 넘을 수있는 스파이크가 있습니다. 현재는 캐릭터가 특정 거리에 도달하면 무작위로 구성된 스파이크가 만들어지고 체크 포인트 거리가 뒤로 밀리는 등 거의 체크 포인트와 같은 스타일로 스파이크가 새로 생성됩니다 (다소 임의적입니다). . 스파이크와 함께, 나는 땅을 이루는 타일을 만드는 데 사용되는 별개이지만 매우 유사한 체크 포인트와 같은 시스템을 가지고 있습니다.특정 노드를 제거하는 스프라이트 키트

- (void) didSimulatePhysics { 
if (player.position.x > endlessX) { 
    int random = player.position.x + self.frame.size.width; 
    [self createSpike:random]; 
    endlessX += self.frame.size.width/2.2 + arc4random_uniform(30); 
} 
if (player.position.x + self.frame.size.width > endlessGroundX) { 
    [self createGround:endlessGroundX]; 
    endlessGroundX += tile1.frame.size.width; 
} 
    [self centerOnNode: player]; 
} 

createSpike 및 createGround 방법의 파라미터는 SKSpriteNodes위한 단지 'X'의 값이다 이 부분 내 코드는 'endlessX'및 'endlessGroundX'는 검사 값이다. 캐릭터 자체가 움직이며 스파이크와 타일이 고정되어 있으므로 현재이 기능을 사용하고 있습니다. 즉, 캐릭터가 마찰 또는 같은 다른 힘의 운동 에너지 중 하나를 잃지 않을 것으로

-(void) createPlayer { 

player = [SKSpriteNode spriteNodeWithImageNamed:@"base"]; 
player.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); 
player.name = @"player"; 
player.zPosition = 60; 
player.xScale = 0.8; 
player.yScale = 0.8; 
player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:player.frame.size.height/2]; 
player.physicsBody.mass = 1; 
player.physicsBody.linearDamping = 0.0; 
player.physicsBody.angularDamping = 0.0; 
player.physicsBody.friction = 0.0; 
player.physicsBody.restitution = 0.0; 
player.physicsBody.allowsRotation = NO; 
player.physicsBody.dynamic = YES; 
player.physicsBody.velocity = CGVectorMake(400, 0); 
player.physicsBody.categoryBitMask = playerCategory; 
player.physicsBody.collisionBitMask = wallCategory; 
player.physicsBody.contactTestBitMask = wallCategory | spikeCategory; 
[myWorld addChild:player]; 
    } 

: 이것은 내가 캐릭터를 만드는 오전 방법이다.

- (void) centerOnNode: (SKSpriteNode *) node { 
CGPoint cameraPositionInScene = [node.scene convertPoint:node.position fromNode:node.parent]; 
node.parent.position = CGPointMake(self.frame.size.width/5 + node.parent.position.x - cameraPositionInScene.x, node.parent.position.y); 
    } 

내가 '에서이 메서드를 호출 오전 : 그럼, 캐릭터가 항상 화면에 같은 x 방향의 위치에 남아 있도록 사과 자신의 모험 게임에서 사용되는 방법'노드 센터 '를 사용하고 있습니다 didSimulatePhysics. '

When I run this for some time, the programs gets slower and slower. I am guessing that that is due to the fact that I am never removing these nodes and they are always being added. However, to fix this problem, I tried doing something like this: 

-(void)update:(CFTimeInterval)currentTime { 
[self enumerateChildNodesWithName:@"*" usingBlock:^(SKNode *node, BOOL *stop) { 
    if (node.position.x + 50 < player.position.x) { 
     [node removeFromParent]; 
    } 
}]; 
} 

합니다 (+50 단지 노드를 제거하기 전에 화면 밖으로 있는지 확인하는 것) 그러나, 나는이 대신 '경우'문을 만족하는 특정 노드를 제거했을 때, 프로그램은 모든 스프라이트 노드를 제거합니다. 이 문제를 해결하기 위해 누락 된 다른 방법이나 무언가가 있습니까? 아니면 특정 노드를 제거하는 다른 간단한 방법이 있습니까?

+0

디버거 사용 방법을 알고 있습니까? if 문 블록 내에서 중단 점을 설정하고 값이 예상 한 값인지 확인하는 것은 어떻습니까? – prototypical

답변

2

예를 들어 스파이크를 애니메이션화하는 방법과 같이 세부 사항이 부족하여 너무 구체적이지 않습니다.

SKAction *moveSpikeAction = [SKAction moveToX:-50 duration:5]; 
    SKAction *removeSpikeAction = [SKAction removeFromParent]; 
    SKAction *spikeSequence = [SKAction sequence:@[moveSpikeAction, removeSpikeAction]]; 
    [yourSpikeSpriteNode runAction:spikeSequence]; 

아이디어는 단순히 스파이크가 오프 스크린 위치로 애니메이션 때 당신이 removeFromParent 조치를 사용하는 것이되는 것은 그것을 취소 : 그럼에도 불구하고, 당신이 공유하고있는 것과 당신이 조금이 같은 뭔가를 찾고 수 있습니다 추측 .

+0

방금 ​​내 글을 편집했습니다. 미안, 방금 설명했던 방식으로 생각하지도 않았다. 현재 나는 캐릭터 이동을하고 스파이크와 타일은 고정되어 있습니다. 내가 가지고있는 형식으로 문제를 해결할 방법이 있습니까? 또는 노드 방법에서 중심을 제거하고 설명 된 방식으로 전환하는 것이 더 간단할까요? – user2929299

+0

나는 그 프로젝트에 꽤 달려 있다고 말할 수 있습니다. 게임이 끊임없이 한 축을 움직이는 끝없는 주자라면, 그렇다면 내가 말하는 것이 가장 쉬운 방법이라고 믿습니다. 몇 가지 방향으로 움직이는 속도와 사용자가 속도를 제어 할 때 좀 더 복잡한 무언가를 만들면 더 나은 솔루션을 찾을 수있을 것입니다 ... –

+0

나는 x 축에 일정하게 유지하려고 노력하고 있습니다. 그런 식으로 전환하십시오! 그러나 사람이 플레이 할 때 어떻게 이러한 스파이크를 지속적으로 만들 수 있습니까? – user2929299

관련 문제