这是我的运动方法:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
location = [touch locationInView: [touch view]];
CGPoint location_ = [[CCDirector sharedDirector] convertToGL:location];
NSLog(@"Click Position = (%f,%f)",location_.x,location_.y);
float moveSpeed = 40.0;
float moveDist = sqrt(pow(abs(location_.x - sprite.position.x),2) + pow(abs(location_.y - sprite.position.y),2));
float moveTime = (moveDist / moveSpeed);
[sprite runAction:[CCMoveTo actionWithDuration:moveTime position:location_]];
}
这是我的初始化方法。
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
self.isTouchEnabled = YES;
// create and initialize a Label
[self scheduleUpdate]; // available since v0.99.3
[self schedule: @selector(update:) interval:0.5];
CGSize size = [[CCDirector sharedDirector] winSize];
bg = [CCSprite spriteWithFile:@"testBG.png"];
bg.position = ccp( size.width /2 , size.height/2 );
[self addChild: bg];
sprite = [CCSprite spriteWithFile:@"testSprite.png"];
sprite.position = ccp( size.width /2 , size.height/2 );
[self addChild: sprite];
[self runAction:[CCFollow actionWithTarget:sprite]];
}
return self;
}
我的窗口跟随精灵四处走动,但就像我说的那样,精灵会在第一次触摸后到达与触摸不同的点。谁能告诉我发生了什么事?
编辑:我认为这可能与坐标系本身有关,或者可能与
[touches anyObject];
? 我的搜索结果很少。
编辑2:我发现如果我再次将我的精灵返回到 bg 精灵的中间,它会从那里再次正常运行,直到它离得太远。