限制精灵旋转 - Cocos2d

发布于 2024-11-03 03:36:17 字数 2633 浏览 0 评论 0原文

我有两只手放在屏幕两侧,根本不动,还有两个拇指可以 360 度旋转。我想限制拇指旋转,这意味着我只需要它们能够像正常拇指一样旋转。我是 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。

提前致谢

I have two hands on either side of the screen that do not move at all and two thumbs that rotate 360 degrees. I want to limit the thumbs rotation, meaning I only need them to be able to rotate like normal thumbs. I am new to cocos2d so any help will be greatly appreciated. Here is what I have so far

    #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

This lets both of the thumbs move at the same time in upward/downward angles with the anchor point at the bottom of the thumbs(acting like a joint).

I also need the thumbs rotation to reset back to 0 once the touches have ended.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2024-11-10 03:36:17

在您的 ccTouchesMoved:withEvent: 方法中,只需检查新的旋转是否在给定阈值内,然后再将其应用于精灵即可。

最后,在 ccTouchesEnded:withEvent: 中,您可以简单地将旋转值设置回 0:

lthumbRotation = 0;
rthumbRotation = 0;

In your ccTouchesMoved:withEvent: method, simply check to see if the new rotation is within a given threshold before applying it to your sprites.

Finally, in ccTouchesEnded:withEvent:, you can simply set the rotation values back to 0:

lthumbRotation = 0;
rthumbRotation = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文