限制精灵旋转 - Cocos2d
问题描述:
我有两只手在屏幕的两侧完全不动,两只手指旋转360度。我想限制拇指旋转,这意味着我只需要他们能够像普通拇指一样旋转。我是新来的cocos2d,所以任何帮助将不胜感激。以下是我迄今为止限制精灵旋转 - Cocos2d
#import "cocos2d.h"
@interface GameScene : CCLayer {
CGFloat lthumbRotation, rthumbRotation;
CCSprite *lthumb, *rthumb;
}
+(CCScene *) scene;
@end
------------------------------------
#import "GameScene.h"
@implementation GameScene
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
GameScene *layer = [GameScene node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if ((self = [super init]))
{
lthumb = [CCSprite spriteWithFile:@"lthumb.png" rect:CGRectMake(0,0, 145, 59)];
lthumb.position = ccp(100, 140);
lthumb.anchorPoint = ccp(0.3, 0.8);
[self addChild:lthumb z:0];
rthumb = [CCSprite spriteWithFile:@"rthumb.png" rect:CGRectMake(0,0, 145, 59)];
rthumb.position = ccp(380, 140);
rthumb.anchorPoint = ccp(0.7, 0.8);
[self addChild:rthumb z:0];
[self scheduleUpdate];
}
return self;
}
-(void)update:(ccTime)delta
{
lthumb.rotation = lthumbRotation;
rthumb.rotation = rthumbRotation;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, rthumb.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, rthumb.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
lthumbRotation += currentTouch - previousTouch;
rthumbRotation -= currentTouch - previousTouch;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void) dealloc
{
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
[super dealloc];
}
@end
这让两个拇指同时移动向上/向下的角度与锚点在拇指(像个关节)的底部。
我还需要旋转拇指以在触摸结束后重置回0。
在此先感谢
答
在你ccTouchesMoved:withEvent:
方法,简单地检查,看看是否新的旋转是将其应用到你的精灵之前给定的阈值内。
最后,在ccTouchesEnded:withEvent:
,你可以简单地设置旋转值回0:
lthumbRotation = 0;
rthumbRotation = 0;
对不起我仍然很新的这一点,你可以解释通过检查,看是否新的旋转是你的意思在将其应用于精灵之前,在给定的阈值内?另外,“lthumbRotation = 0;”工作。出于某种原因,我试图将位置设置为0(这是漫长的一天)。非常感谢你。 – Jonathan 2011-04-25 04:20:54