Author Topic: forced scrolling modification  (Read 19860 times)

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
forced scrolling modification
« on: 2005-08-01, 03:09:06 PM »
Hi, just wanted to know if it would be possible to modify the forced scrolling section of this script so that it would still scroll right on the map its currently set to (2-4BonusLevel), but scroll up for another map (4-2SpaceFlight). (Also, it would need to start at the bottom of the map [heh, obviously]; so something extra might need to be added in to tell it to start there). Here's the script I have right now, it does the forced scrolling, the horizontal window split (for 2 player), and a jump button:

Code: [Select]
' ======== INITIAL STARTUP SCRIPT (Number 0) =========

Sub Player_OnPlayInit()
HostObj.StartScript=1
End Sub

HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16



Option Explicit

Dim XOff, YOff

Dim P2OffsetX, P2OffsetY
Dim P2Actions, intSprTwo

Const nSprWidth = 41
Const nSprHeight = 75

Sub Display_KeyDown(KeyCode, Shift)
' E=69, S=83, D=68, F=70
If KeyCode = 69 Then P2Actions = P2Actions Or ACTION_UP
If KeyCode = 83 Then P2Actions = P2Actions Or ACTION_LEFT
If KeyCode = 68 Then P2Actions = P2Actions Or ACTION_DOWN
If KeyCode = 70 Then P2Actions = P2Actions Or ACTION_RIGHT
End Sub

Sub Display_KeyUp(KeyCode, Shift)
If KeyCode = 69 Then P2Actions = P2Actions And Not ACTION_UP
If KeyCode = 83 Then P2Actions = P2Actions And Not ACTION_LEFT
If KeyCode = 68 Then P2Actions = P2Actions And Not ACTION_DOWN
If KeyCode = 70 Then P2Actions = P2Actions And Not ACTION_RIGHT
End Sub

Sub Player_OnAfterMoveSprites()
  Dim oMap, oPlayer, oLayer, nLyrWid, nLyrHgt, i
  Set oPlayer = ProjectObj.GamePlayer
  Set oMap = oPlayer.rMap


'-----Two-Player-Stuff-----
'If ProjectObj.GamePlayer.rMap.name = "0-4LevelSelect" or ProjectObj.GamePlayer.rMap.name = "map2" or ProjectObj.GamePlayer.rMap.name = "map3" or ProjectObj.GamePlayer.rMap.name = "map4" then

  If ProjectObj.GamePlayer.rMap.name <> "0-4LevelSelect" then
      intSprTwo = -1
oPlayer.ScrollMarginY = 185
  else
Set oLayer = oMap.MapLayer(1)
      If intSprTwo < 0 then
          For i = 0 to oLayer.SpriteCount - 1
              If oLayer.Sprite(i).rDef.Name = "Player2" then intSprTwo = i
          next
      End If
      With oLayer.Sprite(intSprTwo)
oPlayer.ScrollMarginY = 62
           If .ProcessAction(P2Actions) Then
              .CurState = (.CurState Mod (.rDef.Template.StateCount \ 2)) _
                        + .rDef.Template.StateCount \ 2
              .CurFrame = .CurFrame Mod .rDef.StateFrameCount(.CurState)
           Else
              .CurState = (.CurState Mod (.rDef.Template.StateCount \ 2))
           End If

          oMap.ViewTop = 240
          If P2OffsetX + oPlayer.ScrollMarginX > .X Then
              P2OffsetX = .X - oPlayer.ScrollMarginX
          End If
          If P2OffsetX + oMap.ViewWidth - oPlayer.ScrollMarginX < .X + nSprWidth Then
              P2OffsetX = .X - oMap.ViewWidth + oPlayer.ScrollMarginX + nSprWidth
          End If
          If P2OffsetY + oPlayer.ScrollMarginY > .Y Then
              P2OffsetY = .Y - oPlayer.ScrollMarginY
          End If
          If P2OffsetY + oMap.ViewHeight - oPlayer.ScrollMarginY < .Y + nSprHeight Then
              P2OffsetY = .Y - oMap.ViewHeight + oPlayer.ScrollMarginY + nSprHeight
          End If

          If P2OffsetX < 0 Then P2OffsetX = 0
          If P2OffsetY < 0 Then P2OffsetY = 0
           nLyrWid = oLayer.Columns * 32
           nLyrHgt = oLayer.Rows * 32
          If P2OffsetX > nLyrWid - oMap.ViewWidth Then P2OffsetX = nLyrWid - oMap.ViewWidth
          If P2OffsetY > nLyrHgt - oMap.ViewHeight Then P2OffsetY = nLyrHgt - oMap.ViewHeight
          oMap.Draw P2OffsetX, P2OffsetY, False
          oMap.ViewTop = 0
      End With
  End If

