Author Topic: Side swapping  (Read 3738 times)

eric22222

  • Fanatic
  • ***
  • Posts: 174
    • View Profile
    • Eric Online
    • Email
Side swapping
« on: 2007-05-02, 08:24:29 AM »
Little background info here: as you may remember me saying, the planet where our game takes place is tidally locked to its sun. The level I'm working on is a mountain range halfway across the planet. One side is covered in vegetation, the other is bare rock. The feature I thought up for this is being able to swap back and forth from side to side by pressing shift. (A quick picture is here)

This is the piece of code that's activates when the player presses shift:
Code: [Select]
With ProjectObj.GamePlayer.PlayerSprite
 If NOT (.rDef.SolidTest((-.X+9568), .Y) OR .rDef.SolidTest((-.X+9568), .Y+31) OR .rDef.SolidTest((-.X+9599), .Y) OR .rDef.SolidTest((-.X+9599), .Y+31))Then
  .X = -.X + 9568
  .DX = -.DX
  If left(.rDef.Name,14) = "Player_jumping" Then
   Direction = -Direction + 1
   Change_to_jumping
  End If
  If (.rDef.Name = "Player_"+MapNum) OR (.rDef.Name = "Player_spear_"+MapNum) Then
   If (.CurState MOD 2) = 0 Then .CurState = .CurState + 1
   If (.CurState MOD 2) = 1 Then .CurState = .CurState - 1
  End If
 End If
End With

Now the trouble is, every once in a while (and only while moving right), the swap function crashes the game, giving me "subscript out of range". Any ideas will be helpful.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Side swapping
« Reply #1 on: 2007-05-02, 02:07:09 PM »
How many states does the sprite have?  I'm guessing that the following is happening:

.CurState == K where K the max number of states, and K is even.
so when the line
Code: [Select]
If (.CurState MOD 2) = 0 Then .CurState = .CurState + 1executes, it attempts to increase .CurState to greater than K, that is, greater than the max number of states.  Hence, subscript out of range when the engine tries to access the sprite's [K+1]th state.

What you need to do, in that case, is simply make sure that .CurState doesn't get to that point.  If you want it to loop around back to zero, you could simply change the line to this:

Code: [Select]
If (.CurState MOD 2) = 0 Then .CurState = (.CurState + 1) MOD .rDef.StateCount
I'm not sure if that's the answer, but it's highly likely that either that, or the opposite is true (with the line right below it).  My guess, however, is that it's not a problem with the line below, since 0 MOD 2 != 1
Edward Dassmesser

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2733
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Side swapping
« Reply #2 on: 2007-05-02, 02:52:18 PM »
Another possibility is that the state you switched to has fewer frames than the state you switched from, and the current frame is not valid in the new state.  You might have to re-set the current frame to 0 when switching states to make sure it's not referring to a frame that's invalid for this state.

eric22222

  • Fanatic
  • ***
  • Posts: 174
    • View Profile
    • Eric Online
    • Email
Re: Side swapping
« Reply #3 on: 2007-05-02, 07:29:53 PM »
Let's see...

Durnurd, I thought that might be the case, and added a little message function to tell me what state crashed the game, and I always ended up getting a crash when curstate == 3 (gah, swapping syntaxes...).

BlueMonkmn, that sounds like a very good answer. I'll try that out.

<EDIT> That must've been it. By setting .curframe to 0 whenever it swaps, crashes have stopped completely. I guess my left-facing sprite has a few less frames than the right-facing counterpart. That explains why it only happened every once in a while.

<EDIT++> 150 posts!
« Last Edit: 2007-05-02, 07:36:48 PM by eric22222 »