2012-09-15 1 views
0

저는 Cocos2d에서 원수가 서로 다른 미리 정의 된 경로를 가로 지르는 간단한 2D 게임을 만들고 있습니다. 넥스트 프레임 방법은 다음의 코드를 갖는다 :왜 CCActionInterval을 재사용 할 수 있습니까?

int indexCount = 0; 
for (Enemy *e in enemies) { 

    if ([cocosGuy doesCollideWithRect: [e boundingBox]]) 
    { 
     for (Enemy *e in enemies) 
     { 
      [self removeChild: e cleanup: YES]; 
     } 
     [self startGame]; 
    } 
    //^This code not relevant to the question 
    if ([e numberOfRunningActions] == 0) 
    { 
     [e setPosition: [[enemy_positions objectAtIndex:indexCount] CGPointValue]]; 
     [e runAction: [beziers objectAtIndex: indexCount]]; 
    } 
    ++indexCount; 
} 

번째의 코드는 상기 문이 '베지'배열에서 'enemy_positions'어레이 및 CCActionInterval에서 CGPoint을 수행하고자하는 경우. 작동합니다 - 적의 경로가 완료되면 위치가 변경되고 작업이 다시 실행됩니다. 그러나 행동이 처음 실행 된 후에 왜 이렇게 깨지지 않습니까? CCActions가 한 번만 예정되어 있지 않습니까?

위치 및 동작을 단일 구조체로 리팩터링하고 싶으니까 내가 무엇이 먼저 진행되고 있는지 확인하고 싶습니다. CCAction 수업을 오해하고 있습니까?

-(NSArray*) makeBeziers { 

ccBezierConfig bezierconf1; 
bezierconf1.controlPoint_1 = ccp(-200, 5); 
bezierconf1.controlPoint_2 = ccp(300, 100); 
bezierconf1.endPosition = ccp(1000,5); 

ccBezierConfig bezierconf2; 
bezierconf2.controlPoint_1 = ccp(-200, 5); 
bezierconf2.controlPoint_2 = ccp(300, 100); 
bezierconf2.endPosition = ccp(1000,5); 

ccBezierConfig bezierconf3; 
bezierconf3.controlPoint_1 = ccp(-200, 5); 
bezierconf3.controlPoint_2 = ccp(300, 100); 
bezierconf3.endPosition = ccp(1000,5); 

NSArray *myarray; 

myarray = [[NSArray arrayWithObjects: [CCBezierBy actionWithDuration:3 bezier: bezierconf1], 
       [CCBezierBy actionWithDuration:3 bezier: bezierconf2], 
       [CCBezierBy actionWithDuration:3 bezier: bezierconf3], 
       nil] retain]; 
return myarray; 
} 

답변

0

이 "실수로"작동합니다

또한, 여기에 '베지'배열을 생성하기위한 현재의 팩토리 메소드입니다. 행동은 한 번만 생각해야합니다. 그들이 달리고 나서 풀려날거야.

그러나 작업을 별도의 배열에 저장 했으므로 이러한 작업은 유지됩니다. 따라서 다시 실행할 수 있습니다. 일부 작업에는 효과가 있고 다른 작업에는 미묘한 문제가 표시 될 수 있으며 일부 작업은 아무 것도하지 않거나 메모리 누출 또는 즉시 충돌이 발생할 수 있습니다.

액션을 재사용하는 것은 일반적으로 나쁜 행동으로 간주됩니다. 각 액션의 코드를 알지 못하고 재사용하면 "불량"이 발생하지 않는다는 것을 확인해야합니다.