如何在特定位置移动物体。例如如何在cocos2d的特定位置移动物体
。我有一个小酒吧(宽= 50,高= 10)。我必须手动移动柱塞。我只想移动x坐标(限制是x = 20(起点)到x = 50(终点))在y坐标上不移动。但是它在没有任何动作后将其移动50到10。 编码: -
- (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);
}
}
你必须寻找不同的坐标系的科科斯和UIView。所以你需要从locationInView转换点。我不记得确切。但CCDirector是实现这种方法的类。 ;-)我希望这有助于。 – 2011-01-13 20:36:11