T
Treecase86
Guest
Hey, back for the second thread of the day. I'm making a Sonic engine. So, among other things, the vertical movement won't work. The jumping stops after a few frames, the gravity won't even apply, etc. Please check over my code to see what's wrong. I know that it'll be a long list. Please note I haven't fully implemented many things(i.e slopes, loops, the like).
Code:
///Variables
grounded = false
xpos = 14 //Sonic's origin(x)
ypos = 19 //Sonic's origin(y)
xsp = 0 //Sonic's horizontal speed
ysp = 0 //Sonic's vertical speed
gsp = 0 //Sonic's ground speed (Important with angles)
slope = 0 //the current slope that Sonic's on
angle = 0 //current angle(ground)
//Constants
acc = 0.046875 //acceleration
dec = 0.5 //deceleration
fric = 0.046875 //friction(same as acc)
top = 6 //top speed
air = 0.09375 //air (x) acceleration
jmp = -6.5 //vertical acceleration
topJmp = 6 //top xsp in air
grav = 0.21875 //gravity
slp = 0.125 //slope?
slprollup = 0.078125 //acc when rolling up slope
slprolldown = 0.3125 //acc going down
fall = 2.5 //???
Code:
///Step event\\\
///Movement
//directions
right = keyboard_check(vk_right) or keyboard_check(ord('D'))
left = keyboard_check(vk_left ) or keyboard_check(ord('A'))
space = keyboard_check_pressed(vk_space)
//acceleration
if abs(gsp) < top
{
if (right)
{
gsp += acc
image_xscale = 1
}
else if (left)
{
gsp -= acc
image_xscale = -1
}
}
//deceleration
if right or left
{
// if the opposite directional button from where
// we are moving is pressed, then we decelerate
if (gsp > 0 and left)
{
gsp -= dec
}
if (gsp < 0 and right)
{
gsp += dec
}
}
// decelerate if no buttons are pressed
else
{
gsp -= fric * sign(gsp)
if abs(gsp) < fric
{
gsp = 0
}
}
//ground speed
/*xsp = gsp * cos(ang)
ysp = gsp *- sin(ang)
*/
///Collision
//checking for floor
///Jumping
var grounded;
grounded = tile_layer_find(1,x,y)
if grounded == false
{
//if off of ground, apply gravity
ysp += grav
}
else
{
//otherwise, ysp = 0
ysp = 0
}
if (space and grounded)
{
//if jumping, you've jumped
grounded = false
ysp += jmp
}
//application
xsp = gsp
xpos += xsp
ypos += ysp
x += xsp
y += ysp
///Animations
if gsp == 0
{
//if no gsp, sprite = standing
sprite_index = sprSonic
}
//Running
else if gsp < top
{
sprite_index = sprSonWalk
image_speed = gsp
}
if gsp >= top
{
sprite_index = sprSonRun
image_speed = gsp
}
if ysp != 0
{
sprite_index = sprSonJump
}