【问题标题】:I want to move sprite body when touch point detect我想在检测到触摸点时移动精灵体
【发布时间】:2012-07-07 08:04:19
【问题描述】:

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

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

请帮帮我... 谢谢

【问题讨论】:

    标签: iphone ios cocos2d-iphone ccsprite


    【解决方案1】:

    请尝试下面的代码,它会为你工作。

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

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

    【讨论】:

      【解决方案2】:

      在你的初始化方法中添加self.isTouchEnabled = YES;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多