开发者

How to move an Object using Slingshot in different Angles?

I'm a beginner in iPhone Application Development and Xcode and I'm developing a small game using Box2D which has a slingshot effect. I need help with moving a body in its respective angles when the sling shot is used. The slingshot is drawn using ccDrawLine and a body is placed on it.

In my project the object moves haphazardly in different directions when the sling shot is used. Does anyone know how to fix this?

My code:

Draw the slingshot:

-(void)draw
{
    //NSLog(@"in dra");
    //glDisable(GL_TEXTURE_2D);
    //glDisableClientState(GL_COLOR_ARRAY);
    //glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    _world->DrawDebugData();

    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_COLOR_ARRAY);
    //glEnableClientState(GL_TEXTURE_COORD_AR开发者_运维百科RAY);

    glColor4f(0.6, 0.4, 0.2, 1.0);
    glLineWidth(4.0f);

    //glEnable(GL_LINE_SMOOTH);
    ccDrawLine( ccp(80, 75),ccp(pt1,pt2));
    ccDrawLine(ccp(pt1,pt2), ccp(240,75));
    ccDrawLine(ccp(80,75),ccp(80,0));
    ccDrawLine(ccp(240,75),ccp(240,0));    
}

Place the object on the sling:

-(void)addsprite2
{
    stone=[CCSprite spriteWithFile:@"rock.png"];
    stone.position=ccp(160,80);
    stone.tag=1;
    [self addChild:stone];
}

I'm trying to create and add the angles manually.

Angle creation:

    if (stone.position.y < 80 && stone.position.y >= 70)
    {
        ft = abs(tp2)/PTM_RATIO;
    }
     else if(stone.position.y < 70 && stone.position.y >=60)
    {
        ft = (abs(tp2)+90)/PTM_RATIO;
    }

    else if(stone.position.y < 60 && stone.position.y >= 50)
    {
        ft = (abs(tp2)+110)/PTM_RATIO;
    }

    else if(stone.position.y < 50 && stone.position.y >= 40)
    {
        ft = (abs(tp2)+140)


You can use a bezier curve to implement the projectile curve effect in a 2D space. Then set the the first and second control points and the end point depending on the pull distance.

This is the simplest way to implement the projectile curve.

Also to move the object you may use touchesMoved function and set the position of the object as the same as that of the touchesMoved point.

object.position = pulling.position;

This will allow you to move the object as you move your finger.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