首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当精灵到达特定位置时销毁焊接接头

当精灵到达特定位置时销毁焊接接头
EN

Stack Overflow用户
提问于 2012-08-15 00:01:49
回答 1查看 110关注 0票数 0

我在代码中的两个sprite之间创建了一个更新(参见下面的weldJoint方法),我还创建了一个方法,在释放mouseJoint时返回sprite的确切位置。我想将sprite的当前位置与spritePositionRelease值进行比较,如果y值相同而x值不同,则销毁weldJoint。请帮帮忙。

spritePositionRelease:

代码语言:javascript
复制
- (CGPoint)spritePositionRelease    {

for(b2Body *b = mouseJoint->GetBodyB(); b; b=b->GetNext())    {
    if (b->GetUserData() != NULL)
    {
        CCSprite *mySprite = (CCSprite*)b->GetUserData();
        if (mySprite.tag == 1) {
            mySprite.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
            CGPoint spritePosition = mySprite.position;
            CCLOG(@"the sprite position is x:%0.2f , y:%0.2f", spritePosition.x, spritePosition.y);

            return spritePosition;

        }
    }
}
}

ccTouchesEnded:

代码语言:javascript
复制
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

if (mouseJoint)
{
    [self spritePositionRelease];

    world->DestroyJoint(mouseJoint);
    mouseJoint = NULL;
}
}

更新:

代码语言:javascript
复制
-(void) update: (ccTime) dt
{
//It is recommended that a fixed time step is used with Box2D for stability
//of the simulation, however, we are using a variable time step here.
//You need to make an informed choice, the following URL is useful
//http://gafferongames.com/game-physics/fix-your-timestep/

int32 velocityIterations = 8;
int32 positionIterations = 1;

// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world->Step(dt, velocityIterations, positionIterations);

// using the iterator pos over the set
std::set<BodyPair *>::iterator pos;

for(pos = bodiesForJoints.begin(); pos != bodiesForJoints.end(); ++pos)
{
    b2WeldJoint         *weldJoint;
    b2WeldJointDef      weldJointDef;

    BodyPair            *bodyPair = *pos;
    b2Body              *bodyA = bodyPair->bodyA;
    b2Body              *bodyB = bodyPair->bodyB;

    weldJointDef.Initialize(bodyA,
                            bodyB,
                            bodyA->GetWorldCenter());

    weldJointDef.collideConnected = false;
    weldJoint = (b2WeldJoint*) world->CreateJoint(&weldJointDef);

    // Free the structure we allocated earlier.
    free(bodyPair);

    // Remove the entry from the set.
    bodiesForJoints.erase(pos);
}

}  
EN

回答 1

Stack Overflow用户

发布于 2012-08-15 13:40:08

你确定mouseJoint>GetBodyB()总是返回正确的正文吗?也许你应该勾选mouseJoint>GetBodyA()?

不管怎么说,你的支票会很简单:

代码语言:javascript
复制
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

if (mouseJoint)
{
   CGPoint releasePoint = [self spritePositionRelease];
   CGPoint touchPoint = [[touches anyObject] locationInView:[[touches anyObject] view]];
   if((releasePoint.y==touchPoint.y) &&(releasePoint.x!=touchPoint.x))
   {
//Destroy weld joint
   }
    world->DestroyJoint(mouseJoint);
    mouseJoint = NULL;
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11956312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档