微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在box2d iphone中拖动指针并跳转主体

如何解决在box2d iphone中拖动指针并跳转主体

| 嗨,我正在尝试创建类似斗球的游戏, 我正在使用Box2d作为iPhone中的UIView的物理引擎, 我创造了世界,并向其中添加了对象。 设置好一切。 但是当用户释放淀粉时,我不知道如何绘制淀粉并弹起。 (参见图片) 如果有人可以解决。 谢谢。! 这是屏幕短片     

解决方法

我通过以下代码解决了这个问题。声明可变数组。
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];

}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。