objective c - Cocos2d 'snap to grid' logic -


i'm building game in cocos2d , i'm having hard time implementing want. imagine 8 x 10 grid of squares. can touch square , drag square's row / column horizontally / vertically, not both. after release row / column, squares 'snap' place in grid according closest row / col positions.

everything works programmatically, no overlaps or misplaced squares or invalid positions. however, cannot seem 'snap grid' animate way want.

i'm using mvc design pattern separate views game logic. view handed array of 'squarestoupdate', looks @ every square, finds corresponding sprite, , updates position of sprite based on position of square.

the problem arises when trying along lines of this:

-update(): each square in squarestoupdate:   if square not being dragged @ moment:     setup 'ccmoveto' bring sprite in line square   

i can't squares move freely while being dragged, ccmoveto when not being dragged. either create new action every update, or squares freak out.

i don't know if it's logic broken, if ccmoveto not doing want, or if problem harder thought. me out logic?

good old pencil , paper gives me this, i'm not sure it's 100%:

- (void)update:(cctime)dt {     nsmutablearray *todraw = [self.rootview whatamidrawing];     (gameobject *o in todraw) {         ccsprite *sprite = (ccsprite*)[self.batch getchildbytag:o.tag];         if (o.moving == no) {             if (o.snapping == no) {                 o.snapping = yes;                 ccmoveto *move = [ccmoveto actionwithduration:self.rootview.model.activegame.snapspeed position:o.position];                 [sprite runaction:move];             }         } else {             o.snapping = no;             sprite.position = o.position;             sprite.rotation = o.rotation;         }     }     [super update:dt]; } 

i override cocos2d touch methods:

- (bool)cctouchbegan:(uitouch *)touch withevent:(uievent *)event - (void)cctouchmoved:(uitouch *)touch withevent:(uievent *)event - (void)cctouchended:(uitouch *)touch withevent:(uievent *)event 

cctouchbegan compare touch point every game object , set "touched" flag:

cctouchmoved set position of "touched" object new touch position, , set "moving" flag.

cctouchended clear "touched" flags, run ccmoveto new position object "moving" flag, , clear "moving" flags.

the update() method have change position of every sprite position of object.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -