Help with movement bug

Blitz3D Forums/Blitz3D Beginners Area/Help with movement bug

Rooster(Posted 2015) [#1]
I been having a problem with my code were when I try to make the floor move to simulate movement the floor just keeps going even when I am no longer pressing the key.

I think it may have something to do with the type I am using to hold the coordinates for the floor pieces, but I am not sure if that is it.

Well is most of the code.


;Main Progam
While Not KeyDown (KEY_ESCAPE)

 ;Check_input ()

 Updat_floors ()

 Cls

 Draw_all ()

 
 Flip 

Wend     



End   


;Type and Functions

;Types

;This type holds floor stats
Type FloorT

 Field x
 Field y

End Type 

;This type holds the players stats
Type PlayerT

 Field X 
 Field Y

End Type


;Functions 

;Checks player input
Function Check_input ()

 If KeyDown (KEY_LEFT) Then 
  player\x =player\x -5
 EndIf 

 If KeyDown (KEY_RIGHT) Then 
  player\x =player\x +5
 EndIf

 If KeyDown (KEY_UP) Then 
  player\Y =player\y +5 
 EndIf

 If KeyDown (KEY_DOWN) Then 
  player\Y =player\y -5 
 EndIf


End Function 


Function Check_collision (checkx1,checky1,checkx2,checky2,width1,height1,width2,height2 )


 If checkx1 = width2 >= checkx2 - width1 And checkx1 - width2 <= checkx1 + width1 Then 
  temp = 1
  EndIf

 If checky1 = height2 >= checky2 - height1 And checky1 - height2 <= checky1 + height1 Then 
  temp = 1
  EndIf


 Return temp 


End Function 


;Draws averything
Function Draw_all ()

 Draw_floor ()

 Draw_player ()


End Function 

;Draws the floor
Function Draw_floor ()

 For floors.FloorT = Each FloorT
  
  DrawImage FloorImg,floors\x,floors\y

 Next 

End Function 

;Draws the player on the screen
Function Draw_player ()

 DrawImage PlayerImg, 320,240

End Function       

;Generates the floor
Function Generate_floor ()   

 For temp = 0 To 10

  floors.FloorT = New FloorT   

  floors\x = floorgen (0,temp) 
  floors\y = floorgen (1,temp) 


 Next 

End Function  


;this function updats the floor pois to to creat the players movement 
Function Updat_floors ()

;transfers the number from the player type temp variables
 

If KeyDown (KEY_LEFT) Then 
  player\x =player\x -5
 EndIf 

 If KeyDown (KEY_RIGHT) Then 
  player\x =player\x +5
 EndIf

 If KeyDown (KEY_UP) Then 
  player\Y =player\y +5 
 EndIf

 If KeyDown (KEY_DOWN) Then 
  player\Y =player\y -5 
 EndIf



 tempx =0
 tempy =0

 tempx = player\x ;remove?
 tempy = player\y

;updats the floors type
 For floors.FloorT = Each FloorT
  
  floors\x = floors\x + tempx
  floors\y = floors\y + tempy

 Next 

End Function  



If you need to see more of the code just let me know.


Thanks for looking at this, and I hope I was clear. :3


Matty(Posted 2015) [#2]
What is happening is this:

You set the value of player x or y when a key is pressed. (In fact you add to it)

Upon entering the function again the values of player x or y retain their non zero value.

You then add this non zero value to the floors x and y each and every frame.


Rooster(Posted 2015) [#3]
Well I made this change and it still dose it.

Function Check_input ()

 player\x = 0
 player\y = 0

 If KeyDown (KEY_LEFT) Then 
  player\x =player\x -5
 EndIf 

 If KeyDown (KEY_RIGHT) Then 
  player\x =player\x +5
 EndIf

 If KeyDown (KEY_UP) Then 
  player\Y =player\y +5 
 EndIf

 If KeyDown (KEY_DOWN) Then 
  player\Y =player\y -5 
 EndIf


I don't know what I did wrong, because what you said made sense. So what would you do?


Matty(Posted 2015) [#4]
If that is all you changed it still won't work because Check_input() is commented out in your original code.


Rooster(Posted 2015) [#5]
LOL! (I can't believe that I X3)

Thank you very much for the help Matty. :D


RemiD(Posted 2015) [#6]
That's why a new view is sometimes necessary, i had a similar problem a few months ago.
Thanks Matty.