如何将对象移动到特定位置。
例如,我有一个小条形物体(宽度=50,高度=10)。我想手动将其移动,就像活塞一样。我只想在x坐标上移动(限制是x = 20(起始点)到x = 50(终点)),不要在y坐标上移动。但是它移动到50后停止移动。
编码:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (isPlaying) {
UITouch *touch = [[event allTouches] anyObject];
touchPosition = [touch locationInView:touch.view];
if ( CGRectContainsPoint(para3.boundingBox,touchPoint)
isDragging = YES;
touchOffset = para3.position.y - touchPosition.y;
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (isPlaying) {
UITouch *touch3 = [[event allTouches] anyObject];
float distanceMoved =
([touch3 locationInView:touch3.view].y + touchOffset) -
para3.position.y;
float newY = para3.position.y + distanceMoved;
if (newY > 67 && newY < 99)
para3.position = CGPointMake(newY , para3.position.y );
//para3.contentSize/2
if (newY >67 )
para3.position = CGPointMake( 67, para3.position.y );
if (newY < 99)
para3.position = CGPointMake( 99, para3.position.y );
}
}