使用手指在Cocos2d中旋转精灵

3

我需要帮助解决用手指旋转精灵的问题。精灵可以旋转,但是在我的手指刚接触到精灵时,它会自己旋转几度。

此外,只有当手指围绕精灵中心旋转时,旋转才有效。

我正在尝试模拟自行车轮,并将齿轮精灵和踏板精灵作为齿轮精灵的子节点。我希望在摇动踏板并旋转时,自行车轮能够旋转。但我还没有做到这一步,现在我正试图解决如何消除齿轮在初始时的旋转。

以下是完整代码:

#import "BicycleScene.h"

@implementation BicycleScene

+(id) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    BicycleScene *layer = [BicycleScene node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id) init
{
    if ((self=[super init])) {

        CGSize winSize = [[CCDirector sharedDirector] winSize];

        //place the bicycle sprite
        bicycleSprite = [CCSprite spriteWithFile:@"bike_gear.png"];
        bicycleSprite.position = ccp(bicycleSprite.contentSize.width/2 + 100, winSize.height/2);
        [self addChild:bicycleSprite z:1];

        //place the pedal sprite
        pedalSprite = [CCSprite spriteWithFile:@"bike_pedal.png"];
        [bicycleSprite addChild:pedalSprite z:1];

        pedalSprite.position = ccp(150, 15);

        //enable touch
        self.isTouchEnabled = YES;

        [self schedule:@selector(gameLoop:) interval:.1/100];

    }

    return self;
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    NSLog(@"Touch begun");

}

-(void)gameLoop:(ccTime) dt{

    bicycleSprite.rotation = cocosAngle;
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    NSLog(@"Touch moved");

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    CGPoint previousLocation = [touch previousLocationInView:[touch view]];

    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
    CGPoint previousTouchingPoint = [[CCDirector sharedDirector] convertToGL:previousLocation];

    CGPoint vector = ccpSub(touchingPoint, bicycleSprite.position);
    CGFloat rotateAngle = -ccpToAngle(vector);

    previousAngle = cocosAngle;
    cocosAngle = CC_RADIANS_TO_DEGREES( rotateAngle);

    //bicycleSprite.rotation = cocosAngle;
}

@end

我有些困惑这句话的意思:

CGPoint vector = ccpSub(touchingPoint, bicycleSprite.position);

应该实际如下:

CGPoint vector = ccpSub(touchingPoint, previousTouchingPoint );

我也尝试了那个方法,但它没有起作用。

我还将我的完整的xcodeproj上传到4shared供任何想查看的人使用,链接在这里:http://www.4shared.com/file/5BaeW4oe/Healthy.html


当您使用previousTouchingPoint而不是bicycleSprite.position时会发生什么?是否有相同的行为?还是什么都没有? - lins314159
当我使用previousTouchingPoint而不是bicycleSprite.position时,齿轮也会跳动,并且当它缓慢旋转时,齿轮也会出现很多闪烁。 - tbag
我刚刚测试了你上面的代码,它做到了我的预期。问题是它在touchBegan时没有移动到正确的角度吗?还是在初始移动时跳跃而不是逐渐移动到一个角度? - lins314159
@ lins314159 - 是的,这正是问题所在。在初始移动时,它会跳跃而不是逐渐移动到一个角度。 - tbag
2个回答

9
@interface MainScene : CCLayer {
    CCSprite *dial;

    CGFloat dialRotation;

}

+ (id)scene;

@end
---------------------------------
@implementation MainScene

+ (id)scene
{
    CCScene *scene = [CCScene node];
    CCLayer *layer = [MainScene node];
    [scene addChild:layer];

    return scene;
}

- (id)init
{
    if((self = [super init])) {
        CCLOG(@"MainScene init");

        CGSize size = [[CCDirector sharedDirector]winSize];

        dial = [CCSprite spriteWithFile:@"dial.png"];
        dial.position = ccp(size.width/2,dial.contentSize.height/2);
             [self addChild:dial];

        self.isTouchEnabled = YES;
        [self scheduleUpdate];

    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)update:(ccTime)delta
{
    dial.rotation = dialRotation;
}

- (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, dial.position);
    CGFloat firstRotateAngle = -ccpToAngle(firstVector);
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

    CGPoint vector = ccpSub(touchingPoint, dial.position);
        CGFloat rotateAngle = -ccpToAngle(vector);
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

    //keep adding the difference of the two angles to the dial rotation
    dialRotation += currentTouch - previousTouch;
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}

编辑:很抱歉,我无法将此保持在代码模式下。但这是解决方案,应该很容易在您的代码中实现。干杯!


要将文本格式化为代码,请选择您想要的文本,然后单击带有花括号按钮。 - lins314159

0
ccTouchesBegan 中,使用与 ccTouchesMoved 相同的代码来确定角度,然后使用 CCRotateTo 移动到该角度。您需要处理用户在 CCRotateTo 活动时移动手指的情况,可能需要停止当前操作并启动另一个操作以移动到新角度。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接