UnderwareDESIGN

PlayBASIC => Beginners => Topic started by: Mick Berg on November 04, 2005, 10:09:53 PM

Title: Using psubs and For-next loops
Post by: Mick Berg on November 04, 2005, 10:09:53 PM
I need a bit of help with two things; First, anytime I use a subroutine or loop in my program, I have to refresh the screen and redraw the sprites within the sub or loop or else the action will not be seen. Is that acceptable, or is there a technique that I am missing here? I had hoped to refresh everything just once at the end of the program loop.
Second, how do I move a sprite in a parabolic curve, to represent a jump? I know you use the SIN command but can't work out how, if anyone would be so kind as to show me.
Thanks,

Mick Berg.
Title: Using psubs and For-next loops
Post by: Draco9898 on November 04, 2005, 11:44:09 PM
Hello Mick, it's pretty hard without some source code so...
What I'm assuming is your drawing problem is all varibles are NOT global. Hence if you have sprite index varibles like, (ThisSprite = GetfreeSprite(), Sprite ThisSprite,etc,etc) and you try to relay this information into a function indirectly, this won't work. You could use global varibles like Mini-Arrays like this: ThisSprite(0) = My Sprite because the function will be able to see it, even if you don't pass it in.

2nd:
To make your sprite move in a jumping fashion, use varibles that represent how real physics work, with things such as velocity, acceleration, and gravity. Using Sin() won't work because it's a fixed parabolic math function and isn't dynamic enough.



Here's something to chew on:

[pbcode]
SetFPS 60

`before the loop define all data stuff
Global WorldGravity#=0.22

Type MyPlayer
  XVelo#, YVelo#
 X#, Y#
EndType
Dim Player As MyPlayer
Player.X#=20: Player.Y#=100

Do
   Cls 0

   Handle_Player()

Sync
Loop

Psub Handle_Player()
   `--Do Physics
  `Gravity
   Player.YVelo#=Player.YVelo#+WorldGravity#
  `Adjust positions by velocity
   Player.X#=Player.X#+Player.XVelo#
  Player.Y#=Player.Y#+Player.YVelo#
   `Physics 'collision'
  If Player.Y#>450
 Player.Y#=450
     Player.YVelo#=Player.YVelo#*-1
 Player.XVelo#=1.2
  EndIf
    If Player.X#>800
 Player.X#=0
    EndIf
  `Draw Player
 Circle Player.X#,Player.y#,10,1
EndPsub
[/pbcode]

Title: Using psubs and For-next loops
Post by: Mick Berg on November 06, 2005, 12:48:03 AM
Thanks so much, this is very helpful, both to me and, I'm sure, a lot of fellow newbies. I'll work on it and let you know how I get on.