簡體   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