2. Yes, when you have code between a "With" and an "End With" anything beginning with a "." is an extension to the With expression. Therefore you don't need oPlayerSprite. You can simply check "If .rDef.Template.StateCount = 36", and set ".CurState = RectToPolarState(DX, DY)"
Everything became much clearer quite suddenly! Between the VBScript help files on MSDN and what I know of database programming I'm finally making some progress here. Plus finally getting how the objects and variables are tree-ed (sic) helps too.
I still have trouble with the capturing key-presses. I'm trying to set it up so that the sub runs while the key is pressed and only while the key is pressed (regardless of the other keys states). I want it to run another sub (Sub DoSpriteSteady) while neither the up or down key is pressed. I can get the first part to work, but I seem to have difficulting XOr-ing the subs.
Plus, I still get a subscript out of range when I rotate the sprite past 180 degrees. I'm wondering if maybe it due to the way the sprite is set up. I have a 36 state player sprite, I started the sprite facing left, then clicked the "36 state button" and the error always occurs while the sprite moves through the left facing state. That should be state 18 (?) starting from zero, so it should be within the boundary of the array.
Anyway, here's what I have at present (minus the additions for checking to see if a key is held down)
Sub Player_OnControllerMove(OldActions, NewActions)
If ProjectObj.GamePlayer.rMap.Name = "RB1-1" Then
If (Not OldActions) And NewActions And ACTION_UP Then DoSpriteAccelerate
If (Not OldActions) And NewActions And ACTION_DOWN Then DoSpriteDeccelerate
If (Not OldActions) And NewActions And ACTION_LEFT Then DoSpriteLeft
If (Not OldActions) And NewActions And ACTION_RIGHT Then DoSpriteRight
End If
End Sub
Sub DoSpriteAccelerate
Dim DX, DY
With ProjectObj.GamePlayer.PlayerSprite
.rDef.Template.MoveSpeed = 15
.X =.X + (.Width -.Width)/2
.Y =.Y + (.Width -.Width)/2
GetStateDeltas DX, DY
.DX = (DX * .rDef.Template.MoveSpeed)
.DY = (DY * .rDef.Template.MoveSpeed)
.CurState = RectToPolarState(DX, DY)
End With
End Sub
'Sub DoSpriteSteady
'Dim DX, DY
' With ProjectObj.GamePlayer.PlayerSprite
' .rDef.Template.MoveSpeed = 11
' .X =.X + (.Width -.Width)/2
' .Y =.Y + (.Width -.Width)/2
' GetStateDeltas DX, DY
' .DX = (DX * .rDef.Template.MoveSpeed)
' .DY = (DY * .rDef.Template.MoveSpeed)
' .CurState = RectToPolarState(DX, DY)
' End With
'End Sub
Sub DoSpriteDeccelerate
Dim DX, DY
With ProjectObj.GamePlayer.PlayerSprite
.rDef.Template.MoveSpeed = 8
.X =.X + (.Width -.Width)/2
.Y =.Y + (.Width -.Width)/2
GetStateDeltas DX, DY
.DX = (DX * .rDef.Template.MoveSpeed)
.DY = (DY * .rDef.Template.MoveSpeed)
.CurState = RectToPolarState(DX, DY)
End With
End Sub
Sub DoSpriteLeft
Dim DX, DY
With ProjectObj.GamePlayer.PlayerSprite
.CurState = (.CurState + 1) Mod 36 'line 60
GetStateDeltas DX, DY
.DX = (DX * .rDef.Template.MoveSpeed)
.DY = (DY * .rDef.Template.MoveSpeed)
.CurState = RectToPolarState(DX, DY)
End With
End Sub
Sub DoSpriteRight
Dim DX, DY
With ProjectObj.GamePlayer.PlayerSprite
.CurState = (.CurState +35) Mod 36
GetStateDeltas DX, DY
.DX = (DX * .rDef.Template.MoveSpeed)
.DY = (DY * .rDef.Template.MoveSpeed)
.CurState = RectToPolarState(DX, DY)
End With
End Sub
Function GetStateDeltas(DX, DY)
With ProjectObj.GamePlayer.PlayerSprite
Select Case .rDef.Template.StateType
Case STATE_SINGLE
If Abs(DX) + Abs(DY) > 1 Then
DX = .DX / (Abs(.DX) + Abs(.DY))
DY = .DY / (Abs(.DX) + Abs(.DY))
Else
If Abs(DX) + Abs(DY) < 1 Then DX = 0 : DY = -1
End If
Case STATE_LEFT_RIGHT
DY = 0
If (.CurState Mod 2) = 0 Then DX = -1 Else DX = 1
Case STATE_8_DIRECTION
Select Case (.CurState Mod 8)
Case 0 : DX = 0 : DY = -1
Case 1 : DX = 1 : DY = -1
Case 2 : DX = 1 : DY = 0
Case 3 : DX = 1 : DY = 1
Case 4 : DX = 0 : DY = 1
Case 5 : DX = -1 : DY = 1
Case 6 : DX = -1 : DY = 0
Case 7 : DX = -1 : DY = -1
End Select
Case Else
DX = Cos((.CurState Mod 36) * 3.14159 / 18)
DY = -Sin((.CurState Mod 36) * 3.14159 / 18)
End Select
End With
End Function
Function RectToPolarState(X, Y)
Dim Angle, Pi
Pi = 3.14159
If X <> 0 Then
Angle = Atn(-Y / X)
Else
Angle = -(Pi / 2) * Sgn(Y)
End If
If X < 0 Then
Angle = Pi + Angle
ElseIf Y > 0 Then
Angle = Pi * 2 + Angle
End If
RectToPolarState = ((Angle * 18) / Pi) Mod 36
End Function
HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16
If I haven't said so before, thanks for all the help!