繁体   English   中英

我想在触摸点检测时移动精灵体

[英]I want to move sprite body when touch point detect

我想在触摸屏时移动精灵体,但它不能发生......

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    for(b2Body *b=world->GetBodyList();b;b=b->GetNext())
    {
    CGPoint location=[touch locationInView:[touch view]];
        location=[[CCDirector sharedDirector]convertToGL:location];
         b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);


        if(b->GetUserData()!=NULL)
        {
        CCSprite *sprite =(CCSprite *)b->GetUserData();

            b->SetTransform(b2Vec2(location.x, location.y), 0);
            id action = [CCMoveTo actionWithDuration:0.4 position:CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO)];
             [sprite runAction:action];
        }
     }
}

请帮帮我...谢谢

请尝试以下适用于您的代码。

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{       
    for( UITouch *touch in touches ) {
        CGPoint location = [touch locationInView: [touch view]];

        location = [[CCDirector sharedDirector] convertToGL: location];
        for(b2Body *b=world->GetBodyList();b;b=b->GetNext())
        {
            if(b->GetUserData()!=NULL)
            {
                CCSprite *sprite =(CCSprite *)b->GetUserData();
                b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
                b->SetTransform(b2Vec2(locationWorld.x, locationWorld.y), 0);
                id action = [CCMoveTo actionWithDuration:0.4 position:CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO)];
                [sprite runAction:action];

            }
        }

    }
}

身体的精灵移动到触摸结束的位置。

add self.isTouchEnabled = YES; 在你的init方法中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM