Search code examples
objective-cxcodeif-statementcocos2d-iphonetouchesmoved

lots of if statements, need help making some sort of loop


I need a quicker way to write this.

if(sprite1.position.x==33 && sprite1.position.y==33){
// some code goes here to add sprite1 to an array object at index 0
}
if(sprite2.position.x==33 && sprite2.position.y==33){
// some code goes here to add sprite2 to an array object at index 0
}
if(sprite3.position.x==33 && sprite3.position.y==33){
// some code goes here to add sprite3 to an array object a index 0
}
if(sprite4.position.x==33 && sprite4.position.y==33){
// some code goes here to add sprite4 to an array object at index 0
} ....e.t.c

if(sprite1.position.x==33 && sprite1.position.y==97){
// some code goes here to add sprite1 to an array object at index 1
}
if(sprite2.position.x==33 && sprite2.position.y==97){
// some code goes here to add sprite2 to an array object at index 1
}

I have 4 arrays that hold 4 sprites each.

I have 16 points and 16 sprites. Each sprite has been give a random point so I have to check every sprite if it is equal to the point and each sprite has only one point. It is then added to an array. I call the object from the array at object at index 0 as i know for sure that the object at index 0 is a sprite that is equal to point1 which is(33,33). The sprite will be called in cctouchmoved, so i can move the sprite at that point but the array is so i can move a column of sprites as i know that they are all in the right positions. Right now my problem is typing all of that is very long do i have to make a loop or something.


Solution

  • You could perhaps store pointers to your 16 sprites in an array, and also have an array of points that you need to check them against. Then have a nested for loop doing the checking. Which sprite hit which point will then be indicated by the current counter of those loops

    In this example I have not used a point type (eg CGPoint), but a separate array of x points and y points. So any one point you are interested in should be stored in the same element of the two arrays. Eg if you 7th point is (55, 97), then myXPoints[7] = 55; and myYPoints[7] = 97;

    The example loops through your array of 16 sprite pointers. Within each loop, it will loop through the paired array of points. When you have a hit, the loop counters indicate where the hit occurred.

    Disclaimer: This only shows a possible technique, and will probably need adapting to accomodate your types and data structures etc.

        #define NUMSPRITES 16
        #define NUMPOINTS 16
    
        // These arrays are to contain the data that will be compared
    
        int myXPoints [NUMPOINTS];
        int myYPoints [NUMPOINTS];
        Sprite *mySpritePointers [NUMSPRITES];
    
        // Populate the sprite array with pointers to my sprites
        mySpritePointers [0] = &mySpriteArray[0].Sprite1;
        mySpritePointers [1] = &mySpriteArray[0].Sprite2;
        ... etc ...
        mySpritePointers [15] = &mySpriteArray[3].Sprite4;
    
        // Populate the points array
        myXPoints [0] = 33;
        ... etc ...
        myXPoints [15] = 97;
    
        myYPoints [0] = 33;
        ... etc ...
        myYPoints [15] = 97;
    
        // Now check each sprite
        for (int nSprite = 0;  nSprite < NUMSPRITES;  nSprite++)
        {
            // And check each point
            for (int nPoint = 0;  nPoint < NUMPOINTS;  nPoint++)
            {
                // check this sprite against the x and the y points for this point
                if(mySpritePointers [nSprite]->position.x == myXPoints [nPoint] && mySpritePointers [nSprite]->position.y == myYPoints [nPoint])
                {
                    // some code goes here to add mySpritePointers [nSprite] to an array object at index nPoint
                } 
            }
        }
    

    Good luck!