10-15-2012, 03:31 PM
I doubt anyone will be able to make use of the posted code oput of its context.
It also isn't the most elegant way to code such a selection. I'm pretty sure MM3 on the NES didn't check for collisions between the selection marker and stage icon to determine the position.
You have nine stages. Easy solution would be to have a variable for the selection with values from 0 to 8 - let us call if selectionVar for fancyness.
Interpret the stage icons like this:
0 1 2
3 4 5
6 7 8
When the selector has been moved, place it at the position of the appropriate stage icon.
To move the selector to the left (<-) you can simply subtract 1 from the current value.
Why I use the modulo operator? To prevent moving from 3 to 2 (middle row to top row) for example. What it does? This:
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
8 % 3 = 2
So the selection will not move when it is on 0, 3 or 6 (the leftmost icons)! Fancy, isn't it?
Move right:
Move up:
Move down:
Afterwards, in every case independent of the direction, reposition the selection marker:
It also isn't the most elegant way to code such a selection. I'm pretty sure MM3 on the NES didn't check for collisions between the selection marker and stage icon to determine the position.
You have nine stages. Easy solution would be to have a variable for the selection with values from 0 to 8 - let us call if selectionVar for fancyness.
Interpret the stage icons like this:
0 1 2
3 4 5
6 7 8
When the selector has been moved, place it at the position of the appropriate stage icon.
To move the selector to the left (<-) you can simply subtract 1 from the current value.
Code:
if ((selectionVar % 3) > 0) {
selectionVar--;
}
Why I use the modulo operator? To prevent moving from 3 to 2 (middle row to top row) for example. What it does? This:
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
8 % 3 = 2
So the selection will not move when it is on 0, 3 or 6 (the leftmost icons)! Fancy, isn't it?
Move right:
Code:
if ((selectionVar % 3) < 2) {
selectionVar++;
}
Move up:
Code:
if (selectionVar > 3) {
selectionVar -= 3;
}
Move down:
Code:
if (selectionVar < 6) {
selectionVar += 3;
}
Afterwards, in every case independent of the direction, reposition the selection marker:
Code:
switch (selectionVar) {
case 0:
x = sparkManCubeObj.x;
y = sparkManCubeObj.y;
break;
case 1:
x = needleManCubeObj.x;
y = needleManCubeObj.y;
break;
// ET CETERA OKAY ;)
}