'-----Automatic-Scrolling-Stuff-----
  If ProjectObj.GamePlayer.rMap.name = "2-4BonusLevel" then
      XOff = XOff + 1
      With ProjectObj.GamePlayer
          if .PlayerSprite.X <= XOff then
              if .PlayerSprite.DX < 0 then .PlayerSprite.DX = 1
              .PlayerSprite.DX = .PlayerSprite.rDef.Template.MoveSpeed
              if .PlayerSprite.X < XOff - .PlayerSprite.Width / 2 then
                  XOff = 1
              End if
          end if
          .rMap.Draw XOff, YOff
      End With
  End If

End Sub

Sub Player_OnControllerMove(OldActions, NewActions)

'-----Jumping-Player-----
  With ProjectObj.GamePlayer.PlayerSprite
      If (Not OldActions) And NewActions And ACTION_BUTTON2 Then
          If (.rDef.SolidTest(.X, .Y + .Height) Or .rDef.SolidTest(.X + .Width - 1, .Y + .Height)) Or (Not .pRideOnRef Is Nothing) Then
              .DY = - .rDef.Template.JumpHeight
          End If
      End If
  End With

End Sub

intSprTwo = -1
HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.SinkObjectEvents CurrentDisplay, "Display"
HostObj.ConnectEventsNow()
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Scrolling
« Reply #1 on: 2005-08-01, 04:32:09 PM »
If I understand that script correctly, it's redrawing the map over the top of the already drawn map on every frame just to show the custom scroll position.  Is that right?  That seems rather inefficient.  Rather than re-drawing the map, couldn't you just change the MapScrollX and MapScrollY properties of the player object?  You could also use those properties to retrieve the existing scroll position.  That way if, for example, you start the player at the bottom of the map, you can just scroll up from the existing position rather than having to calculate the proper offset for the starting position.

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
forced scrolling modification
« Reply #2 on: 2005-08-01, 06:01:42 PM »
.....
 :?

mmm  sorry, not sure what your saying. heh, i didnt write the script, it was a combined effort on your and durnurd's part..
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
forced scrolling modification
« Reply #3 on: 2005-08-01, 06:04:09 PM »
Well, excuse me. ;D

Actually, I wrote the automatic scrolling script a while ago, and didn't know of any other way to scroll the map.

The fact is, this script was not written with modifiability entirely in mind.  If I were to rewrite this script, I would change quite a bit of stuff so that it could work in more general terms (scrolling in any direction, scrolling propperly using MapScrollX and MapScrollY).  Using this same script, it would take a bit of changing to allow scrolling up as well.
Edward Dassmesser

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
forced scrolling modification
« Reply #4 on: 2005-08-01, 06:30:39 PM »
hmmm, so what if i copy and paste it so that its not 1 section trying to do 2, or would that not help?

---edit---
ok, got up the nerve to try it and this is what i made:
Code: [Select]
'Automatic Scrolling Stuff 2
  If ProjectObj.GamePlayer.rMap.name = "4-2SpaceFlight" then
      YOff = YOff - 1
      With ProjectObj.GamePlayer
          if .PlayerSprite.Y <= YOff then
              if .PlayerSprite.DY < 0 then .PlayerSprite.DY = 1
              .PlayerSprite.DY = .PlayerSprite.rDef.Template.MoveSpeed
              if .PlayerSprite.Y < YOff - .PlayerSprite.Height / 2 then
                  YOff = 1
              End if
          end if
          .rMap.Draw XOff, YOff
      End With
  End If


i put it between the 1st auto scroll and hte end sub right below it. it worked, but only kinda. its drawing the map over hte inventory stuff at the top, so i need a wat ot tell it to draw it 32 pixils lower, and its not starting at the bottom of the map, im not really sure where its starting (i think right at the top).

