Gravity For Sprites
Author |
Message |
drummaboy
|
Posted: Sat Dec 05, 2009 7:16 pm Post subject: Gravity For Sprites |
|
|
What is it you are trying to achieve?
Make my sprite have gravity without it disappearing
What is the problem you are having?
Whenever I try to make my sprite have gravity , it dissapears
Describe what you have tried to solve this problem
I've tried everything. But it won't work.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
process DoMusic
loop
Music.PlayFile ("mario.mp3")
end loop
end DoMusic
fork DoMusic
% var statements and constants
var posx, posy : int
var velx, vely : real
const groundheight := 5
var jumpspeed : int
jumpspeed := 60
posx := 21
posy := 5
velx := 0
vely := 0
const runspeed := 1
const gravity := 1
var mario, marior, mariosl, mariol, mariojl, mariojr : int % mariostandleft and mario left
var spr, spr1, spr2, spr3, spr4, spr5, spr6, spr7 : int
var platform, tunnel : int
var infinite : int
infinite := 1000000
% End of var statments and constants ... Drawing Scenery
drawfillbox (0, 0, maxx, maxy, 52)
drawfillbox (0, 0, maxx, 21, brightgreen)
%cloud 1
drawfilloval (0, 350, 10, 10, white)
drawfilloval (15, 350, 10, 10, white)
drawfilloval (30, 350, 10, 10, white)
%cloud 2
drawfilloval (100, 350, 10, 10, white)
drawfilloval (115, 350, 10, 10, white)
drawfilloval (130, 350, 10, 10, white)
%cloud 3
drawfilloval (200, 350, 10, 10, white)
drawfilloval (215, 350, 10, 10, white)
drawfilloval (230, 350, 10, 10, white)
%sun
drawfillarc (maxx - 20, maxy - 25, 25, 50, 50, 410, yellow)
% Draw platform
tunnel := Pic.FileNew ("tunnel.bmp")
spr7 := Sprite.New (tunnel )
Sprite.SetPosition (spr7, 550, - 5, false)
Sprite.SetHeight (spr7, 10)
Sprite.Show (spr7 )
platform := Pic.FileNew ("platform.bmp")
spr2 := Sprite.New (platform )
Sprite.SetPosition (spr2, 300, 45, true)
Sprite.SetHeight (spr2, 10)
Sprite.Show (spr2 )
%End of Drawing Scenery and Start of Drawing Mario
mario := Pic.FileNew ("mariostand.bmp")
spr := Sprite.New (mario )
marior := Pic.FileNew ("marioright.bmp")
spr1 := Sprite.New (marior )
mariosl := Pic.FileNew ("mariostandL.bmp")
spr3 := Sprite.New (mariosl )
mariol := Pic.FileNew ("marioleft1.bmp")
spr4 := Sprite.New (mariol )
mariojl := Pic.FileNew ("jumpleft.bmp")
spr5 := Sprite.New (mariojl )
mariojr := Pic.FileNew ("jumpright.bmp")
spr6 := Sprite.New (mariojr )
var chars : array char of boolean
loop
Input.KeyDown (chars )
if chars (KEY_RIGHT_ARROW) then
Sprite.ChangePic (spr, marior )
delay (60)
Sprite.ChangePic (spr, mario )
delay (60)
Sprite.ChangePic (spr,marior )
Sprite.ChangePic (spr,mario )
posx + = 5
elsif chars (KEY_LEFT_ARROW) then
Sprite.ChangePic (spr, mariol )
delay (50)
Sprite.ChangePic (spr, mariosl )
delay (50)
Sprite.ChangePic (spr,mariol )
Sprite.ChangePic (spr,mariosl )
posx - = 5
elsif chars ('z') then
Sprite.ChangePic (spr, mariojl )
posy + = jumpspeed
end if
% this is where vely and velx contradict with the position
vely - = gravity
posx + = round (velx )
Sprite.SetPosition (spr, posx, posy, false)
Sprite.SetHeight (spr, 10)
Sprite.Show (spr )
if posx > 545 then
posx := 545
elsif posy > 10 then
posy := 10
elsif posy > groundheight then
Sprite.ChangePic (spr,mario )
posy := groundheight
end if
end loop
|
Please specify what version of Turing you are using
4.1.1 (which i think is 4.1)
But can someone please tell me how to attach folders?? so I can send you the pictures so you can play the game and find out whats wrong. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
|
|
|
 |
