在box2d iPhone中拖动指针并跳跃身体

发布于 2024-10-31 15:57:12 字数 238 浏览 2 评论 0原文

你好,我正在尝试创建一个像桶球这样的游戏,

我在 iPhone 中使用 box2d 作为物理引擎和 UIView, 我创建世界并向其中添加对象。 把每件事都安排好。

但我不知道当用户释放上浆时如何绘制上浆和弹跳。 (见图)

如果有人有解决办法,请解决。

谢谢。! 这是屏幕截图

Hi i am 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-11-07 15:57:12

我通过以下代码解决了这个问题。声明可变数组。

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];

}

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];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文