First you need a sprite. It doesn’t need to have any sub-images or so, but you need to set the origin to the centre. This is because we will use the draw_sprite_ext functions to rotate, and we want the sprite to be rotated around itself rather than around an axis beside the car, which would look rather strange.
draw_sprite_ext(sprite_index,0,x,y,1,1,direction,-1,1);
You may copy that line straight off, as it will work with any object (variables such as sprite_index, x, y and direction will auto adjust it). Next thing is to make the acceleration work, with this car engine the user may accelerate using the “up” arrow button, so in the keyboard up event, put this:
if (speed<7) {
speed +=0.5
}
The condition says: if the current speed is lower than seven, accelerate. Thus seven is the max speed. You may set this to anything else; I just took seven as an example. Max speed may also be a variable, which is useful if you want to have speed boosters that will raise your speed above the max speed (then you’d need to raise the max speed). To slow down we put the reversed piece of code in step event, which looks like this:
if (speed>0) {
speed -=0.2;
}
Notice that this will run even though you are pressing the up button, therefore the actual acceleration is merely 0.3 (0.5-0.2), each step. To turn we put this code in the right key event:
if (speed>0) {
direction -= 5;
}
The condition states the obvious, that you can’t turn without at least some speed. For left key event you should do the reverse, where direction += 5 instead of -= … -=/+= means that the values will be change relatively to their previous value by the way. This means that direction +=5 is the same as direction = direction+5.
I think this is all, I got an example here for you to study:
http://gmtutorials.com/files/carengine.gm6
.
Users logged in:
Comments
Loading comments...