2011-04-22 75 views
2

我想知道是否有人知道我將如何將CCSprite連接到cpConstraint並使其與物理更新。這裏是我的代碼:在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

可能要將此分成更小的問題,我想了很多的人被這個職位大小壓倒...只是說' – tallen11 2011-04-24 18:36:01

+0

我不知道我會如何分開這個問題。 – Stephen 2011-04-25 16:36:10

回答

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

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

//add to CCLayer with is self here 

[self addChild:s];