2012-07-04 54 views
0

我有一艘太空船。我希望飛船移動到玩家觸摸的區域。到目前爲止,我有這樣的代碼:Cocos2D觸摸移動到目標控件?

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    // initialize touch 
    UITouch *touch = [touches anyObject]; 

    // point out location of touch 
    CGPoint location = [touch locationInView:[touch view]]; 

    // move to location 
    [player runAction:[CCMoveTo actionWithDuration:0.5 position:location]]; 

    // log action 
    CCLOG(@"player moved"); 

} 

但是,這有一個怪癖:當我在某些地方觸摸快車將移動緩慢的,而不是向上下來,和其他怪異的動作。有什麼方法可以糾正這些代碼嗎?

回答

2

行後:

CGPoint location = [touch locationInView:[touch view]]; 

加入這一行:

location = [[CCDirector sharedDirector] convertToGL: location]; 

然後嘗試。 :)

+0

它工作得很好! – beakr