我需要幫助確定用手指旋轉精靈。精靈旋轉得很好,但是在初始觸摸我的手指時,它本身以某種方式旋轉了幾度。用手指在Cocos2d中旋轉精靈
此外,旋轉僅在手指圍繞精靈中心旋轉時才起作用。
我試圖模擬自行車車輪,並有一個齒輪精靈和一個踏板精靈作爲齒輪精靈的孩子。當我觸碰踏板並旋轉它時,我想讓自行車輪旋轉。我還沒有那麼遠。現在我正試圖弄清楚如何擺脫這種裝備的初始轉變。
下面是完整的代碼
#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 2011-03-15 07:25:25
當我使用previousTouchingPoint而不是bicycleSprite.position時,齒輪也會跳躍很多,並且齒輪緩慢旋轉時也會出現很多閃爍現象。 – tbag 2011-03-15 15:43:53
我剛剛測試了上面的代碼,它完成了我期望的操作。問題是touchBegan沒有移到正確的角度?或者它在初始移動時跳躍而不是逐漸移動到一個角度? – lins314159 2011-03-16 08:40:13