2017-04-08 13:08:55

Hi!

Can you give me an example code walktimer such, which goes four directions?
So forward, backward, right, left.

Please help me!

Regards,
Bryan.

2017-04-08 18:02:25

Run a timer. You can check how much time has passed like this:
void main()
{
timer mytimer;
show_game_window("timer test");
while(mytimer.elapsed < 5000)//milliseconds
wait(5);
}
What does this do? Creates a timer, watches it until it reports that 5000 milliseconds (or 5 seconds) have passed and then quits.
Now let's look at the movement side of things. Let's say you have a player class like this:
class player
{
int x_position;
int y_position;
timer movement_speed;
void move()
{
//Check if movement_speed.elapsed is greater than the interval at which you want to move.
if(movement_speed.elapsed >= 250)//four steps per second, about the step interval of human running.
{
//do your checks here.
if(key_pressed(KEY_UP))
walk(forward);//forward should be defined as a const int.
//...

}

}
//and here's where you might handle walking itself.
void walk(int direction)
{
if(direction == forward)
{
y++;//geometry. If you're moving up and down you're moving along the Y axis. If you're moving left and right you're moving along the x axis.
//code to play footstep sounds goes here.
//...

}
}

}
Of course this isn't complete and working, but it should give you an idea of where to start. There's more to it if you want to make a complicated game, like checking where the player is trying to step and making sure it can actually step there (for instance, making sure you can't step inside a wall).

Official server host for vgstorm.com and developer of the Manamon 2 netplay server.
PSA: sending unsolicited PMs or emails to people you don't know asking them to buy you stuff is disrespectful. You'll just be ignored, so don't waste your time.

2017-04-08 18:51:32

This might also work.
int x;
int y;
timer walktime;
sound step1;
void main(){
show_game_window("walktimer test");
step1.load("sounds/step1.wav");
while(true)
{
if(key_down(KEY_UP) and walktime.elapsed>=300)
{
step1.play();
x+=1;
}
if(key_down(KEY_DOWN) and walktime.elapsed>=200)
{
step1.play();
x-=1;
walktimer.restart();
}
and so you will carry on with the y command.
I always say, to learn from an example will be the best way to learn.
This is reely easy, tell me if you don't understand something, i am reely not the best with bgt, but i will try to help where i can.
thanks.

best regards
never give up on what ever you are doing.

2017-04-08 20:56:18

Thanks! And in this game what the fault?
Please, tell me.
For some reason, you don't walk, simply nothing happens.
The game sourcecode link.

http://www.tdstudios.esy.es/side scroller.zip

2017-04-09 11:11:29

Maby look i made a mistake. the walk timer should be restarted after it is elapsed.

best regards
never give up on what ever you are doing.

2017-04-30 10:05:56

int x,y;
double cooldown;
void main() {
step.load("Footstep.wav");
while(!key_pressed(KEY_ESCAPE)) {
if(cooldown>0.0) cooldown-=0.005;
if(cooldown<0.0) cooldown=0.0;
if((key_down(KEY_UP))&&(y<10)) walk(0,1);
else if((key_down(KEY_LEFT))&&(x>0)) walk(-1,0);
else if((key_down(KEY_DOWN))&&(y>0)) walk(0,-1);
else if((key_down(KEY_RIGHT))&&(x<10)) walk(1,0);
wait(5);
}
}
bool walk(int x_increment,int y_increment) {
if(cooldown!=0.0) return false;
x+=x_increment;
y+=y_increment;
cooldown=0.325;
step.stop();
step.play();
return true;
}
For me the best way is handling timers through double variables.