objective c - Sprite goes above/below the limits of grid movement system -
i'm trying implement grid movement in game. grid has 1 column , 3 lines.
[ ] y3 - 60 % of height [ ] y2 - 40% of height [ ] y1 - 20% of height
in iphone 3.5 inch, these height are:
[ ] y3 - 193px [ ] y2 - 128px [ ] y1 - 64px
when user taps screen above npc, npc should move next cell above him (e.g. if npc in y1, should go y2). , same thing should happen in opposite direction. but, npc should never com above 193px or below 64px. not happening. if tap twice, when npc moving, starts moving faster , ignores grind, , go above/below boundaries.
this code. how can improve want?
-(void) touchbegan:(uitouch *)touch withevent:(uievent *)event { cgpoint touchlocation = [touch locationinnode:self]; positiony1 = self.contentsize.height*.2; positiony2 = self.contentsize.height*.40; positiony3 = self.contentsize.height*.60; float playermove; if (touchlocation.y > _player.position.y) { if (_player.position.y >= positiony3) { nslog(@"decisao 1"); playermove = positiony3; } else if (_player.position.y >= positiony2 && _player.position.y <= positiony3) { nslog(@"decisao 2"); playermove = positiony3; } else if (_player.position.y >= positiony1 && _player.position.y <= positiony2) { nslog(@"decisao 3"); playermove = positiony2; } else if (_player.position.y <= positiony1) { nslog(@"decisao 4"); playermove = positiony1; } } else if ((touchlocation.y < _player.position.y)) { if (_player.position.y <= positiony1) { nslog(@"decisao 5"); playermove = positiony1; } else if (_player.position.y >= positiony1 && _player.position.y <= positiony2) { nslog(@"decisao 6"); playermove = positiony1; } else if (_player.position.y >= positiony2 && _player.position.y <= positiony3) { nslog(@"decisao 7"); playermove = positiony2; } else if (_player.position.y >= positiony3) { nslog(@"decisao 8"); playermove = positiony3; } } // move our sprite touch location ccactionmoveto *actionmove = [ccactionmoveto actionwithduration:1.0f position:cgpointmake(_player.position.x, playermove)]; [_player runaction:actionmove];
}
one problem allow multiple move actions stop, every time tap move action runs regardless of whether there's move action running. remove move actions (or of them) before running new one:
[_player stopallactions]; // or: remove tag [_player runaction:actionmove];
next, if none of if/else true value of playermove left undefined:
float playermove;
if happen variable may hold value @ random, possibly moving node far, far away screen. that'll make appear sprite disappear , give hard time figure out what's going on.
it practice, practice indeed, initialize standard value types default value:
float playermove = 0.0;
Comments
Post a Comment