sonic101 Posted June 9, 2006 Report Share Posted June 9, 2006 Okay this is the code I'm using for acceleration with my game //in the players right arrow key press event for (i=0; i<= 20; i +=1 ) { facing='right' air=false; // set the speed to be running at global.speed_count = min(global.speed_count+0.6,20) //move if place_free(x+global.speed_count,y-i) { x+=global.speed_count; y-= i; exit; } if image_speed < 1 {image_speed += 0.1} The bolded part is what sets the acceleration. My question is how would I do deacceleration when the key is released but no other keys are pressed, but if you should be doing full speed and hit the left arrow key do a skid stop? It honestly took m a week to figure out acceleration so maybe it's best to get soem help with deacceleration. aagian i used GM vers 6.1 registered. Link to comment Share on other sites More sharing options...
Kain Posted June 9, 2006 Report Share Posted June 9, 2006 Since there's no event for "not pressing left or right, but maybe pressing something", you might as well make your own. Probably the best way to do it is in a step event and call the key checking buttons manually inside if... else if... else statements. (Or even better, set variables like pRight, pLeft, etc to a key check in the begining step and use those variables for controls. That way dellayed keypress following and key-based recording/playback are pretty simple): if( keyboard_check(vk_right) && !keyboard_check(vk_left)) { //Right Acceleration global.speed_count = min(global.speed_count+0.6,20); facing = 'right'; } else if( keyboard_check(vk_left) && !keyboard_check(vk_right)) { //Left Acceleration global.speed_count = max(global.speed_count-0.6,-20); facing = 'left'; } else { //Decceleration global.speed_count = sign(global.speed_count)*max(abs(global.speed_count)-0.6,0); } //move if place_free(x+global.speed_count,y-i) { x+=global.speed_count; y-= i; exit; } As for skidding, you just put inside the left or right pressing segments if global.speed_count is less than or greater than (depending on which), set skidding on. But set skidding of before them, so that if neither execute, you aren't skidding: skidding = 0; ... //Right if(global.speed_count < 0) skidding = 1; ... //Left if(global.speed_count > 0) skidding = 1; ... Link to comment Share on other sites More sharing options...
sonic101 Posted June 9, 2006 Author Report Share Posted June 9, 2006 "global.speed_count = sign(global.speed_count)*max(abs(global.speed_count)-0.6" Im not sure what the abs and sign command do could you explain what it's doing? Link to comment Share on other sites More sharing options...
Kain Posted June 9, 2006 Report Share Posted June 9, 2006 sign gets the sign of the variable (either -1, 0, or 1). abs gets the absolute value of the variable (the number minus the sign). For example: -8.7 has a sign of -1 and an abs of 8.7. What that snipett of code does is first take the absolute value of the number and subtracts it by 0.6 (the decceleration speed) but uses a max function to make sure it doesn't go beyond 0. After that, if multiplies it by its sign to make sure that it gives back the sign it took away in the abs function. Basically, it brings the number 0.6 closer to 0 without going beyond 0. Link to comment Share on other sites More sharing options...
sonic101 Posted June 9, 2006 Author Report Share Posted June 9, 2006 ah, okay... thign is i suck most badly at math ( never got higher then 52 in passing grades) Link to comment Share on other sites More sharing options...
Kain Posted June 9, 2006 Report Share Posted June 9, 2006 If it helps you to understand, this is the more direct way to do it: if(global.speed_count>0) global.speed_count = max(global.speed_count-0.6,0); if(global.speed_count<0) global.speed_count = min(global.speed_count+0.6,0); There's not always merrit in trying to compact code if it just makes it confusing, after all. Link to comment Share on other sites More sharing options...
Recommended Posts