2011-04-22 2 views
2

CCSpritecpConstraint에 연결하고 물리로 업데이트하는 방법을 알고있는 사람이 있는지 궁금합니다.Cocos2d에서 CCSprites 사용

upper = [game.spaceManager addPolyAt:cpv(70,195) mass:300 rotation:0 numPoints:6 points:cpv(2,12), cpv(28,8), cpv(33,0), cpv(36,-10), cpv(-33,-10), cpv(-20,8)]; 
upper->collision_type = kTireCollisionType; 

cpShape *lower = [game.spaceManager addPolyAt:cpv(70,125) mass:300 rotation:0 numPoints:7 points:cpv(34,8), cpv(31,0), cpv(25,-9), cpv(7,-13), cpv(-20,-8), cpv(-30,0), cpv(-35,8)]; 
lower->collision_type = kTireCollisionType; 

//HIT POINT 
cpShape *sensor = [game.spaceManager addCircleAt:cpv(70,160) mass:10 radius:10]; 
sensor->sensor = YES; 
sensor->collision_type = kScoreCollisionType; 

cpShape *sensor2 = [game.spaceManager addCircleAt:cpv(70, 160) mass:10 radius:10]; 
sensor2->sensor = YES; 

//Combine them into one body! 
[game.spaceManager combineShapes:upper, lower, sensor, sensor2, nil]; 

//The "rope" 
cpVect a1 = cpv(0,30);  //Local coordinates of tire 
cpVect a2 = cpv(70,320); //World coordinates (staticBody is at (0,0)) 

//calculate the length of the rope 
float max = cpvdist(cpBodyLocal2World(upper->body, a1), a2); 
cpConstraint *rope = [game.spaceManager addSlideToBody:upper->body fromBody:game.spaceManager.staticBody toBodyAnchor:a1 fromBodyAnchor:a2 minLength:1 maxLength:max]; 

cpConstraintNode *ropeNode = [cpConstraintNode nodeWithConstraint:rope]; 
ropeNode.color = ccBLUE; 
[game addChild:ropeNode]; 

//Attach a sprite to the sensor, lucky for us it's exactly in the center 
//of the tire... otherwise this wouldn't work 
[game addChild:[super initWithShape:sensor file:@"TractorTireFront.png"] z:3]; 
[game addChild:[cpCCSprite spriteWithShape:sensor2 file:@"TractorTireBack.png"] z:1]; 

ivGame = game; 

//Free the shape when we are released 
self.spaceManager = game.spaceManager; 
self.autoFreeShape = YES; 

[self schedule:@selector(step:) interval:.1]; 

return self; 

이것은 init 메서드에 대한 코드입니다 : 여기 내 코드입니다. 나는 화면에 타이어가 있고, 당신이 볼 수 있듯이, 화면 상단에서 걸려 있고 타이어 상단에 붙어있는 로프 역할을하는 cpConstraint이 있습니다. 어떻게 든이 제약에 CCSprite을 붙이고 싶습니다. 그리고 타이어가 흔들 리거나, 또 다른 방법으로 CCSprite을 사용하고 타이어가 흔들 리도록 업데이트해야합니다.

저는 cocos2d + spaceManager를 사용하고 있습니다. 사람들이 CCSprite을 사용하여 타이어 움직임으로 업데이트한다고 말하는 것을 들었지만,이를 달성하기에 cocos2d 또는 spaceManger에 익숙하지 않습니다. 누군가 코드 스 니펫이나 튜토리얼을 가지고 있다면이 문제를 어떻게 해결할 수 있는지 보여줍니다. 나는 그것을 매우 감사 할 것이다.

+0

작은 질문에이를 분리 할 수있는 CCSprite를 초기화하지 않는, 내가 많이 생각 사람들이이 게시물 크기에 압도됩니다 ... 그냥 선생님 ' – tallen11

+0

나는이 질문을 어떻게 분리할지 모르겠다. – Stephen

답변

0

나는 다람쥐를 알고 있지만 당신이 당신의 틱 방법에 넣어 것입니다 것은

-(void) tick() { 

// not sure how you get position in chipmunk, somehow convert it to cgPoint 
CGPoint * p = ccp(cpConstraint->position.x, cpConstraint->position.y); 

//find out the offset of the tire compared to the cpConstraint then add it to p 

p = ccp(p.x + offset.x, p.y + offset.y); 

//assign it to your CCSprite 

sprite.position = p; 

} 

CCSprite * s = [CCSprite spritreWithFile:"picture.png"]; 

//add to CCLayer with is self here 

[self addChild:s]; 
관련 문제