我遇到了一些cocos2d代码的问题, 它在模拟器上运行正常(在这种情况下意味着当我触摸和滚动时精灵正在移动)但是它在我的ipod上不起作用 ccTouchesMoved在两者上都被调用,但精灵仅在模拟器中移动
CCSprite *gradA = [CCSprite spriteWithFile:@"grad.png"];
gradA.anchorPoint = ccp(0, 0);
gradA.position = ccp(0, 0);
[...]
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(@"ccTouchesMoved called");
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
int d = (_prevTouch.y - location.y);
// This code should make the sprite moving
// And it does on the simulator but not on my ipod
gradA.position = ccp(PARALAX_X, gradA.position.y - d);
_prevTouch = location;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
_firstTouch = location;
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
_lastTouch = location;
}
BTW如果我做“gradA.position = ccp(0,gradA.position.y - d);” 在除了ccTouchesMoved以外的任何其他方法中,它可以在设备和模拟器上运行。
这可能是一个愚蠢的错误(我希望是这样),因为这是我的第一个项目。
答案 0 :(得分:1)
这是我现在如何移动我的精灵(在ccTouchesMoved中):
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
CGPoint newPos = ccpAdd(currentFragment.position, translation);
currentFragment.position = newPos;
答案 1 :(得分:0)
答案很简单,如果设备和模拟器之间有问题,你应该检查一下(以及文件名大小写) NSLog在设备上非常慢,因此它会阻止设备渲染精灵移动。
我在ccTouchesMoved方法的代码中有两个NSLog, 当我删除它时,它就像一个魅力。
避免在设备上使用NSLog。