【发布时间】:2011-03-29 06:16:13
【问题描述】:
我需要帮助弄清楚我的手指如何旋转精灵。精灵旋转得很好,但在我的手指最初触摸时,它会以某种方式自行旋转几度。
只有当手指围绕精灵的中心旋转时,旋转才有效。
我正在尝试模拟自行车车轮,并有一个齿轮精灵和一个踏板精灵作为齿轮精灵的孩子。我希望自行车车轮在我触摸踏板并旋转时旋转。我还没有到那一步。现在我正试图弄清楚如何摆脱齿轮的初始换档。
这是完整的代码
#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 而不是 bikeSprite.position 会发生什么?同样的行为?什么都没有?
-
当我使用previousTouchingPoint而不是bikeSprite.position时,齿轮也会跳动很多,并且当齿轮缓慢旋转时也会有很多闪烁。
-
我刚刚测试了您上面的代码,它完成了我期望它做的事情。是不是在 touchBegan 上没有移动到正确的角度?或者它是跳跃而不是在初始移动时逐渐移动到某个角度?
-
@ lins314159 - 是的,这正是问题所在。在初始移动时,它会跳跃而不是逐渐移动到某个角度。
标签: cocos2d-iphone rotation sprite