Push the players along with your Camera scrolling [4.5.9]

SciNEStist

Well-known member
For anyone with an autoscroll in their game, and want a solid method of preventing the player from disappearing off-camera, this is for you! Not only does it push your player along with the camera, It will be able to help you "squish" the player if they get stuck between a wall and the edge

This is designed to work well with the Improved Camera Scrolling script, but if you have your own autoscroller, this should probably work just as well with some minor tweaks
Step 0: back your stufff up first!

Step 1: set your edge reaction of your player to something that will just stop the player from moving horizontally. This script doesnt use those bounds, so no need for anything fancy. For example

Code:
LDA #$00
STA Object_h_speed_lo,x
STA Object_h_speed_hi,x
RTS

Save that and name it something like "stopatedge.asm" somewhere inside your Base_4_5 folder, then you can point to that file in project settings >script settings, scroll down to one of the "Edge Reaction #" and point it to your new file. then go to the "Project Lablels" tab and make sure you name the corrosponding reaction # to "stop at edge"

Step 2: click Scripts >Defined Scripts >Subroutines > Handle Physics (should be the top one)

In there, look for "doHandlePhysics:" and immediatly under that, paste this code:
Code:
LDA ScreenFlags00
AND #%00000010 ; is autoscroll checked?? remove this and the 3 lines below if you arent using this flag to autoscroll
BNE +
    JMP +skippush
+
CPX player1_object ; is it player 1?
BEQ +
CPX player2_object ; is it player 2?
BEQ +
JMP +skippush
+
LDA Object_x_hi,x
SBC camX
CMP #$03 ; if the player is within 3 pixels of the left edge, we squish
BCS +skipsquishR
;PUT WHAT YOU WANT TO HAPPPEN WHEN A PLAYER IS SQUISHED HERE
+skipsquishR
CMP #$08 ; if the player is within 8 pixels of the left edge, we push right
BCS +skippushR
    LDA #$01
    STA tempA
    STA tempB
    JMP doMoveRight
+skippushR
CMP #$F4 ; if the player is too far off the right side, we squish (if your player is fatter, decrease this number)
BCC +skipsquishL
;PUT WHAT YOU WANT TO HAPPPEN WHEN A PLAYER IS SQUISHED HERE
+skipsquishL
CMP #$F0 ; if the player is close to the right side, we push left (if your player is fatter, decrease this number)
BCC +skippushL
    LDA #$01
    STA tempA
    STA tempB
    JMP doMoveLeft
+skippushL
+skippush

Step 3: search way down further, and you will see a commented out line: ";doMoveLeft:", uncomment it by changing it to "doMoveLeft:" save your changes!

Step 4: there are designated parts where you can put anything you want to happen when a player gets squished. (a screen warp for example) be careful not to pack too much in there. Make a subroutine if you want to get fancy with it

Done!
 

TalkingCat

Member
I had some problems when executing the death routine if the screen scrolling continued while the player was flattening.
To avoid this, you need to add a command to stop scrolling the screen before executing the death routine.
Code:
LDA ScreenFlags00
AND #%00000000
STA ScreenFlags00
Maybe it's an obvious thing, but I've spent some time figuring it out, so I'll leave it here)

This is what my Push the player code looks like
Code:
LDA ScreenFlags00
AND #%00000010 ; а
BNE +
    JMP +skippush
+
CPX player1_object ; is it player 1?
BEQ +
JMP +skippush
+
LDA Object_x_hi,x
SBC camX
CMP #$03
BCS +skipsquishR

        LDA ScreenFlags00
        AND #%00000000
        STA ScreenFlags00

JSR playerDeath

+skipsquishR
CMP #$08
BCS +skippushR
    LDA #$01
    STA tempA
    STA tempB
    JMP doMoveRight
+skippushR
CMP #$F0
BCC +skipsquishL


        LDA ScreenFlags00
        AND #%00000000
        STA ScreenFlags00

JSR playerDeath

+skipsquishL
CMP #$F4
BCC +skippushL
    LDA #$01
    STA tempA
    STA tempB
    JMP doMoveLeft
+skippushL
+skippush
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

before
Video_230803224546.gif after Video_230803224430.gif
 

TalkingCat

Member
A small addition to the lesson. When the pause button is pressed, the scrolling does not stop and the game breaks.
Video_230905104204.gif
To stop scrolling during a pause, I inserted this code snippet in the script "newcamerahandler.asm" :
Code:
;;THIS PART IS THE FLAG FOR AUTOSCROLL, CHANGE TO ANOTHER FLAG IF DESIRED
LDA ScreenFlags00
AND #%00000010 ; is autoscroll checked??
BNE +
    JMP +scrollfollowsplayer ; if not, go to follow mode
+


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 LDA isPaused      ;; load the isPaused variable into the accumulator.            ;;
  BEQ +canMoveS    ;; if the game is paused go to next line, if not go to canMove.  ;;
        RTS

  +canMoveS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;       
    
        

;;autoscroll is checked, so first we get the speed
    LDA screenSpeed

Video_230905104420.gif
I used the pause script from this tutorial https://www.nesmakers.com/index.php?threads/non-textbox-based-pause.2421/
 

Toad64

New member
So weird question regarding this script, I'm looking in the "doHandlePhysicsShooter" script, and I don't see any call to "doMoveLeft"? Am I looking in the wrong place?
edit: I realized this was probably designed for the scrolling platformer and not a shooter? Would this still work with the scrolling shooter somehow?
 
Last edited:

SciNEStist

Well-known member
yeah, the player object is moved differently in a shooter module. its possible to adapt the principles though.
 
Top Bottom