2013-04-25 24 views
0

堆棧溢出的好朋友 我今天在你面前展示了一個(不是很好)的cocos2d問題。如何在CCMoveTo過程中連續檢查瓷磚貼圖中的衝突

如何在動作中檢查衝突而不是檢查目標磁貼? 我正在使用具有可以拼接的圖塊的元層的地圖。下面的代碼在我點擊可碰撞的瓷磚時起作用,但當我點擊它時,不會發生,如果發生這種情況,他會直接走過去。

目前,我檢測碰撞與此代碼:

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
[_player stopAllActions]; 
CGPoint touchLocation = [touch locationInView:touch.view]; 

touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
touchLocation = [self convertToNodeSpace:touchLocation]; 

CGPoint playerPos = _player.position; 
CGPoint diff = ccpSub(touchLocation, playerPos); 

int diffx = diff.x; 
int diffy = diff.y; 
int adiffx = diffx/24; 
int adiffy = diffy/24; //Porque my tile size is 24  

if (abs(diff.x) > abs(diff.y)) { 
    if (diff.x > 0) { 
     playerPos.x = playerPos.x + adiffx * 24; 
    } else { 
     playerPos.x = playerPos.x + adiffx * 24; 
    } 
} else { 
    if (diff.y > 0) { 
     playerPos.y = playerPos.y + adiffy * 24; 
    }else{ 
     playerPos.y = playerPos.y + adiffy * 24; 
    } 
} 
[self setPlayerPosition:playerPos]; 
} 

我試圖

[self schedule:@selector(setPlayerPosition:) interval:0.5]; 

沒有任何的運氣:

-(void)setPlayerPosition:(CGPoint)position { 
CGPoint tileCoord = [self tileCoordForPosition:position]; 
int tileGid = [_meta tileGIDAt:tileCoord]; 
if (tileGid) { 
    NSDictionary *properties = [_tileMap propertiesForGID:tileGid]; 
    if (properties) { 
     NSString *collision = properties[@"Collidable"]; 
     if (collision && [collision isEqualToString:@"True"]) { 
      return; 
     } 
    } 
} 

float speed = 10; 
CGPoint currentPlayerPos = _player.position; 
CGPoint newPlayerPos = position; 
double PlayerPosDifference = ccpDistance(currentPlayerPos, newPlayerPos)/24; 
int timeToMoveToNewPostition = PlayerPosDifference/speed; 
id moveTo = [CCMoveTo actionWithDuration:timeToMoveToNewPostition position:position]; 
[_player runAction:moveTo]; 

//_player.position = position; 
} 

順便說一句,這會從這種方法稱爲,剛剛在這個應用程序即刻崩潰

NSAssert(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, @"TMXLayer: invalid position"); 我真的不能責怪它崩潰在那裏。

CCMoveTo正在運行時,如何經常檢查與可碰撞元瓷磚的碰撞?

+0

你只檢查在觸摸的位置的瓷磚。你是否維護所有瓷磚陣列?如果是這樣,那麼你可以安排一個「碰撞檢測器」方法,主動檢查玩家和所有方塊之間的碰撞。 – 2013-04-25 16:05:42

+0

不,我不,因爲我不知道如何:D你能否賜教我,偉大的@SmugbitStudios – 2013-04-25 17:14:50

回答

0

在你的init,安排碰撞檢測方法:

[self schedule:@selector(checkCollisions:)]; 

然後如下定義它:

-(void)checkCollisions:(ccTime)dt 
{ 
    CGPoint currentPlayerPos = _player.position; 
    CGPoint tileCoord = [self tileCoordForPosition:currentPlayerPos]; 
    int tileGid = [_meta tileGIDAt:tileCoord]; 
    if (tileGid) 
    { 
     NSDictionary *properties = [_tileMap propertiesForGID:tileGid]; 
     if (properties) 
     { 
      NSString *collision = properties[@"Collidable"]; 
      if (collision && [collision isEqualToString:@"True"]) 
      { 
       [_player stopAllActions]; 
       return; 
      } 
     } 
    } 
} 
+0

太好了,謝謝!這(有點)的作品!除此之外,當他碰到他們時,他會陷入瓦片裏。任何方法只是讓他無法走路時,有什麼在他的方式? – 2013-04-25 18:32:52

+0

這樣做只會在精靈在牆內時停止。 – dqhendricks 2013-04-25 18:41:23

+0

你應該在移動之前檢查碰撞,如果移動會讓你進入牆內,則應該停止移動。 – dqhendricks 2013-04-25 18:42:02

-1

我使用類似的元層。我個人跳過使用MoveTo,並創建了我自己的更新代碼,既移動精靈,並進行碰撞檢測。

我想知道潛在的新職位是什麼,然後我發現在這個新職位的精靈的所有四個角落裏有什麼位置,然後我循環所有貼圖,是可怕的,我停止了精靈。

我實際上更進一步,檢查是否只移動x,或只有y改變結果,然後移動到該位置,而不是如果它移動。

+0

這似乎很聰明,但我不是真的在實施我自己的CCMoveTo方法的水平..你能提供一個例子/解釋一個有點更深入的如何開始? – 2013-04-25 17:16:06

+0

我保持簡單,但使用一個複雜的數學公式。我不說移動到foo位置,而是說以foo的速度向foo方向移動。然後,我使用數學來確定下一幀的位置將取決於方向和速度。一旦我知道新的職位將是什麼,我可以檢查上面提到的東西。另外,爲了限制這種移動,我說以一定的速度向foo方向移動,持續時間爲baz。然後,我會記錄總時間,並在設定的時間結束後停止移動。 – dqhendricks 2013-04-25 18:45:51

+0

看起來像一個聰明的方式。你有興趣發佈你的代碼嗎?請:D – 2013-04-28 17:52:54

相關問題