开发者

Drag pointer and jump body in box2d iphone

Hi i am开发者_Go百科 trying to create a game like bucket ball,

i am using box2d as physics engine with UIView in iPhone, i create world and add object into it. set every thing fine.

but i don't know how can i draw starching and bouncing when user release starching. (see image for)

Please if any one has work around.

Thanks.! Here is Screen Short


i solved this by following code. declare mutable array.

NSMutableArray *arrTouches;

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if([[touches allObjects] count] == 1)
    {
        if([arrTouches count]==2)
        {
            UITouch *touch = [[touches allObjects] objectAtIndex:0];
            CGPoint location = [touch locationInView: [touch view]];
            location = [[CCDirector sharedDirector] convertToGL: location];
            [arrTouches replaceObjectAtIndex:1 withObject:NSStringFromCGPoint(location)];
            [self jumpBall];
        }
    }
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if([[touches allObjects] count] == 1)
    {
        if([arrTouches count] == 1)
        {
            UITouch *touch = [[touches allObjects] objectAtIndex:0];
            CGPoint location = [touch locationInView: [touch view]];
            location = [[CCDirector sharedDirector] convertToGL: location];
            [arrTouches addObject:NSStringFromCGPoint(location)];
        }
        else if([arrTouches count] == 2)
        {
            UITouch *touch = [[touches allObjects] objectAtIndex:0];
            CGPoint location = [touch locationInView: [touch view]];
            location = [[CCDirector sharedDirector] convertToGL: location];
            [arrTouches replaceObjectAtIndex:1 withObject:NSStringFromCGPoint(location)];
        }
    }
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];

    if([[touches allObjects] count] == 1 && CGRectContainsPoint(_ball.boundingBox, location))
    {
        NSLog(@"Touch Begin : X: %f Y: %f",location.x,location.y);
        [arrTouches addObject:NSStringFromCGPoint(location)];
    }
}

-(void)jumpBall
{
    CGPoint diff = ccpSub(CGPointFromString([arrTouches objectAtIndex:0]), CGPointFromString([arrTouches objectAtIndex:1]));

    CGPoint oldP = CGPointFromString([arrTouches objectAtIndex:0]);
    CGPoint newP = CGPointFromString([arrTouches objectAtIndex:1]);

    CGPoint p = CGPointFromString([arrTouches objectAtIndex:1]);

    float floatX = p.x/PTM_RATIO;
    float floatY = p.y/PTM_RATIO;

    _body->ApplyForce(b2Vec2(-5.0f*diff.x,5.0f*newP.y), _body->GetPosition());

    [arrTouches removeAllObjects];

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