如何在特定位置移動物體。例如如何在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