Help with code

Hello, I'm making a CBS and find an error while creating it. Here's the code:

Code:
@> Key Input Processing: V[0043:MENU_KEY], (W) { Select, Cancel, Down, Up, }
@> Comment: -------- Ecolhendo o monstro --------
@> Conditional Branch: V[0043:MENU_KEY] == 1
  @> Play SE: Cursor1, 50%, 100%, 50
  @> Control Variables: V[0049:monstro atual] += 1
  @>
 : Branch End
@> Conditional Branch: V[0043:MENU_KEY] == 4
  @> Play SE: Cursor1, 50%, 100%, 50
  @> Control Variables: V[0049:monstro atual] -= 1
  @>
 : Branch End
@> Conditional Branch: V[0049:monstro atual] < 1
  @> Control Variables: V[0049:monstro atual] = 3
  @>
 : Branch End
@> Conditional Branch: V[0049:monstro atual] > 3
  @> Control Variables: V[0049:monstro atual] = 1
  @>
 : Branch End
@> Comment: ------------------------------------------------------
@> Conditional Branch: V[0049:monstro atual] == 1
  @> Conditional Branch: V[0028:m1_HP] > 0
    @> Control Variables: V[0044:cursor_batalha_x] = [0003:monstro 1]'s Screen X
    @> Control Variables: V[0045:cursor_batalha_y] = [0003:monstro 1]'s Screen Y
    @> Comment: Número da id da picture do monstro
    @> Control Variables: V[0055:id picture alvo] = 2
    @>
   : Else
    @> Control Variables: V[0049:monstro atual] += 1
    @>
   : Branch End
  @>
 : Branch End
@> Conditional Branch: V[0049:monstro atual] == 2
  @> Conditional Branch: V[0033:m2_HP] > 0
    @> Control Variables: V[0044:cursor_batalha_x] = [0004:monstro 2]'s Screen X
    @> Control Variables: V[0045:cursor_batalha_y] = [0004:monstro 2]'s Screen Y
    @> Comment: Número da id da picture do monstro
    @> Control Variables: V[0055:id picture alvo] = 3
    @>
   : Else
    @> Control Variables: V[0049:monstro atual] += 1
    @>
   : Branch End
  @>
 : Branch End
@> Conditional Branch: V[0049:monstro atual] == 3
  @> Conditional Branch: V[0038:m3_HP] > 0
    @> Control Variables: V[0044:cursor_batalha_x] = [0005:monstro 3]'s Screen X
    @> Control Variables: V[0045:cursor_batalha_y] = [0005:monstro 3]'s Screen Y
    @> Comment: Número da id da picture do monstro
    @> Control Variables: V[0055:id picture alvo] = 4
    @>
   : Else
    @> Control Variables: V[0049:monstro atual] += 1
    @>
   : Branch End
  @>
 : Branch End
@> Move Picture: 10, (V[0044:cursor_batalha_x], V[0045:cursor_batalha_y]), 100%, 0%, 0.0 sec, RGBS(100,100,100,100)
@> Comment: ------------------------------------------------------
@> Comment: ------------------------------------------------------
@> Conditional Branch: V[0043:MENU_KEY] == 5
  @> Erase Picture: 10
  @> Control Variables: V[0053:alvo X] = V[0044:cursor_batalha_x]
  @> Control Variables: V[0054:alvo y] = V[0045:cursor_batalha_y]
  @> Control Switches: S[0013:Atacar monstro] = ON
  @> Control Switches: S[0012:Atacar(escolhendo)] = OFF
  @>
 : Branch End
@> Conditional Branch: V[0043:MENU_KEY] == 6
  @> Control Variables: V[0044:cursor_batalha_x] = 8
  @> Control Variables: V[0045:cursor_batalha_y] = 183
  @> Move Picture: 10, (V[0044:cursor_batalha_x], V[0045:cursor_batalha_y]), 100%, 0%, 0.0 sec, RGBS(100,100,100,100)
  @> Control Switches: S[0011:Opções batalha] = ON
  @> Control Switches: S[0012:Atacar(escolhendo)] = OFF
  @>
 : Branch End


And here a video showing the problem:



While I press down button, the cursor just move to the monster and upper-left. But if I press up button, the cursor moves infinitely to upper-left. But this error only occurs if I have 1 or 2 monsters. If I have 3 monsters (max number) works fine.
What can I do to fix that?
I can't find any reason for the cursor to jump on more positions than enemies since you hardcoded every single case. So if there's no other event that could corrupt stuff, it should be impossible to fail the way your video demonstrates.

