[4.5.9] Holding B to Run

dale_coop

Moderator
Staff member
A small tutorial to show how to implement the Holding B to Run in your projects.

HoldingB.gif

The principle is simple, we'll modify the Physics to modify the speed when B is pressed.

INSTRUCTIONS

Modify your Handle Physics script (you can find it in the "Project Settings > Script Settigns" under "SubRoutines").
Find those exact lines in the script (around line 110 or 270 or 280 depending of the module used):
Code:
        LDY Object_type,x
        LDA ObjectMaxSpeed,y
        ASL
        ASL
        ASL
        ASL
        ;AND #%00001111
        STA myMaxSpeed
        LDA ObjectMaxSpeed,y
        LSR
        LSR
        LSR
        LSR
        STA myMaxSpeed+1

And replace those lines with:
Code:
        LDY Object_type,x
        LDA ObjectMaxSpeed,y
        STA tempA
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;; if holding B
        CPX player1_object
        BNE +skip   ;; if not the player object, we skip
            LDA gamepad
            AND #%00000010  ;; B button
            BEQ +skip
                LDA tempA
                ASL     ;; double the speed
                STA tempA
        +skip:
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        LDA tempA
        ASL
        ASL
        ASL
        ASL
        ;AND #%00001111
        STA myMaxSpeed
        LDA tempA
        LSR
        LSR
        LSR
        LSR
        STA myMaxSpeed+1

Voilà, that's it.
 

NightMusic

Active member
A small tutorial to show how to implement the Holding B to Run in your projects.

View attachment 7869

The principle is simple, we'll modify the Physics to modify the speed when B is pressed.

INSTRUCTIONS

Modify your Handle Physics script (you can find it in the "Project Settings > Script Settigns" under "SubRoutines").
Find those exact lines in the script (around line 110 or 270 or 280 depending of the module used):
Code:
        LDY Object_type,x
        LDA ObjectMaxSpeed,y
        ASL
        ASL
        ASL
        ASL
        ;AND #%00001111
        STA myMaxSpeed
        LDA ObjectMaxSpeed,y
        LSR
        LSR
        LSR
        LSR
        STA myMaxSpeed+1

And replace those lines with:
Code:
        LDY Object_type,x
        LDA ObjectMaxSpeed,y
        STA tempA
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;; if holding B
        CPX player1_object
        BNE +skip   ;; if not the player object, we skip
            LDA gamepad
            AND #%00000010  ;; B button
            BEQ +skip
                LDA tempA
                ASL     ;; double the speed
                STA tempA
        +skip:
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        LDA tempA
        ASL
        ASL
        ASL
        ASL
        ;AND #%00001111
        STA myMaxSpeed
        LDA tempA
        LSR
        LSR
        LSR
        LSR
        STA myMaxSpeed+1

Voilà, that's it.
This is awesome! Run Agent Cooper Run! (get that next cup of midnight black coffee!) :) thank you
 

TheRetroBro

Active member
A small tutorial to show how to implement the Holding B to Run in your projects.

View attachment 7869

The principle is simple, we'll modify the Physics to modify the speed when B is pressed.

INSTRUCTIONS

Modify your Handle Physics script (you can find it in the "Project Settings > Script Settigns" under "SubRoutines").
Find those exact lines in the script (around line 110 or 270 or 280 depending of the module used):
Code:
        LDY Object_type,x
        LDA ObjectMaxSpeed,y
        ASL
        ASL
        ASL
        ASL
        ;AND #%00001111
        STA myMaxSpeed
        LDA ObjectMaxSpeed,y
        LSR
        LSR
        LSR
        LSR
        STA myMaxSpeed+1

And replace those lines with:
Code:
        LDY Object_type,x
        LDA ObjectMaxSpeed,y
        STA tempA
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;; if holding B
        CPX player1_object
        BNE +skip   ;; if not the player object, we skip
            LDA gamepad
            AND #%00000010  ;; B button
            BEQ +skip
                LDA tempA
                ASL     ;; double the speed
                STA tempA
        +skip:
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        LDA tempA
        ASL
        ASL
        ASL
        ASL
        ;AND #%00001111
        STA myMaxSpeed
        LDA tempA
        LSR
        LSR
        LSR
        LSR
        STA myMaxSpeed+1