mirhagk
|
Posted: Sat Dec 05, 2009 8:26 pm Post subject: Re: Gravity For Sprites |
|
|
first off to upload a folder you put the folder into a .zip or a .rar file. It makes the folder a single file so it can be uploaded to the internet
now lets look at your code:
and
Turing: |
% this is where vely and velx contradict with the position
vely - = gravity
posx + = round (velx )
Sprite.SetPosition (spr, posx, posy, false)
Sprite.SetHeight (spr, 10)
Sprite.Show (spr )
if posx > 545 then
posx := 545
elsif posy > 10 then
posy := 10
elsif posy > groundheight then
Sprite.ChangePic (spr,mario )
posy := groundheight
end if
end loop
|
first off the whole point of the y velocity is to change the y position right? Your code doesn't have any spot to change the position from the velocity. So add this (after the subtraction of gravity)
now the jump should not change the posy, but rather the vely right, so that they have an initial burst of speed, then as the velocity decreases by gravity the sprite will fall back down
so change the posy+=jumpspeed to vely+=jumpspeed
now the last section the
Turing: |
elsif posy > groundheight then
Sprite.ChangePic (spr,mario )
posy := groundheight
end if
|
shouldn't it be less than groundheight, not greater than (otherwise he can't jump and will just keep falling and DISAPEAR!!!!)
also have it make the vely 0 so that you don't have vely become like 999999999 or something.
so change that piece of code to
Turing: |
elsif posy < groundheight then
Sprite.ChangePic (spr,mario )
posy := groundheight
vely := 0
end if
|
|
|
|
|
|
 |
B-Man 31

|
Posted: Sat Dec 05, 2009 10:22 pm Post subject: RE:Gravity For Sprites |
|
|
wow, mirhagk. looks like you covered everything. |
|
|
|
|
 |
drummaboy
|
Posted: Sun Dec 06, 2009 11:13 am Post subject: Re: Gravity For Sprites |
|
|
Thanks mirhagk. Everthing works until I changed posy += jumpspeed to vely += jumpspeed.
Whenever I jump, my sprite just dissapears and never appears again. but when it's posy += jumpspeed the sprite doesn't dissapear
but my sprite jumps way to fast and comes down way to fast.
Could you help me fix it? |
|
|
|
|
 |
B-Man 31

|
Posted: Sun Dec 06, 2009 11:54 am Post subject: RE:Gravity For Sprites |
|
|
change jumpspeed, make it lower. |
|
|
|
|
 |
drummaboy
|
Posted: Sun Dec 06, 2009 12:24 pm Post subject: Re: Gravity For Sprites |
|
|
B-Man 31 thanks for the reply but sadly that didn't work. |
|
|
|
|
 |
B-Man 31

