2009-08-07 4 views
0

한 가지 문제가 있습니다. cpSegmentShapeNew를 회전시키기 위해 코딩을했는데 작동하지 않습니다.사용자가 cocos2d에서 화면을 터치 할 때 몸체를 회전시킬 수있는 방법

//**creating shape 
testBody = cpBodyNew(INFINITY, INFINITY); 
cpShape* testShape = cpSegmentShapeNew(testBody, cpv(230, 82), cpv(193, 46), 0.0f); 
testShape->e = 0.0; 
testShape->u = 0.0; 
testShape->data = flipper; 
testShape->collision_type = 2; 
cpSpaceAddStaticShape(space, testShape); 

//Body moving when user touch 
-(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
//event that starts when a finger touchs the screen 
UITouch *touch = [touches anyObject]; 
CGPoint tmpLoc = [touch locationInView: [touch view]]; 
CGPoint location = [[Director sharedDirector] convertCoordinate:tmpLoc]; 

ball.position = location; 
ballBody->p = location; 
[flipper runAction:[RotateTo actionWithDuration:0.1f angle:60]]; 

cpBodySetAngle(testBody, 60); 

cpvrotate(testBody->rot, cpv(100000,0)); 

return kEventHandled; 
} 

사람이 내가 잘못 곳을 가르쳐주세요, 다음 코드에 보라.

감사합니다.

답변

1

인사말,

문제는 당신이 코드를 통해 두 객체 (스프라이트 + 몸)를 회전하고 있다는 점이다.

당신이 필요로하는 것은 하나를 회전시키고, 다른 객체가 그것이 일어 났음을 알 수있게합니다. 당신이 몸을 이동하는 경우

예를 들어, 스프라이트를 업데이트하는 방법은 같아야합니다 :

void updateShapes(void* ptr, void* unused) 
{ 
cpShape* shape = (cpShape*)ptr; 
Sprite* sprite = shape->data; 
if(sprite) 
{ 
    cpBody* body = shape->body; 
    [sprite setPosition:cpv(body->p.x, body->p.y)]; 
    [sprite setRotation: (float) CC_RADIANS_TO_DEGREES(-body->a)]; 
} 
} 

코드의 마지막 줄은 회전을 업데이트합니다. 그게 네가 누락 된 라인이야.

앞으로 도움이되기를 바랍니다.

행운을 빌어 요 코코스 2 메이트!

요한 티.