Hello! Using the c language and I need help with a particular problem. I have sp
ID: 2246634 • Letter: H
Question
Hello! Using the c language and I need help with a particular problem.
I have sprite that has to move from stationary to walking speed (0.5) when a key is pressed. Either 'a' or 'd' for left and right respectivly.
All this is encpsulated in a single function called onestep that plays one step of the game. Now, the problem arises when I have to make the character run i.e change the speed to 1 from the current walking speed of 0.5 when the same key is pressed again. So far i've got something like this for walking right: if ( key == d ){ walk }, however I dont' know how to deal with a second key press once the sprite is walking. Heres what I have so far, I know its wrong, but am I at least on the right track?
I.e
set global variable speed = 0;
for walking: if ( key == d) { walk and set speed = 1}
for running: if ( key == d && speed = 1){ run and set speed = 2}
doesn't work because the program goes right into the 'run' if statement regardless once d is pressed....
Any help would be appreaciated! Thanks!
Explanation / Answer
The below logic should solve your problem.
/*The below variable walk will be useful to identify whether the walking it enabled or not*/
set global variable walk=0;
/*The below variable speed can control the speed/repeatable key press, when the user press on the key again to increase the speed*/
set global variable speed=0;
if(key==d && walk ==0){
speed=0;
walk++;
}else if(key==d && walk >1){
/*enable running by incrementing/increasing the speed with 1 */
speed++;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.