But okay, let's try something.

Did a little restructuring to your code, this variation will check through the entire troop depending on HOW the cursor was navigated (up or down) instead of incrementing in both cases. The process needs both an additional switch and variable to differ and count. To make it work this way at all, it's important to start choice-IDs for enemies at 0 (not 1) so we can simplify the process by using the divide-with-remainder-function (%=).

Labels used
[1: Repeat cursor down in check] [4: Repeat cursor up in check] [8: Loop troop-check if there are enemies alive]
[55: Enemy found, move cursor-pic] [58: Continue with check for confirm and cancel] [100: Exit process]

Spoiler: The Code
Code:
<>Key Input [MenuKey], Wait, {Down, Up, Confirm, Cancel}
<>Set Variable [CheckCounter] = 0
<>If Variable [MenuKey] == 0
 <>Label Jump: 100
 <>
:END If
<>If Variable [MenuKey] == 1
 <>Play SE: "Cursor1", 50%, 100%, 50
 <>Label Set: 1
 <>Set Variable [Choice] += 1
 <>Set Switch [CheckBackwards] = OFF
 <>
: END If
<>If Variable [MenuKey] == 4
 <>Play SE: "Cursor1", 50%, 100%, 50
 <>Label Set: 4
 <>Set Variable [Choice] -= 1
 <>Set Switch [CheckBackwards] = ON
 <>
: END If
<>If Variable [MenuKey] >= 5
 <>Label Jump: 58
 <>
: END If
<>Set Variable [Choice] %= 3
<>If Variable [Choice] == 0
 <>If Variable [Enemy1HP] > 0
  <>Set Variable [CursorX] = [Enemy1] ScreenX
  <>Set Variable [CursorY] = [Enemy1] ScreenY
  <>Set Variable [TargetPicture] = 2
  <>Label Jump: 55
  <>
 : Else
  <>Label Jump: 8
  <>
 : END If
 <>
: END If
<>If Variable [Choice] == 1
 <>If Variable [Enemy2HP] > 0
  <>Set Variable [CursorX] = [Enemy2] ScreenX
  <>Set Variable [CursorY] = [Enemy2] ScreenY
  <>Set Variable [TargetPicture] = 3
  <>Label Jump: 55
  <>
 : Else
  <>Label Jump: 8
  <>
 : END If
 <>
: END If
<>If Variable [Choice] == 2
 <>If Variable [Enemy3HP] > 0
  <>Set Variable [CursorX] = [Enemy3] ScreenX
  <>Set Variable [CursorY] = [Enemy3] ScreenY
  <>Set Variable [TargetPicture] = 4
  <>Label Jump: 55
  <>
 : Else
  <>Label Jump: 8
  <>
 : END If
 <>
: END If
<>Label Set: 8
<>Set Variable [CheckCounter] += 1
<>If Variable [CheckCounter] >= 3
 <>Note: (ALL ENEMIES ARE KNOCKOUT OR TROOP IS EMPTY)
 <>
: END If
<>If Switch [CheckBackwards] == ON
 <>Label Jump: 4
 <>
: Else
 <>Label Jump: 1
 <>
: END If
<>Label Set: 55
<>Move Picture: 10, ([CursorX], [CursorY]), 100%, 0%, 0.0s, RGBS(100, 100, 100, 100)
<>Label Jump: 100
<>Label Set: 58
<>If Variable [MenuKey] == 5
 <>Erase Picture: 10
 <>Set Variable [TargetX] = [CursorX]
 <>Set Variable [TargetY] = [CursorY]
 <>Set Switch [AttackEnemy] = ON
 <>Set Switch [AttackChoice] = OFF
 <>
: END If
<>If Variable [MenuKey] == 6
 <>Set Variable [CursorX] = 8
 <>Set Variable [CursorY] = 183
 <>Move Picture: 10, ([CursorX], [CursorY]), 100%, 0%, 0.0s, RGBS(100, 100, 100, 100)
 <>Set Switch [BattleChoice] = ON
 <>Set Switch [AttackChoice] = OFF
 <>
: END If
<>Label Set: 100
<>

~炬燵あ

Original thread: https://forums.rpgmakerweb.com/threads/help-with-code.149361/