---edit-2---
ok, fixed the inventory problem, i forgot to set the top map display to 32 (in the map editor). ok, so that just leaves the problem that its starting right at the top of the map (yes, im sure it is now b/c i put a few tiles in the map to see ifhtats what was happening.
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
forced scrolling modification
« Reply #5 on: 2005-08-01, 09:51:06 PM »
Well, that's.... one way to do it, I suppose.

You'll need a line in there to tell it that YOff doesn't start at 0 (which it does right now) but rather, at
Code: [Select]
YOff = ProjectObj.GamePlayer.rMap.Height - ProjectObj.GamePlayer.rMap.ViewHeight

Put this in the same relative place that the line XOff = 1 is at
Edward Dassmesser

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
forced scrolling modification
« Reply #6 on: 2005-08-01, 09:56:23 PM »
Oh, and since you are going up instead of down, you'll want to change the place that says

Code: [Select]
if .PlayerSprite.DY < 0 then .PlayerSprite.DY = 1
.PlayerSprite.DY = .PlayerSprite.rDef.Template.MoveSpeed
if .PlayerSprite.Y < YOff - .PlayerSprite.Height / 2 then


Code: [Select]
if .PlayerSprite.DY > 0 then .PlayerSprite.DY = -1
.PlayerSprite.DY = -.PlayerSprite.rDef.Template.MoveSpeed
if .PlayerSprite.Y > YOff + .rMap.ViewHeight - .PlayerSprite.Height / 2 then
Edward Dassmesser

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
forced scrolling modification
« Reply #7 on: 2005-08-01, 10:00:31 PM »
ok so it should look like this then?

Code: [Select]
'Automatic Scrolling Stuff 2
  If ProjectObj.GamePlayer.rMap.name = "4-2SpaceFlight" then
      YOff = YOff - 1
      With ProjectObj.GamePlayer
          if .PlayerSprite.Y <= YOff then
              if .PlayerSprite.DY > 0 then .PlayerSprite.DY = -1
              .PlayerSprite.DY = -.PlayerSprite.rDef.Template.MoveSpeed
              if .PlayerSprite.Y > YOff + .rMap.ViewHeight - .PlayerSprite.Height / 2 then
                  YOff = ProjectObj.GamePlayer.rMap.Height - ProjectObj.GamePlayer.rMap.ViewHeight 'YOff = 1
              End if
          end if
          .rMap.Draw XOff, YOff
      End With
  End If


b/c that didnt change anything

---edit---
how about if i just use an actual number, like
YOff = 15000 (cuz thats hte map height)

hmm that didnt change anything either...
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
forced scrolling modification
« Reply #8 on: 2005-08-01, 10:07:14 PM »
Ah, XOff doesn't appear to be initialized anywhere (it should be, but oh well, the errors of my way come back to haunt me)

A quick fix would be to put this just after Sub Player_OnAfterMoveSprites()
Code: [Select]

if ProjectObj.GamePlayer.rMap.Name = "4-2SpaceFlight" and YOff = 0 then YOff = ProjectObj.GamePlayer.rMap.Height - ProjectObj.GamePlayer.rMap.ViewHeight
Edward Dassmesser

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
forced scrolling modification
« Reply #9 on: 2005-08-01, 10:14:17 PM »
Error playing map: Display must be open to flip
[OK]

stopped at line 29 on character 73:
object doesnt support this property or method: 'ProjectObj.GamePlayer.rMap.Height'
[OK]

line 29 is hte line you just told me to add (if ProjectObj.GamePlayer.rMap.Name = "4-2SpaceFlight" and YOff = 0 then YOff = ect....)

character 73 is the first Y of the part
YOff = ProjectObj.GamePlayer.rMap.Height - Project....


i fixed it by typing in the map's height (15000) and now it works. the only problem is that if i go off the top of hte map (ro even start to) i get stuck off screen and cant get back on, not even by waiting for the screen to scroll back over me
so what ill have to do is have a sprite the width of hte screen but like 2 pixils high just off screen, or maybe like 1 pixil on the screen, and have the player "bounce" off of it (using colisions), just like i did on the L2-4Bonus map.

Yay! It works!
in hindsight ill have to make a note to change that line if/when i change the map size (15000 was just a random # i typed in for testing). ahh well, it works, so im not gonna f**k with it  ;) .
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
forced scrolling modification
« Reply #10 on: 2005-08-02, 06:41:48 AM »
I think you could eliminate this whole block of code since I doubt you're using it:
Code: [Select]
         if .PlayerSprite.Y <= YOff then
              if .PlayerSprite.DY < 0 then .PlayerSprite.DY = 1
              .PlayerSprite.DY = .PlayerSprite.rDef.Template.MoveSpeed
              if .PlayerSprite.Y < YOff - .PlayerSprite.Height / 2 then
                  YOff = 1
              End if
          end if

Another interesting thing you could do is just use that sprite you made (for the player you bounce off of) -- use it to control the scrolling.  You could make a similar sprite on any map that you want to auto-scroll and it would be very simple to do all the scrolling then.

Bah, now look what you made me do -- I'm going to have to go try it myself and see how cool that is ;).

Whew -- OK, try this little project: http://gamedev.comdel.net/files/AutoScroller.zip

It uses any sprite whose name begins with "Scroller" to control the automatic scrolling on a map.  You don't even need to define a collision definition between the sprite and the player to keep then on the screen (I included better scripting ways of doing that).  So you should ignore the fact that the sprite is visible and 640 pixels wide actually :) (It can be invisible and small).  The script will "bounce" the player back onto the screen if he gets too close to the edge (within the scrolling margin, whatever you defined that to be).

Here's a copy of the script code for your convenience:
Code: [Select]
Dim ScrollerSprite

Function FindScrollerSprite()
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      For I = 0 To .SpriteCount - 1
         If Left(.Sprite(I).rDef.Name, 8) = "Scroller" Then
            Set FindScrollerSprite = .Sprite(I)
            Exit Function
         End If
      Next
   End With
End Function

Sub Player_OnAfterMoveSprites()

   If IsEmpty(ScrollerSprite) Then
      Set ScrollerSprite = FindScrollerSprite
   End If

   If Not ScrollerSprite.rDef.rLayer.pMap Is ProjectObj.GamePlayer.rMap Then
      Set ScrollerSprite = FindScrollerSprite
   End If


   With ProjectObj.GamePlayer
      .MapScrollX = ScrollerSprite.X
      .MapScrollY = ScrollerSprite.Y
      If .PlayerSprite.Y > .MapScrollY + .rMap.ViewHeight - .ScrollMarginY - .PlayerSprite.Height Then
         .PlayerSprite.DY = -.PlayerSprite.rDef.Template.MoveSpeed
      ElseIf .PlayerSprite.Y < .MapScrollY + .ScrollMarginY Then
         .PlayerSprite.DY = .PlayerSprite.rDef.Template.MoveSpeed
      End If

      If .PlayerSprite.X > .MapScrollX + .rMap.ViewWidth - .ScrollMarginX - .PlayerSprite.Width Then
         .PlayerSprite.DX = -.PlayerSprite.rDef.Template.MoveSpeed
      ElseIf .PlayerSprite.X < .MapScrollX + .ScrollMarginX Then
         .PlayerSprite.DX = .PlayerSprite.rDef.Template.MoveSpeed
      End If
   End With

End Sub

HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
forced scrolling modification
« Reply #11 on: 2005-08-02, 11:48:17 AM »
one mroe quick question; in this level, i'm using a different sprite that's a bit larger than the regular one, so would i just add something like

Code: [Select]
 If ProjectObj.GamePlayer.rMap.name = "4-2SpaceFlight" then
oPlayer.ScrollMarginX = 75
oPlayer.ScrollMarginY = 100
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
forced scrolling modification
« Reply #12 on: 2005-08-03, 05:43:07 AM »
Yeah, that syntax should work, although I'm a bit suspicious about your numbers -- why your x scroll margin would by smaller than your y scroll margin.  Are you sure you mean to allow the sprite to get within 75 pixels of the edge of the screen horizontally and within 100 pixels vertically?  75 seems like a pretty small horizontal scroll margin.

Also, on the maps where the auto-scrolling is going on, you probably want to set the scroll margin to zero unless you like the player bouncing off an invisible box that's smaller than the map display area.

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
forced scrolling modification
« Reply #13 on: 2005-08-03, 09:23:35 AM »
heh yea, just ignor those numbers, i wasnt thinking when i wrote them in. thats the size of the player sprite, not the scroll margines i want  ::) .
it should actually be like 280x170 on a normal map. ill give that and 0x0 a try and see what looks better.

---edit---
hmmm, thats weird, both sets of scroll margines look exactly the same in the game. well, whatever works  :) . one other thing thats really weird. if hte player goes up and off the screen only just a slight bit then stops, he gets pulled completely off screen and stuck there. im gonna fix it with that "solid sprite" thing, i just figured id mention it.
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
forced scrolling modification
« Reply #14 on: 2005-08-03, 04:32:11 PM »
It should not be possible for the player to move off the screen if the script was implemented correctly.  Since the script (the one I sent last) is only setting the scroll position properties and not overriding the scroll position (by re-drawing the map) any more, that should really be impossible (unless the sprite you're calling the player is not really the "player sprite").  The map is re-scrolled to put the player on the screen every frame before the map is drawn.  I think you'll want to figure out the correct way to fix it rather than using the sprite solution because you would need one sprite for each side of the screen.  Managing 4 sprites is probably going to be harder to get correct then fixing the script.  I think something's gone terribly wrong with your script if your player can leave the screen, and putting sprites there may not even fix it.

Also, aren't you suspicious that the script is not even running or working if it seemed to have no effect on the scroll margins?