|
Posted: Sun Dec 06, 2009 4:33 pm Post subject: RE:Gravity For Sprites |
|
|
yeah, i just realized that, lol. could you please post an updated version so we can see the code please. thanks |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
drummaboy
|
Posted: Sun Dec 06, 2009 5:29 pm Post subject: Re: Gravity For Sprites |
|
|
OK i really don't know what happened but it doesn't dissapear anymore. It's jus that my sprite doesn't gradually go down.
It just goes up and appears on the ground again.
and how can I make it do tht I can jump while press tha arrow keys 2.
Cause my sprite won't jump unless I stop moving first.'
process DoMusic
loop
Music.PlayFile ("mario.mp3")
end loop
end DoMusic
fork DoMusic
% var statements and constants
var posx, posy : int
var velx, vely : real
const groundheight := 5
var jumpspeed : int
jumpspeed := 50
posx := 21
posy := 5
velx := 0
vely := 0
const runspeed := 1
var gravity :int := 1
var mario, marior, mariosl, mariol, mariojl, mariojr : int % mariostandleft and mario left
var spr, spr1, spr2, spr3, spr4, spr5, spr6, spr7 : int
var platform, tunnel : int
var infinite : int
infinite := 1000000
% End of var statments and constants ... Drawing Scenery
drawfillbox (0, 0, maxx, maxy, 52)
drawfillbox (0, 0, maxx, 21, brightgreen)
%cloud 1
drawfilloval (0, 350, 10, 10, white)
drawfilloval (15, 350, 10, 10, white)
drawfilloval (30, 350, 10, 10, white)
%cloud 2
drawfilloval (100, 350, 10, 10, white)
drawfilloval (115, 350, 10, 10, white)
drawfilloval (130, 350, 10, 10, white)
%cloud 3
drawfilloval (200, 350, 10, 10, white)
drawfilloval (215, 350, 10, 10, white)
drawfilloval (230, 350, 10, 10, white)
%sun
drawfillarc (maxx - 20, maxy - 25, 25, 50, 50, 410, yellow)
% Draw platform
tunnel := Pic.FileNew ("tunnel.bmp")
spr7 := Sprite.New (tunnel)
Sprite.SetPosition (spr7, 550, -5, false)
Sprite.SetHeight (spr7, 10)
Sprite.Show (spr7)
platform := Pic.FileNew ("platform.bmp")
spr2 := Sprite.New (platform)
Sprite.SetPosition (spr2, 300, 45, true)
Sprite.SetHeight (spr2, 10)
Sprite.Show (spr2)
%End of Drawing Scenery and Start of Drawing Mario
mario := Pic.FileNew ("mariostand.bmp")
spr := Sprite.New (mario)
marior := Pic.FileNew ("marioright.bmp")
spr1 := Sprite.New (marior)
mariosl := Pic.FileNew ("mariostandL.bmp")
spr3 := Sprite.New (mariosl)
mariol := Pic.FileNew ("marioleft1.bmp")
spr4 := Sprite.New (mariol)
mariojl := Pic.FileNew ("jumpleft.bmp")
spr5 := Sprite.New (mariojl)
mariojr := Pic.FileNew ("jumpright.bmp")
spr6 := Sprite.New (mariojr)
var chars : array char of boolean
loop
Input.KeyDown (chars)
if chars (KEY_RIGHT_ARROW) then
Sprite.ChangePic (spr, marior)
delay (60)
Sprite.ChangePic (spr, mario)
delay (60)
Sprite.ChangePic (spr,marior)
Sprite.ChangePic (spr,mario)
posx += 5
elsif chars (KEY_LEFT_ARROW) then
Sprite.ChangePic (spr, mariol)
delay (50)
Sprite.ChangePic (spr, mariosl)
delay (50)
Sprite.ChangePic (spr,mariol)
Sprite.ChangePic (spr,mariosl)
posx -= 5
elsif chars ('z') then
Sprite.ChangePic (spr, mariojl)
delay (50)
Sprite.ChangePic (spr, mariojl)
vely := jumpspeed
end if
% this is where vely and velx contradict with the position
vely -= gravity
posx += round (velx)
posy += round (vely)
Sprite.SetPosition (spr, posx, posy, false)
Sprite.SetHeight (spr, 10)
Sprite.Show (spr)
if posx > 545 then
posx := 545
elsif posy > 65 and posx > 100 then
posy := 65
gravity := 0
elsif posy = 65 and posx >300 then
gravity := 1
elsif posy < groundheight then
Sprite.ChangePic (spr,mario)
posy := groundheight
vely := 0
end if
end loop |
|
|
|
|
 |
|
|