ios - How to continuously check collisions in Tiled tilemaps during a CCMoveTo -


hello people of stack overflow stand before today (not great) cocos2d issue.

how can check collisions while in action, instead of checking target tile? working tilemap has meta layer there collidable tiles. following code works when click collidable tile, not when click beyond it, if happens walk straight through.

currently, detect collisions code:

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

by way, gets called method:

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

i tried

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

without luck, instantly crashed app @

nsassert( pos.x < _layersize.width && pos.y < _layersize.height && pos.x >=0 && pos.y >=0, @"tmxlayer: invalid position"); , can't blame crashing there.

how check collisions collidable meta tiles while ccmoveto running?

in init, schedule collision detecting method:

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

then define follows:

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

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -