使用 cocos2d for iPhone 触摸旋转精灵
我对编程非常陌生,对于我的无知可能给某人带来的任何不便,我提前表示歉意。也预先感谢您。我有一个代表左轮手枪上的子弹皮套的精灵,我希望实现的效果是触摸影响皮套的旋转。我已经弄清楚了所有的三角函数,并且几乎达到了预期的结果。在 ccTouchMoved 的第一个实例上,子弹皮套会跳跃以使其旋转与触摸一致,几乎就像皮套上有一个点与触摸同步一样。之后它就会像你期望的那样旋转。如何让它相对于我上次触摸时停止的位置旋转?我见过其他人也有类似的问题。我也看到了该问题的答案。我只是无知地得到这个答案所说的内容,因为它看起来像是为比我更有经验的程序员准备的。
这是我自己的场景,包含我所有的逻辑
#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);
}
...
@end
这是我第一次尝试游戏。我开始制作一些界面的原型,但已经遇到了障碍!再次预先感谢您!
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);
}
...
@end
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否明白你的意思,但这里有一个建议,
这个问题可能是因为你分配了你的 sprite.rotation = someValue;这将强制为你的精灵分配一个特定的角度,这将导致“跳跃”
,使用这个,sprite.rotation--;或 sprite.rotation++;在你的触摸中慢慢地移动它。在这种情况下,您可能必须更改计算角度旋转的方式。
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.