Voilà, that's it.
will this work in platformer if Im using b to shoot?
 

dale_coop

Moderator
Staff member
it should work too (like when Mario shoot a fireball before starting to run ;))
(....Unless your 'B shoot' stops the player's movements)
 

TheRetroBro

Active member
it should work too (like when Mario shoot a fireball before starting to run ;))
(....Unless your 'B shoot' stops the player's movements)
So I implemented this and it works . My only questions is can I change the increase speed to say 20% instead of 50? 50 is just way too fast!
 

dale_coop

Moderator
Staff member
If you want to use a more precise speed run, you could replace the
Code:
LDA tempA
ASL     ;; double the speed
STA tempA

With:
Code:
LDA tempA
CLC
ADC #16 ;; you can use any value here from 00 to 255 to adjust the running speed
STA tempA
 

Luxnolucas

Member
Yep, i try to use this
Code:
pha
  CPX player1_object
  LDA Object_h_speed_hi,x
  SEC
  SBC #$02   ; Ralentissement
  STA Object_h_speed_hi,x
 
  LDA Object_v_speed_hi,x
  SEC
  SBC #$00   ; Ralentissement
  STA Object_v_speed_hi,x
pla

But it's not really smooth... And ultimately, managing the scrolling speed with keys is more suitable than with tiles.
thanks
 
Hey Dale it's me that annoying guy again! just curious your script currently doubles the speed... is there a way to make the speed 1.5 times the normal amount?
because doubling the speed seems too fast for what I'm trying to do
 

SciNEStist

Well-known member
the part where it has
Code:
LDA tempA
ASL     ;; double the speed
STA tempA

can be replaced with anything you want.
Just keep in mind that "myMaxSpeed" is the lo amount (sub pixels), and "myMaxSpeed+1" is the hi amount (pixels) and from a quick look, its stored as "HHHHLLLL" So you want to save your variable accordingly
 

zeldafreakneo

New member
If you want to use a more precise speed run, you could replace the
Code:
LDA tempA
ASL     ;; double the speed
STA tempA

With:
Code:
LDA tempA
CLC
ADC #16 ;; you can use any value here from 00 to 255 to adjust the running speed
STA tempA

How could you use this to subtract speed?
 

General Panzer

Well-known member
So I used this a little differently. I wanted the bats in Frontiers to charge the player. I added a check against the bats action step instead of the players button. The bat starts at 1/3 speed, dives, and then when he changes to the action step that I want him to charge in the speed boost takes over :)

So if you are designing a monster that needs a speed boost for an attack this might be your solution!


LDY Object_type,x
LDA ObjectMaxSpeed,y
STA tempA
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; if holding B
LDA Object_type,x
CMP #33
BEQ +Bats
JMP +skip
+Bats
TXA
STA temp ;; assumes the object we want to move is in x.
GetActionStep temp ;; get current action state
CMP #02 ;compare to 6
BNE +skip
LDA tempA
ASL
ASL
ASL ;; double the speed
STA tempA
+skip:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LDA tempA
ASL
ASL
ASL
ASL
;AND #%00001111
STA myMaxSpeed
LDA tempA
LSR
LSR
LSR
LSR
STA myMaxSpeed+1
;;; now high max speed byte is the actual high byte of speed
;;; low max speed byte is the low byte of speed
 

dale_coop

Moderator
Staff member
So I used this a little differently. I wanted the bats in Frontiers to charge the player. I added a check against the bats action step instead of the players button. The bat starts at 1/3 speed, dives, and then when he changes to the action step that I want him to charge in the speed boost takes over :)

So if you are designing a monster that needs a speed boost for an attack this might be your solution!


Amazing idea !
 
Top Bottom