this is my code:
using System;using SFML.Audio;using SFML.Graphics;using SFML.System;using SFML.Window;using System.Collections.Generic;namespace salto_grnd_pltfrm
{ class Program
{ public static RenderWindow rw
; public static Texture txtr_bgrnd
; public static Texture txtr_grnd
; public static Texture txtr_wall
; public static Texture txtr_plyr
; public static Texture txtr_coin
; public static Texture txtr_fire
; public static Texture txtr_robot
; public static Sprite sprt_bgrnd
; public static Sprite sprt_grnd
; public static Sprite sprt_wall
; public static Sprite sprt_plyr
; public static Sprite sprt_coin
; public static Sprite sprt_robot
; public static Platform
[] arr_pltfrm
= new Platform
[3]; public static bool is_r
=false,is_l
=false,is_u,is_d,is_spc,is_grnd,is_pltfrm,is_jump
; public static float velx
=0
.0f,vely
=0
.0f
; public static float jumpheight
= -300
.0f, gravity
=-400
.0f
; public static float piy,piy2
; public static float piy_d
; public static bool canJump
; public static int acum
=0; public static Clock tmpcl
; public static float vida
=0
.0f
; public static bool isVida
=false; public static bool isUpdate
=true; public static bool mouseOverBtn
=false, isClick
=false, isReleased
=false; public static Clock cl,clck_shoot
; public static Time elapsedTime2
; public static bool idle_der
=true,idle_izq
=false,is_ctrl
; public static bool tmp_area
=false,isBack
=false; public static Texture t1,t2,t3,t4,t5,t6
; public static string r1
=@"E:\sprites\player_idle_r.png"; public static string r2
=@"E:\sprites\player_anim_r.png"; public static string r3
=@"E:\sprites\player_idle_l.png"; public static string r4
=@"E:\sprites\player_anim_l.png"; public static string r5
=@"E:\sprites\enemy_anim_r.png"; public static string r6
=@"E:\sprites\enemy_anim_l.png"; public static string r7
=""; public static string r8
=""; public static Player player
; public static List
<EnemySpawn
> arr_misil
= new List
<EnemySpawn
>(10); public static void Main
(string[] args
) { rw
= new RenderWindow
(new VideoMode
(700,
500),
"tipos de salto..."); rw
.SetActive(true); bool estado
=rw
.IsOpen(); rw
.SetKeyRepeatEnabled(false); rw
.SetFramerateLimit(60); txtr_bgrnd
= new Texture
(@"C:\sprites\bckgrnd.jpg"); sprt_bgrnd
= new Sprite
(txtr_bgrnd
); sprt_bgrnd
.Position = new SFML
.Window.Vector2f(0
.0f,0
.0f
); txtr_grnd
= new Texture
(@"C:\sprites\ground.jpg"); sprt_grnd
= new Sprite
(txtr_grnd
); sprt_grnd
.Position = new SFML
.Window.Vector2f(200
.0f,400
.0f
); txtr_wall
= new Texture
(@"C:\sprites\pltfrm_0_rect_slm.png"); sprt_wall
= new Sprite
(txtr_wall
); sprt_wall
.Position = new SFML
.Window.Vector2f(300
.0f,300
.0f
); txtr_plyr
= new Texture
(@"C:\sprites\Sprite-0002.png"); sprt_plyr
= new Sprite
(txtr_plyr
); sprt_plyr
.Position = new SFML
.Window.Vector2f(190,
400-sprt_plyr
.TextureRect.Height); txtr_coin
= new Texture
(@"C:\sprites\coin_3030.png"); sprt_coin
= new Sprite
(txtr_coin
); sprt_coin
.Position = new SFML
.Window.Vector2f(sprt_wall
.Position.X+(sprt_wall
.TextureRect.Width/2),sprt_wall
.Position.Y-sprt_coin
.TextureRect.Height); txtr_fire
= new Texture
(@"C:\sprites\fire_enemy.png"); txtr_robot
= new Texture
(@"C:\sprites\monster.png"); sprt_robot
= new Sprite
(txtr_robot
); sprt_robot
.Position = new SFML
.Window.Vector2f(530
.0f,sprt_grnd
.Position.Y-sprt_robot
.TextureRect.Height); piy_d
=300-sprt_plyr
.TextureRect.Height; Clock clck
= new Clock
(); Time timeSinceLastUpdate
= Time
.Zero; Time timePerFrame
= Time
.FromSeconds(1
.0f
/60
.0f
); tmpcl
= new Clock
(); clck_shoot
= new Clock
(); elapsedTime2
=new Time
(); cl
=new Clock
(); rw
.KeyPressed+= OnKeyPressed
; rw
.KeyReleased+= OnKeyReleased
; t1
= new Texture
(r1
); t2
= new Texture
(r2
); t3
= new Texture
(r3
); t4
= new Texture
(r4
); t5
= new Texture
(r5
); t6
= new Texture
(r6
); player
= new Player
(); player
.setTexture2(t1
); piy
=400-player
.TextureRect.Height; piy2
=sprt_grnd
.TextureRect.Height+player
.TextureRect.Height; player
.setPosition(300f,sprt_grnd
.Position.Y-player
.TextureRect.Height); createPlatform
(); while(estado
==true) { Time elapsedTime
= clck
.Restart(); timeSinceLastUpdate
+= elapsedTime
; while(timeSinceLastUpdate
>timePerFrame
) { timeSinceLastUpdate
-=timePerFrame
; rw
.DispatchEvents(); if(isUpdate
==true || isReleased
==true) update3
(timePerFrame
); } render
(); rw
.SetTitle(" plyrY: "+player
.Position.Y+" pPosy: "+player
.posy+" pltfrmd "+is_pltfrm
+" canjump "+canJump
+" grndY "+sprt_grnd
.Position.Y); } } public static void update3
(Time deltaTime
) { if(is_r
) { player
.moveR(deltaTime,1f
); idle_der
=false; idle_izq
=false; } if(is_l
) { player
.moveL(deltaTime,1f
); idle_der
=false; idle_izq
=false; } player
.setPosition(player
.Position.X+player
.posx,player
.Position.Y+player
.posy); ManageJump
(deltaTime
); } public static void render
() { rw
.Clear(Color
.White); rw
.Draw(sprt_bgrnd
); rw
.Draw(sprt_grnd
); rw
.Draw(sprt_wall
); if(sprt_coin
!=null) rw
.Draw(sprt_coin
); if(is_r
==true && is_l
==false){ player
.Texture = t2
; } if(is_l
==true && is_r
==false){ player
.Texture = t4
; } if(idle_der
==true){ player
.Texture = t1
; } if(idle_izq
==true){ player
.Texture = t3
; } rw
.Draw(player
); loadPlatform
(); rw
.Display(); } static void createPlatform
(){ Random r
= new Random
(); Platform p
= new Platform
(); for(int i
=0;i
<arr_pltfrm
.Length;i
++){ arr_pltfrm
[i
] = new Platform
(); arr_pltfrm
[i
].setTexture(txtr_wall
); } arr_pltfrm
[0].Position = new SFML
.Window.Vector2f(470f,280f
); arr_pltfrm
[1].Position = new SFML
.Window.Vector2f(400f,175f
); arr_pltfrm
[2].Position = new SFML
.Window.Vector2f(500f,75f
); } static void loadPlatform
(){ for(int i
=0;i
<arr_pltfrm
.Length;i
++) { rw
.Draw(arr_pltfrm
[i
]); } } static void OnKeyPressed
(object sender, KeyEventArgs e
) { if(e
.Code == Keyboard
.Key.Right) { is_r
=true; is_l
=false; idle_der
=false; idle_izq
=false; } if(e
.Code == Keyboard
.Key.Left) { is_l
=true; is_r
=false; idle_izq
=false; idle_der
=false; } if(e
.Code == Keyboard
.Key.Up) { is_u
=true; } if(e
.Code == Keyboard
.Key.Space) { is_spc
=true; } if(e
.Code == Keyboard
.Key.Escape) { Environment
.Exit(0); } if(e
.Code == Keyboard
.Key.LControl){ is_ctrl
=true; } } static void OnKeyReleased
(object sender, KeyEventArgs e
) { if(e
.Code == Keyboard
.Key.Right) { is_r
=false; player
.posx=0
.0f
; idle_der
=true; idle_izq
=false; } if(e
.Code == Keyboard
.Key.Left) { is_l
=false; player
.posx=0
.0f
; idle_izq
=true; idle_der
=false; } if(e
.Code == Keyboard
.Key.Up) { is_u
=false; } if(e
.Code == Keyboard
.Key.Space) { is_spc
=false; } if(e
.Code == Keyboard
.Key.LControl){ is_ctrl
=false; } } public static bool grounded
() { if(player
.Position.Y+player
.TextureRect.Height>=sprt_grnd
.Position.Y && player
.Position.X+player
.TextureRect.Width>=sprt_grnd
.Position.X) { player
.Position=new SFML
.Window.Vector2f(player
.Position.X,piy
); player
.posy=0
.0f
; is_grnd
=true; is_pltfrm
=false; } else { is_grnd
=false; } return is_grnd
; } public static bool pltfrmd
() { for(int i
=0;i
<arr_pltfrm
.Length;i
++){ if((player
.Position.Y+player
.TextureRect.Height>arr_pltfrm
[i
].Position.Y && player
.Position.Y+player
.TextureRect.Height<arr_pltfrm
[i
].Position.Y+arr_pltfrm
[i
].TextureRect.Height ) && (player
.Position.X+player
.TextureRect.Width>arr_pltfrm
[i
].Position.X && player
.Position.X<arr_pltfrm
[i
].Position.X+arr_pltfrm
[i
].TextureRect.Width)) { player
.Position = new SFML
.Window.Vector2f(player
.Position.X,arr_pltfrm
[i
].Position.Y-player
.TextureRect.Height); player
.posy=0
.0f
; is_pltfrm
=true; is_grnd
=false; } } return is_pltfrm
; } static void ManageJump
(Time deltaTime
){ if(grounded
()==true || pltfrmd
()==true){ canJump
=true; } for(int i
=0;i
<arr_pltfrm
.Length;i
++){ if((is_u
==true && canJump
&& player
.Position.Y > sprt_grnd
.Position.Y-150f
) || (is_u
==true && canJump
&& player
.Position.Y > arr_pltfrm
[i
].Position.Y-150f
)){ player
.posy-=30
.0f
*deltaTime
.AsSeconds(); is_pltfrm
=false; } else{ canJump
=false; if((player
.Position.Y <= sprt_grnd
.Position.Y-150f
) || (player
.Position.Y <= arr_pltfrm
[i
].Position.Y-150f
)){ player
.posy++; } player
.posy++; } } } }} When the character jumps from ground it does higher than if jumps from platform. My approach is a constant jump height, but I'm stuck :/
EDIT: I change this:
player.Position.Y > arr_pltfrm
.Position.Y-150f
to this:
player.Position.Y > arr_pltfrm.Position.Y-210f
This should work and the player when jumps while i'ts on platform should jump higher than if it's on ground, but the result is the same.
for(int i=0;i<arr_pltfrm.Length;i++){
if((is_u==true && canJump && player.Position.Y > sprt_grnd.Position.Y-150f) || (is_u==true && canJump && player.Position.Y > arr_pltfrm[i].Position.Y-210f)){
player.posy-=30.0f*deltaTime.AsSeconds();
is_pltfrm=false;
}
else{
canJump=false;
if((player.Position.Y <= sprt_grnd.Position.Y-150f) || (player.Position.Y <= arr_pltfrm[i].Position.Y-210f)){
player.posy++;
}
player.posy++;
}
}
}
Here's a video that can provide more info:
https://vimeo.com/630581663*Finally I solved that adding a global variable and set different values in grounded() and pltfrmd() methods:
public static float jumpheight
;public static bool grounded
() { if(player
.Position.Y+player
.TextureRect.Height>=sprt_grnd
.Position.Y && player
.Position.X+player
.TextureRect.Width>=sprt_grnd
.Position.X) { player
.Position=new SFML
.Window.Vector2f(player
.Position.X,piy
); player
.posy=0
.0f
; is_grnd
=true; is_pltfrm
=false; jumpheight
=-17f
; } else { is_grnd
=false; } return is_grnd
; }public static bool pltfrmd
() { for(int i
=0;i
<arr_pltfrm
.Length;i
++){ if((player
.Position.Y+player
.TextureRect.Height>arr_pltfrm
[i
].Position.Y && player
.Position.Y+player
.TextureRect.Height<arr_pltfrm
[i
].Position.Y+arr_pltfrm
[i
].TextureRect.Height ) && (player
.Position.X+player
.TextureRect.Width>arr_pltfrm
[i
].Position.X && player
.Position.X<arr_pltfrm
[i
].Position.X+arr_pltfrm
[i
].TextureRect.Width)) { player
.Position = new SFML
.Window.Vector2f(player
.Position.X,arr_pltfrm
[i
].Position.Y-player
.TextureRect.Height); player
.posy=0
.0f
; is_pltfrm
=true; is_grnd
=false; jumpheight
=-20f
; } } return is_pltfrm
; }static void ManageJump
(Time deltaTime
){ if(grounded
()==true || pltfrmd
()==true){ canJump
=true; } for(int i
=0;i
<arr_pltfrm
.Length;i
++){ if((is_u
==true && canJump
&& player
.posy > jumpheight
)){ player
.posy-=30
.0f
*deltaTime
.AsSeconds(); is_pltfrm
=false; } else{ canJump
=false; if((player
.posy <= jumpheight
)){ player
.posy++; } player
.posy++; } } } Please, mark this question as solved