【问题标题】:Camera following the touched body相机跟随被触摸的身体
【发布时间】:2013-06-02 13:45:14
【问题描述】:

我需要让 Cocos2d 相机跟随用户在屏幕上触摸的精灵(附加到 Box2D 主体)。当用户拖动播放器时,我需要它能够去世界的其他地方。这必须通过触摸,而不是自动滚动。

我尝试了几种基于教程的方法,但似乎没有解决这个问题。例如,@Michael Fredrickson 在Move CCCamera with the ccTouchesMoved method? (cocos2d,iphone) 提供的解决方案使整个图层移动,但是当它移动时,屏幕上的精灵/身体具有无与伦比的协调性,当我测试它们是否被触摸时,if(fixture->TestPoint(locationWorld))失败。

我还查看了http://www.learn-cocos2d.com/2012/12/ways-scrolling-cocos2d-explained/ 此处的教程,但这也不是我想要的。

任何帮助将不胜感激。

编辑:

我在下面接受 Liolik 的回答,因为它让我走上了正确的道路。不过,难题的最后一部分是使从 getPoint 方法接收到的值成为实例变量,并从 locationWorld 中推断出它,我正在针对 TestPoint 执行此操作。像这样:

UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];    
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
b2Vec2 diff = b2Vec2(difference.x, difference.y);

for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext()) {
    b2Fixture* f = b->GetFixtureList();
    while(f != NULL) {
        if(f->TestPoint(locationWorld-diff)) {
            b2MouseJointDef def;
            def.bodyA = _groundBody;
            def.bodyB = b;
            def.target = locationWorld-diff;
            def.maxForce = 9999999.0f * b->GetMass(); 
            _mouseJoint = (b2MouseJoint*)_world->CreateJoint(&def);
            b->SetAwake(true);
        }
     f = f->GetNext();
    }
}

【问题讨论】:

    标签: cocos2d-iphone


    【解决方案1】:

    在更新功能中:

    CGPoint direction = [self getPoint:myBody->GetPosition()];
    [self setPosition:direction];
    
    
    - (CGPoint)getPoint:(b2Vec2)vec
    {
        CGSize screen = [[CCDirector sharedDirector] winSize];
    
        float x = vec.x * PTM_RATIO;
        float y = vec.y * PTM_RATIO;
    
        x = MAX(x, screen.width/2);
        y = MAX(y, screen.height/2);
    
        float _x = area.width  -  (screen.width/2);
        float _y = area.height - (screen.height/2);
    
        x = MIN(x, _x);
        y = MIN(y, _y);
    
        CGPoint goodPoint = ccp(x,y);
    
        CGPoint centerOfScreen = ccp(screen.width/2, screen.height/2);
        CGPoint difference = ccpSub(centerOfScreen, goodPoint);
    
        return difference;
    }
    

    【讨论】:

    • Liolik,你的 getPoint 方法中的“面积”是什么?
    • 好的,我明白了,该区域是我的地图的大小(在我的情况下是背景图像。)所以现在我可以移动,同时拖动我的播放器,但过了一会儿触摸失去与玩家身体的接触并且滚动停止。我知道我需要重新计算身体的位置以使用新坐标进行调整——但我不知道该怎么做。
    • 我接受了答案,但请查看我在问题正文中的“编辑”,看看最后一块拼图——我需要什么。
    【解决方案2】:

    所以如果我理解正确的话,当精灵在屏幕中间时,背景是静止的,精灵跟随你的手指,但是当你向边缘滚动时,相机开始平移?

    我在我的游戏 Star Digger 中遇到了大致类似的情况,屏幕中间有一艘船在它自己的层上必须绕世界飞行,当船向主世界层发射子弹时也遇到了同样的问题.

    这就是我所做的:

    float thresholdMinX = winSize*1/3;
    float thresholdMaxX = winSize*2/3;
    
    if(touch.x > thresholdMaxX) //scrolling right
    {
      self.x += touch.x - thresholdMaxX;
    }
    else if(touchX < thresholdMinX)
    {
      self.x += thresholdMinX - touchX;
    }
    else
    {
      sprite.position = touch;
    }
    CGPoint spritePointInWorld = ccp(sprite.x - self.x, sprite.y - self.y);
    

    那么每次计算碰撞时,都需要重新计算精灵在世界中的“实际”位置,即其屏幕位置减去世界偏移量,而不是精灵屏幕位置。

    【讨论】:

    • 谢谢,@redux。所以我猜你对世界上的每个精灵都做同样的计算?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2017-08-13
    相关资源
    最近更新 更多