ios - How to Move the sprite with conditions and constrains -



ios - How to Move the sprite with conditions and constrains -

i having 32 sprite in scene had arranged sprite this

o o o o o o o o o o o o o o o o . o o o o o o o o o o o o o o o o

o ---> movingball . ---> empty hole

when move move 1 sprite empty hole, middle sprite removed , sprite , hole should interchanged, this.

o o . -----> . . o

possible ways:

| | | | | | | o | | . | | o | . | . | . o | o o . | o | . o o | o | o | o | o . | | o | | . | o | o | o | | | | | | |

totally 8 possibility move sprite.

can help me how , should want alter in code?

my coding here

- (void)cctouchesbegan:(nsset *)touches withevent:(uievent *)event { uitouch *mytouch = [touches anyobject]; cgpoint location = [mytouch locationinview:[mytouch view]]; location = [[ccdirector shareddirector] converttogl:location]; if(movingball) { for(int = 0; i<32; i++) { ccsprite *currentsprite = (ccsprite *)[self getchildbytag:i]; if(cgrectcontainspoint([currentsprite boundingbox],location)) { // moving sprite touched if(movingball.position.x == hole.position.x+(2*75) || movingball.position.x == hole.position.x-(2*75) || movingball.position.y == hole.position.y+(2*75) || movingball.position.y == hole.position.y -(2*75)) { movingball = (ccsprite *)currentsprite; break; } } } } } -(void)cctouchesmoved:(nsset *)touches withevent:(uievent *)event { uitouch *touch=[touches anyobject]; cgpoint location=[touch locationinview:[touch view]]; location=[[ccdirector shareddirector]converttogl:location]; location=[self converttonodespace:location]; if(!movingball) { return; } movingball.position = location; for(int = 0; i<32; i++) { ccsprite *currentsprite = (ccsprite *)[self getchildbytag:i]; if(cgrectintersectsrect([movingball boundingbox],[currentsprite boundingbox])) { // current sprite touched if(currentsprite.tag == hole.tag) { movingball.position = hole.position; [self removechild:currentsprite]; break; } } }} -(void)cctouchesended:(nsset *)touches withevent:(uievent *)event { if(!movingball) { return; } movingball = nil; }

(i don't know programming language purely algorithmic perspective)

first, compute starting , ending locations user attempting move ball , to. if move not filled space, reject. if move not empty space, reject.

second, calculate motion of ball - xdelta = xend - xstart, ydelta = yend - ystart. motion valid, 1 of 2 must true:

either abs(xdelta) or abs(ydelta) == 2, , other == 0. abs(xdelta) == 2 , abs(ydelta) == 2.

if neither true, reject.

finally, calculate mid point cell. xmid = xstart + xdelta/2, ymid = ystart + ydelta/2. if filled ball, perform move emptying xstart,ystart, emptying xmid,ymid , filling xend,yend. if not filled ball, reject.

ios objective-c cocos2d-iphone

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -