mardi 3 février 2015

Trouble comparing properties of sprite

I’m trying to build a sample app to gain some experience and I’ve designed the layout using SpriteBuilder, a 4 by 4 grid is created (similar to 2048 or Threes!) with 2 blocks generated at random positions. You swipe to combine them and a new block is generated in an empty position with every swipe. These blocks have numbers on them from 1-3 initially and when they are combined the combinations range from 4-6.


So, if 1 and 2 are combined you get 6. Combining 2 and 3 gives 4. Combining 3 and 1 gives 5. Imagine these blocks (1-3) as the primary set of blocks.


The secondary set of blocks (4-6) can combine to return back to the primary. So, 4 and 5 gives 3. Combing 5 and 6 gives 1. Combining 6 and 4 gives 2.


There isn’t really any logic in it at the moment but I plan to make these numbers actually do something later on such as changing the colour of the block or something using a switch statement.


Essentially my goal at the moment is to try and combine three blocks at once when they are in the same row, in any order and are from the same set, so 1, 2 and 3 will give 8 while 4, 5 and 6 will give 7. The problem is, I’m not getting either one of these results when I combine any set at once. It only gives the default result which is combining two of the blocks together and giving their respective values. I can’t combine three at once.



if ([self indexValid:newX+direction.x y:newY+direction.y]) {
NSInteger otherBlockX = newX + direction.x;
NSInteger otherBlockY = newY + direction.y;
Block *otherBlock = _gridArray[otherBlockX][otherBlockY];

if ([self indexValid:otherBlockX+direction.x y:otherBlockY+direction.y]) {

NSInteger otherBlock2X = otherBlockX + direction.x;
NSInteger otherBlock2Y = otherBlockY + direction.y;
Block *otherBlock2 = _gridArray[otherBlock2X][otherBlock2Y];

if (((block.value == 1) && (otherBlock.value == 2) && (otherBlock2.value == 3)) || ((block.value == 2) && (otherBlock.value == 1) && (otherBlock2.value == 3)) || ((block.value == 2) && (otherBlock.value == 3) && (otherBlock2.value == 1)) || ((block.value == 3) && (otherBlock.value == 2) && (otherBlock2.value == 1)) || ((block.value == 3) && (otherBlock.value == 1) && (otherBlock2.value == 2)) || ((block.value == 1) && (otherBlock.value == 3) && (otherBlock2.value == 2))) {
otherBlock2.value = 8;
[self mergeBlocksAtIndex:currentX y:currentY withBlockAtIndex:otherBlock2X y:otherBlock2Y];
[self mergeBlocksAtIndex2:otherBlockX y:otherBlockY withBlockAtIndex:otherBlock2X y:otherBlock2Y];
movedBlocksThisRound = TRUE;
}


The first if statement is responsible for declaring the position of the other block when swiped in a direction, the second is for declaring the position of the further block.The third is the if statement that checks the values of all three and calls the method to merge them. Another if statement that I did not paste is below this and it's similar in that it checks for values 4-6. If neither one of these work, it declares the boolean 'numberCombo = TRUE'. This if statement holds six if statements that are responsible for combining two blocks at once, one of which look like:



if (((block.value == 1) && (otherBlock.value == 2) && (!otherBlock.mergedThisRound)) || ((block.value == 2) && (otherBlock.value == 1) && (!otherBlock.mergedThisRound))) {
otherBlock.value = 6;
[self mergeBlockAtIndex:currentX y:currentY withBlockAtIndex:otherBlockX y:otherBlockY];
movedBlocksThisRound = TRUE;
}


This is followed by five more similar statements that check values 2,3.. 3,1.. 4,5 etc. If I remove the first three if statements in numberCombo (1,2.. 2,3.. 3,1), the if statement for combining 1-3 works as expected resulting in 8, so I know it works correctly, but of course I cannot combine two blocks at a time anymore after removing it. The 'mergedThisRound' property is to prevent having blocks merge more than once per turn.


Is there any way I can have it check three blocks to see if they can combine? Am I going about this incorrectly? Apologies for the long post, this has been frustrating me for quite some time. Thank you in advance for taking the time to help!


Aucun commentaire:

Enregistrer un commentaire