Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to move the object in specific locations in cocos2d

how to move the object in specific locations.

for examples. am having one small bar(width=50, height=10). i have to move this like plunger manually. i want to move only in x cordinates ( limits is x=20(start point) to x=50(end point)) no moves on y coordinates. but its moving 50 to 10 after wards no movement.alt text coding:-

- (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 );
    }
}
like image 405
Srinivas Avatar asked Jan 13 '11 20:01

Srinivas


Video Answer


1 Answers

I hope I have understood the issue perfectly. What I would have done in such a scenario is to include Chipmunk framework in my Game and then make my Plunger and cannon as the Physics objects. Once that is done, the speed and direction (i.e. angle of projectile) can be controlled through the "ApplyImpulse" methods of the framework. The speed and angle would have been controlled by Physics itself once I would provide the initial values....

like image 169
TabishFarooqui Avatar answered Sep 19 '22 13:09

TabishFarooqui