This is how to make your player hold a sword which you can actually steer. This is great in side view games because it gives the user a lot more possibilities to interact and it creates more variation.
So we have to things that should move differently, but dependently on each other. Unlike it is easy to believe we do not need two objects, although we need two sprites. One for the character and one for the sword, as the character is the larger sprite I choose to use this as sprite for the only object.
I use three events, but two of them is left key and right key, and they don’t count because the only thing I got in them is x+=4; and x-=4; … the only “real” event, I think, is the draw event where I put this code:
draw_sprite(spr_stickman,0,x,y);
dir = point_direction(x+170,y+90,mouse_x,mouse_y);
draw_sprite_ext(spr_arms,0,x+170,y+90,1,1,dir,c_white,1);
Let’s look at this code line by line. The first line only draws the sprite of the character itself, as it won’t be shown otherwise, although I set it as sprite, because I use draw event. So that should be the first thing. It’s also first because I want it being placed in the background of the sword which would not overlap the body if it was drawn first.
This second line calculates what direction that should be used when drawing the sword. This is the direction between the handle of the sword and the mouse position. Notice that x+170 and y+90 are merely numbers, as every sprite would probably have differently long arms it is nothing that I can actually write a universal equation for. You should exchange the numbers so that it fit your sprite but use x and y as origin.
When I draw it I draw it with direction dir, and I use exactly the same x and y positions as when I calculated the direction (this is important, otherwise things will screw up). The next article will treat collision with that sword, which might be kind of tricky as it is not an object itself.
Example:
http://gmtutorials.com/files/armsrot.gm6
.
Users logged in:
Comments
Loading comments...