Cocos2d iphone 在Cocos2d中用手指旋转精灵

Cocos2d iphone 在Cocos2d中用手指旋转精灵,cocos2d-iphone,rotation,sprite,Cocos2d Iphone,Rotation,Sprite,我需要有人帮我用手指算出精灵的旋转。精灵旋转得很好,但在我手指的第一次触摸中,它不知怎么地自行旋转了几度 此外,仅当手指围绕精灵中心旋转时,旋转才起作用 我试图模拟自行车车轮,有一个齿轮精灵和踏板精灵作为齿轮精灵的孩子。我想让自行车轮子在我踩下踏板并旋转时旋转。我还没走那么远。现在,我正试图找出如何摆脱齿轮的初始换档 这是完整的代码 #import "BicycleScene.h" @implementation BicycleScene +(id) scene { // 'scen

我需要有人帮我用手指算出精灵的旋转。精灵旋转得很好,但在我手指的第一次触摸中,它不知怎么地自行旋转了几度

此外,仅当手指围绕精灵中心旋转时,旋转才起作用

我试图模拟自行车车轮,有一个齿轮精灵和踏板精灵作为齿轮精灵的孩子。我想让自行车轮子在我踩下踏板并旋转时旋转。我还没走那么远。现在,我正试图找出如何摆脱齿轮的初始换档

这是完整的代码

#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,供任何想在此处查看的人使用:

cctouchsbegind
中,使用与
cctouchsmoved
中相同的代码来确定角度,然后使用
CCRotateTo
移动到该角度。当
CCRotateTo
处于活动状态时,您需要对用户移动手指进行一些处理,可能需要停止当前操作并启动另一个操作以移动到新角度

@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
{

}

编辑:很抱歉,我无法使其保持代码模式。但这就是解决方案,应该很容易在代码中实现。干杯

如果使用上一个接触点而不是bicycleSprite.position,会发生什么情况?同样的行为?什么都没有?当我使用PreviousTouchPoint而不是bicycleSprite.position时,档位也会跳来跳去,而且当它缓慢旋转时,档位会闪烁很多。我刚刚测试了上面的代码,它实现了我期望的功能。问题是它在触摸屏上没有移动到正确的角度?或者它在初始运动时跳跃而不是逐渐移动到某个角度?@lins314159-是的,这正是问题所在。在初始移动时,它会跳跃,而不是逐渐移动到某个角度。若要将文本格式化为代码,请选择所需文本,然后单击带大括号的按钮。