Jump to content
waderain

Joystick question

Recommended Posts

anybody have some good simple smooth code for controls that only go left, right, up and down. No diagonal control

 

I've been experimenting but nothing I've done works very well.

Share this post


Link to post
Share on other sites

If I was doing it, I'd have a bit for left/right and a bit for up/down. If the up/down bit is on, don't look at left/right movement. If the left/right bit is on, don't look at up/down movement.

Share this post


Link to post
Share on other sites
Posted (edited)

Something like this?

 

if joy0up || joy0down then playery=playery+joy0up-joy0down:goto skiphoriz

playerx=playerx+joy0right-joy0left

skiphoriz

 

If you want left/right to have priority, use those first.

Edited by Nukey Shay

Share this post


Link to post
Share on other sites

Something like this?

 

if joy0up || joy0down then player0y=player0y+joy0up-joy0down:goto skiphoriz

player0x=player0x+joy0right-joy0left

skiphoriz

 

If you want left/right to have priority, use those first.

 

Those joystick thingies can only be used in if-thens. You get an Unresolved Symbol error if you try to use them in other ways.

Share this post


Link to post
Share on other sites

This works, but there's probably a better way to do it:



   ;***************************************************************
   ;
   ;  Variable aliases go here (DIMs).
   ;
   dim _Bit4_JoyU = y
   dim _Bit5_JoyD = y
   dim _Bit6_JoyL = y
   dim _Bit7_JoyR = y


   ;***************************************************************
   ;
   ;  Defines the edges of the playfield for an 8 x 8 sprite.
   ;  If your sprite is a different size, you'll need to adjust
   ;  the numbers.
   ;
   const _P_Edge_Top = 9
   const _P_Edge_Bottom = 88
   const _P_Edge_Left = 1
   const _P_Edge_Right = 153


   ;***************************************************************
   ;
   ;  Defines shape of player0 sprite.
   ;
   player0:
   %00111100
   %01111110
   %11000011
   %10111101
   %11111111
   %11011011
   %01111110
   %00111100
end


   ;***************************************************************
   ;
   ;  Sets starting position of player0.
   ;
   player0x = 77 : player0y = 53



__Main_Loop


   ;***************************************************************
   ;
   ;  Sets color of player0 sprite.
   ;
   COLUP0 = $9C


   ;***************************************************************
   ;
   ;  Skips up/down movement if player is moving left or right.
   ;
   if _Bit6_JoyL{6} || _Bit7_JoyR{7}  then goto __Skip_Joy0Down


   ;***************************************************************
   ;
   ;  Up movement check.
   ;
   if !joy0up then _Bit4_JoyU{4} = 0 : goto __Skip_Joy0Up

   if player0y > _P_Edge_Top then player0y = player0y - 1 : _Bit4_JoyU{4} = 1

__Skip_Joy0Up


   ;***************************************************************
   ;
   ;  Down movement check.
   ;
   if !joy0down then _Bit5_JoyD{5} = 0 : goto __Skip_Joy0Down

   if player0y < _P_Edge_Bottom then player0y = player0y + 1 : _Bit5_JoyD{5} = 1

__Skip_Joy0Down


   ;***************************************************************
   ;
   ;  Skips left/right movement if player is moving up or down.
   ;
   if _Bit4_JoyU{4} || _Bit5_JoyD{5} then goto __Skip_Joy0Right


   ;***************************************************************
   ;
   ;  Left movement check.
   ;
   if !joy0left then _Bit6_JoyL{6} = 0 : goto __Skip_Joy0Left

   if player0x > _P_Edge_Left then player0x = player0x - 1 : _Bit6_JoyL{6} = 1

__Skip_Joy0Left


   ;***************************************************************
   ;
   ;  Right movement check.
   ;
   if !joy0right then _Bit7_JoyR{7} = 0 : goto __Skip_Joy0Right

   if player0x < _P_Edge_Right then player0x = player0x + 1 : _Bit7_JoyR{7} = 1

__Skip_Joy0Right


   ;***************************************************************
   ;
   ;  Displays the screen.
   ;
   drawscreen


   goto __Main_Loop


no_diagonal_movement.bas

no_diagonal_movement.bin
  • Like 1

Share this post


Link to post
Share on other sites

A number of ways. Dunno what bB's equates for player0x/player0y are, but a routine that favors left and right when diagonals are used goes like this:

 

 

 
   ldx player0x
   ldy player0y
   lda SWCHA
   asl
   bcs NotRight
   inx
   cpx #154
   beq DontSave
   bne Update
 
NotRight:
   asl
   bcs NotLeft
   dex
   txa
   beq DontSave
   bne Update
 
NotLeft:
   asl
   bcs NotDown
   iny
   cpy #89
   beq DontSave
   bne Update
 
NotDown:
   bmi DontSave
   dey
   cpy #8
   beq DontSave
Update
   stx player0x
   sty player0y
DontSave:

 

If you don't want -any- diagonals or button combos to be recognized at all, just use a table that throws away invalid directions:

 

 

 
   lda SWCHA
   lsr
   lsr
   lsr
   lsr
   tax
   lda Directions,x
   beq DontMove
   cpx #12
   bcc UpdateX
   clc
   adc player0y
   cmp #89
   beq DontMove
   cmp #8
   beq DontMove
   sta player0y
   bne End
 
UpdateX
   adc player0x
   beq DontMove
   cmp #154
   beq DontMove
   sta player0x
DontMove:
End:
...
;somewhere in rom tables:
Directions:
   .byte 0,0,0,0,0,0,0,1
   .byte 0,0,0,-1,0,1,-1,0

 

Share this post


Link to post
Share on other sites
Quote

 p0dir=0

 if joy0up then p0dir=1
 if joy0down then p0dir = 2
 if joy0left then p0dir=3
 if joy0right then p0dir = 4
 if p0dir = 1 then player0y=player0y-1
 if p0dir = 2 then player0y=player0y+1
 if p0dir = 3 then player0x = player0x-1
 if p0dir = 4 then player0x = player0x+1

Just make sure you dim p0dir to a variable first, something like dim p0dir = a

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...