Rotating a sprite with touch with cocos2d for iphone
I am extremely new to programing and I apologize in advance for any inconvenience my ignorance may cause someone. Also thank you in advance. I have a sprite that represents the bullet holster on a revolver, The effect I am looking to accomplish is to have touch effect the rotation of the holster. I have all of the trig figured out and am almost accomplishing the desired result. Upon the very first instance of ccTouchMoved the bullet holster jumps to conform its rotation to the touch, almost like there is a point on the holster that is synched with the touch. After that it spins like you would expect. How do I get it to rotate relative to where I left off on my last touch? I have seen other people who have a similar problem. I have also seen answer replies to the problem. I am just to ignorant to get what that answer was saying because it looked like it was intended for a more seasoned programmer than myself.
Here is my own scene that contains all my logic
#import "MainScene.h"
@implementation MainScene
+(id) scene
{
CCScene *scene = [CCScene node];
MainScene *layer = [MainScene node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if ((self=[super 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) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Touch begun");
}
-(void)update:(ccTime) dt{
dial.rotation = cocosAngle;
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Touch moved");
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:touchLocation];
CGPoint vector = ccpSub(touchingPoint, dial.position);
CGFloat rotateAngle = -ccpToAngle(vector);
deltaAngle = CC_RADIANS_TO_DEGREES( rotateAngle);
}
...
@开发者_如何学Pythonend
This is my first attempt at a game. I began prototyping some of the interface and already hit a speed bump! Thank you again in advance!
i'm not sure if i'm getting what you mean but here's a suggestion
this problem might be because you assigned your sprite.rotation = someValue; this will forcefully assign a specific angle to your sprite which will cause the "jump"
instead, use this, sprite.rotation--; or sprite.rotation++; in your touchMoved to gradually move it. you might have to change the way you calculate the angle rotation in this case.
精彩评论