How do you make a sprite rotate in cocos2D while using SpaceManager

拜拜、爱过 提交于 2019-12-23 03:22:38

问题


I just have a simple sprite - how can I get it to rotate?

A good answer would show how to rotate both a dynamic sprite and a static_mass sprite


回答1:


If the sprite is dynamic / non-static, just do like so:

 cpBodySetAngVel(ObjSmSprite.shape->body,0.25); 

For a static body, you can do something like this:

[ObjSmStaticSprite.shape runAction:[CCRepeatForever actionWithAction:
                         [CCSequence actions:
                          [CCRotateTo actionWithDuration:2 angle:180],
                          [CCRotateTo actionWithDuration:2 angle:360],
                          nil]

                         ]];


smgr.rehashStaticEveryStep = YES; //Setting this would make the smgr recalculate all static shapes positions every step

To Summarize, here is a spinning static sprite, following be spinning dynamic sprite.

  // Add Backboard
    cpShape *shapeRect = [smgr addRectAt:cpvWinCenter mass:STATIC_MASS width:200 height:10 rotation:0.0f ];// We're upgrading this 
  cpCCSprite * cccrsRect = [cpCCSprite spriteWithShape:shapeRect file:@"rect_200x10.png"];
    [self addChild:cccrsRect];

  // Make static object update moves in chipmunk
  // Since Backboard is static, and since we're going to move it, it needs to know about spacemanager so its position gets updated inside chipmunk.
  // Setting this would make the smgr recalculate all static shapes positions every step
//  cccrsRect.integrationDt = smgr.constantDt;
//  cccrsRect.spaceManager = smgr;
  // Alternative method: smgr.rehashStaticEveryStep = YES;
  smgr.rehashStaticEveryStep = YES;

  // Spin the backboard
  [cccrsRect runAction:[CCRepeatForever actionWithAction:
                         [CCSequence actions:
                          [CCRotateTo actionWithDuration:2 angle:180],
                          [CCRotateTo actionWithDuration:2 angle:360],
                          nil]

                         ]];


  // Add the hoop
  cpShape *shapeHoop = [smgr addCircleAt:ccp(winSize.width/2.0f,winSize.height/2.0f - 55.0f) mass:1.0 radius: 50 ];
  cpCCSprite * cccrsHoop = [cpCCSprite spriteWithShape:shapeHoop file:@"hoop_100x100.png"];
    [self addChild:cccrsHoop];
  cpBodySetAngVel(cccrsHoop.shape->body,0.25); 

Disclaimer: I'm trying to answer my own question, and this answer might be missing important subtleties - it just represents what I know, so far.




回答2:


If using cocos2d

use this code in tick to update positon all time _number1.position is the position you would update to, so when _number1 moves _logo2 will rotate to face it _logo2.rotation = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(_number1.position, _logo2.position)));

update _logo1 rotation by touch put this code in touch event handler

_logo2.rotation = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, _logo2.position)));

use this as an action

[_logo2 runAction:[CCRotateTo actionWithDuration:0.0 angle:CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, _logo2.position)))]];

hope this helps someone took me ages to work it out



来源:https://stackoverflow.com/questions/2691589/how-do-you-make-a-sprite-rotate-in-cocos2d-while-using-spacemanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!