2013-03-16 4 views
0

저는 cocos2d/box2d를 처음 사용합니다. 나는 아주 간단한 게임을 개발 중입니다.운동기구의 Cocos2d/Box2d 이상한 동작

1) 몸은 처음에 정적 - :

#import "Machine.h" 

@implementation Machine 

+(id)machineWithWorld:(b2World*)world position:(CGPoint)pos 
{ 
return [[[self alloc] initWithWorld:world position:pos] autorelease]; 
} 

-(id)initWithWorld:(b2World*)world position:(CGPoint)pos 
{ 

if(self = [super initWithShape:[AppDelegate renameFrameForIpad:@"machine"] inWorld:world]) 
{ 
    size = [CCDirector sharedDirector].winSize; 

    self.body->SetTransform([Helper toMeters:pos], 0.0); 
    self.body->SetType(b2_staticBody); 

    safetyCounter = 5; 
    [self schedule:@selector(machineSafetyCounter)]; 

    movementWidthInMeters = (size.width-self.contentSize.width)/PTM_RATIO; 
    linearSpeed = 0.5; 
    [self schedule:@selector(startMoving) interval:1.5]; 
} 

return self; 

}

프라그 마크

-(void)startMoving 
{ 
[self unschedule:_cmd]; 

float distanceFromCenterInMeters = (size.width/2 - self.position.x)/PTM_RATIO; 
float interval = ABS(distanceFromCenterInMeters/linearSpeed); 
if(interval < 0.01f) 
    interval = 0.02f; 

b2Vec2 motionDirection = (distanceFromCenterInMeters > 0.0f) ? b2Vec2(1.0, 0.0) : b2Vec2(-1.0, 0.0); 
self.body->SetType(b2_kinematicBody); 
self.body->SetLinearVelocity(linearSpeed*motionDirection); 

[self schedule:@selector(startMotionFromBeginning) interval:interval-0.01]; 

CCLOG(@"startMoving distance-->%f, interval-->%f", distanceFromCenterInMeters, interval); 
} 

-(void)startMotionFromBeginning 
{ 
[self unschedule:_cmd]; 

float interval = (movementWidthInMeters/2)/linearSpeed; 

self.body->SetLinearVelocity(0.5*b2Vec2(1.0, 0.0)); 
[self schedule:@selector(moveRTL) interval:interval-0.01]; 

[self schedule:@selector(checkIfHelmetIsBelowMachine) interval:0.1]; 

CCLOG(@"startMotionFromBeginning interval-->%f", interval); 
} 

-(void)moveRTL 
{ 
[self unschedule:_cmd]; 

float interval = movementWidthInMeters/linearSpeed; 

self.body->SetLinearVelocity(0.5*b2Vec2(-1.0, 0.0)); 
[self schedule:@selector(moveLTR) interval:interval-0.01]; 

CCLOG(@"moveRTL interval-->%f", interval); 
} 

-(void)moveLTR 
{ 
[self unschedule:_cmd]; 

float interval = movementWidthInMeters/linearSpeed; 

self.body->SetLinearVelocity(0.5*b2Vec2(1.0, 0.0)); 
[self schedule:@selector(moveRTL) interval:interval-0.01]; 

CCLOG(@"moveLTR interval-->%f", interval); 
} 

-(void)checkIfHelmetIsBelowMachine 
{ 
[self unschedule:_cmd]; 

Helmet* helmet = (Helmet*)[[[[[CCDirector sharedDirector] runningScene] children] objectAtIndex:0] getChildByTag:kTagHelmet]; 
float helmetPosX = helmet.position.x; 

if((self.position.x > helmetPosX) && (self.position.x < helmetPosX+helmet.contentSize.width)) 
{ 
    [self unscheduleAllSelectors]; 
    [self schedule:@selector(machineSafetyCounter) interval:0.1]; 

    [self schedule:@selector(startMovingDownwards) interval:0.0]; 

    return; 
} 

[self schedule:_cmd interval:0.1]; 
} 

-(void)startMovingDownwards 
{ 
[self unschedule:_cmd]; 

self.body->SetLinearVelocity(0.25*b2Vec2(0.0, -1.0)); 
[self schedule:@selector(stopMovingDownwards) interval:1.0]; 

CCLOG(@"startMovingDownwards"); 
} 

-(void)stopMovingDownwards 
{ 
[self unschedule:_cmd]; 

self.body->SetLinearVelocity(b2Vec2(0.0, 0.0)); 
[self schedule:@selector(startMoving) interval:0.2]; 

CCLOG(@"stopMovingDownwards"); 
} 

내가 행한 모든 따르고 - 다음은 기계의 동작 코드라는 이름의 내 적 클래스의 ccp (size.width * 0.5, size.height * 0.75)에 배치됩니다.

2) 1.5 초 후 운동 학적이되어 0.5m/s의 선 속도로 움직이기 시작합니다.

3) 화면 너비 센터에서 높이를 유지하면서 현재 거리를 확인하고 그 지점에 도달하는 데 필요한 시간을 평가 한 다음 해당 방향으로 수평으로 이동하기 시작합니다.

4) 그 지점에 도달하면, 그것은 서명 동작을 시작합니다. 왼쪽에서 오른쪽으로 움직이기 시작합니다. 헬멧 (다른 게임 개체)이 아래를지나 가면 1.0 초 후 움직이기 시작하고 멈 춥니 다. 전체 사이클 반복.

5) 헬멧을 발견하면 아래로 내려 가기 시작할 때까지 LTR & RTL을 움직입니다.

문제는 때로는 동작이 예상 한 것과 동일 할 때도 있습니다. 그리고 여러 번, 위쪽으로 움직이기 시작합니다. 그리고 나는 결코 양의 방향으로 움직임 벡터에 대해 y 비트를 설정하지 않았습니다.

누구든지 나에게 무엇을 제안 할 수 있습니까? 제가 여기서 잘못하고 있습니까?

미리 감사드립니다 ... !!

+0

SetLinearVelocity에서 중단 점을 설정하거나 로그 문을 추가하여 양의 y 좌표로 호출을 수신했는지 확인하십시오. – LearnCocos2D

답변

0

몸체를 초기화 한 후에는 몸체를 변경하지 말아야합니다. bodyType. 몸체를 처음 만들 때 나중에 그것을 변경하려고 할 때 문제가 발생하지 않도록 기구학으로 설정하십시오. 본체가 CCSprite에 연결되어 있습니까? 그렇다면 클래스를 CCSprite의 서브 클래스로 만들어야
[super initWithFile:(NSString *) world:(b2World *)] 또는 원하는 방법으로 노력을 용이하게 할 수 있습니다.