How to move an object in certain places in cocos2d

how to move an object in certain places.

for examples. am has one small bar (width = 50, height = 10). I have to move this as a plunger manually. I want to move only in x coordinates (limits x = 20 (start point) to x = 50 (end point)), does not move along y coordinates. but its moving from 50 to 10 after there was 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 ); } } 
+6
objective-c iphone cocos2d-iphone sprite
source share
2 answers

I hope I understood this problem very well. What I would do in such a scenario is to include the Chipmunk framework in my game, and then make the plunger and gun as objects of physics. Once this is done, speed and direction (ie the angle of the projectile) can be controlled using the "ApplyImpulse" methods in the structure. The speed and angle would be controlled by Physics itself as soon as I provided the initial values ​​....

+2
source share

take a look at cocos2d beginner's guide

http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:lesson_2._your_first_game

here are the basics of working with strokes and animation sprites

+1
source share

All Articles