2009-11-13 27 views
2

我真的被困在这个上面。我的应用程序是横向视图,在一个屏幕上,我希望我的说明图像可以滚动。我已将此图像添加为精灵。首先,我试图从其他网站获得滚动效果,但很快我看到滚动是为完整屏幕而不是精灵图像完成的。然后,我通过拖动精灵只在y轴上(上和下)来实现滚动效果。不幸的是,我在某处弄乱了某些东西,因为只有一部分精灵(屏幕上只显示高度为320像素)被拖动,剩下的精灵没有显示出来。该代码是在init层功能如下在COCOS2D中上下拖动一个特别大的雪碧像滚动效果

我有

//Add the instructions image 

instructionsImage = [Sprite spriteWithFile:@"bkg_InstructionScroll.png"]; 
instructionsImage.anchorPoint = CGPointZero; 
[self addChild:instructionsImage z:5]; 
instructionsImage.position = ccp(40,-580); 
oldPoint = CGPointMake(0,0); 
self.isTouchEnabled = YES; 

//The touch functions are as follows 
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [touches anyObject]; 

// Move me! 
if(touch && instructionsImage != nil) { 
    CGPoint location = [touch locationInView: [touch view]]; 
    CGPoint convertedPoint = [[Director sharedDirector] convertCoordinate:location]; 

    CGPoint newPoint = CGPointMake(40, (instructionsImage.anchorPoint.y+ (convertedPoint.y - oldPoint.y))); 
    instructionsImage.position = newPoint; 
    return kEventHandled; 
} 
return kEventIgnored; 
} 

//The other function 
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [touches anyObject]; 

// Move me! 
if(touch && instructionsImage != nil) { 
    CGPoint location = [touch locationInView: [touch view]]; 
    CGPoint convertedPoint = [[Director sharedDirector] convertCoordinate:location]; 
    oldPoint = convertedPoint; 
    return kEventHandled; 
} 

return kEventIgnored; 
} 

回答

0

你的做法是正确的一般。

的代码不正确的格式,你没有明确的症状是问题的到底是什么?

但它看起来好像你在ccTouchesMoved数学是错误的。锚点并不是您在那里关注的,因为这只是图像中出现位置和旋转锚点的比率。像你一样,将它设置为构造函数中的任何有意义的东西,但之后不需要引用它。

尝试只是增加你的运动精灵:

移动deltaY = convertedPoint.y - oldPoint.y;

现在你知道手指上下移动了多少个像素。

重置您的oldPoint数据在下一次:

oldPoint.y = convertedPoint.y;

立即应用此三角洲你的精灵:

instrucitonsImage.position = CCP(instructionsImage.position.y,instructionsImage.position.y +增量);

应该这样做。

+0

“ccp(instructionsImage.position.y,...)”应该是“ccp(instructionsImage.position.x,...)”,但是,这种方法是有效的。 – Evan 2010-05-13 22:37:23