开发者

how to move the object in specific locations in cocos2d

how to move the object in speci开发者_如何学编程fic 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.

how to move the object in specific locations in cocos2d

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 );
    }
}


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....


take a look at the cocos2d beginner's guide

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

here are described the basics of working with touches and sprite animation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