Game Maker Tank Sprite

Posted on  by  admin
  1. Free Sprites For Game Maker

Motion is critical to just about any video game. Nearly every game has moving things in it, and how they move is a vital part of the game. Learning how to program motion and control it effectively is one of the most important parts of a successful game. There are a number of possible approaches to handling position and movement.

Learning how these work will help you make better games.This isn’t absolutely everything there is to know about motion, but it’s a great overview to start with, and covers everything I’ve learned with respect to motion in GameMaker Studio. Position Variables: x, yThe current x and y coordinates of the instance.These are in reference to the absolute x and y coordinates in the room. The left-top corner of the room is (0,0), and the right-bottom corner of the room is (roomwidth, roomheight).Coordinates outside the room are valid locations — objects don’t automatically cease to exist or stop functioning just because they are outside the room. Indeed, they continue to exist and function, unless you code them otherwise, and of course they can come back inside the room.Think of the room as the “stage” of a theater that the player views; the area outside the room is “off-stage”. Stuff still exists when it goes off-stage, and things can happen to that stuff.

However, positions that lie outside the room will never be displayed in the game. The game will stop scrolling at the edge of a room, showing no space outside of it. Xprevious, ypreviousThe former x and y coordinate of the instance, from the previous step. Comes in handy when you need to compare where an object is to where it was, or if you’re trying to handle collisions with high-speed objects (see below), or to “undo” a position change to the old position in the previous step. Xstart, ystartThe x and y coordinates of the instance when it was created. Useful if you need to return an object back to where it started from. You can also change the values to update the starting point (such as if the player reaches a new save point.) GML move functionsThere are a number of GML functions built in to GameMaker, whose names start with “move” that are good to be aware of.

Movesnap(hsnap, vsnap)Moves the instance to the nearest position in the room, aligned to an invisible grid of “cells” of a size defined by the hsnap and vsnap arguments.This is pretty easy to understand. Let’s say you want to base your game off of a grid of 32×32 pixel cells. You want your instances to align to this grid.

If an instance is at a position such that instance.x and instance.y are both mulitples of 32, then it is aligned to the grid. If the instance is not aligned to the grid, this function will move the instance to the nearest location that is a grid intersection.A common problem you may encounter with this function is if you have a grid within the room but that does not take up the entire room. Say you have a chess board in the room, but there is a border around the chess board. You want cells in the invisible grid to align with the squares of the chess board, but because of the border, there’s an offset because the border is not a width that aligns to the grid. To adjust, you simply need to offset the positions of the instance after calling movesnap.

Movesnap ( 32, 32 ); x += borderx; //borderx and bordery must be defined by the programmer. Y += bordery;movesnap(32, 32);x += borderx; //borderx and bordery must be defined by the programmer.y += bordery;Keep in mind, too, that the instance’s sprite origin can confuse the location where the instance is being drawn. If the origin is centered, the instance will appear centered on the grid intersection (like a stone in Go) rather than centered inside the grid cell (like a chess piece). The results from movesnap are based on the instance’s x and y, so how it ends up looking will depend on the instance’s sprite’s origin. Moverandom(hsnap, vsnap)The moverandom function moves the instance to a random location in the room that is an intersection on the invisible grid defined by the hsnap and vsnap arguments.As with movesnap, moverandom has the same issues to be aware of with grid offset and sprite offset. Movetowardspoint(x, y, speed)This function sets the speed and direction of the instance so that it will move towards the point (x, y) as defined by the first two arguments.

Game

It’s convenient in that the function figures out the direction to travel for you, based on the spatial relationship between the calling instance and the destination point. Movecontactall(dir, maxdist), movecontactsolid(dir, maxdist)Moves the calling instance in direction dir, up to maxdist pixels, or until the instance contacts another instance. This does not set a speed, so it is using the position assignment method to move the instance. If the moving instance does encounter an obstacle, it stops just short of triggering a collision. The movecontactsolid function works by checking for contact with objects objects that have the solid property set to true.This is a very useful function for platformer games, where moving instances need to stop when they encounter a solid wall or platform object, without triggering a collision with it. Movebounceall(advanced), movebouncesolid(advanced)Calling this function causes the object to bounce when it encounters another instance (or solid instance) in the game. The instance will only bounce off of other instances if they have a sprite or mask index.

The bounce function calculates the new direction and applies it to the instance.If the advanced parameter is set to true, precise collision detection is used. This requires greater processing and can slow the game down if too many instances are using it. It does not make the angle of the bounce any more precise.

You generally will not need to use precise collision checking.Realistic bouncing is probably better done using the new Box2D physics. This movebounce is an older GML function that has been around in GameMaker since long before the new physics engine was added. You can still use this function if you don’t want to bother with the full physics system. Moveoutsideall(direction, maxdist), moveoutsidesolid(direction, maxdist)These two functions are the inverse of movecontactall and movecontactsolid. They are intended for when two objects are in collision, and can be useful to resolve a collision event so that the collision event does not happen automatically again during the next step. They work by displacing the calling instance up to maxdist pixels away, in direction, until a collision condition is no longer present. If no value is provided for maxdist, GameMaker will assume a value of 1000 pixels.The catch here is that the instance will move until it is outside of any collision condition, not just a collision with the other instance it is currently involved with.

So if the moveoutside function is triggered by a collision with one instance, and moving just outside of collision with the first instance would result in a collision with another instance, the calling object will continue to move until it is positioned outside of any and all collisions, or maxdist is reached. This is possibly not desirable, since you may need the collision with the next instance to be triggered as well. In this case, the best thing to do is set maxdist to a small value, such as the calling instance’s sprite offset.The moveoutsidesolid function can be useful to get an object out of collision condition with solid objects. One of the annoying things about solid objects is that when they overlap (collide) with an object, they prevent the object from moving, causing it to become stuck or embedded inside the solid object. An object with a speed and direction will not move through a Solid object, and if it is in collision with a Solid object, it cannot move by its speed and direction vector any longer. The only way to move it is by position assignment. The moveoutsidesolid function does the necessary calculations to place the calling instance just outside of the collision mask of the Solid object.A lot of GameMaker developers avoid using the Solid property entirely, opting instead to handle objects which they wish to behave as though they were solid using their own code.

MovewrapMentioned here for completeness, but discussed in depth in the section.Shameless plug: I’ve developed an extension called iMprOVEWRAP which is more powerful and flexible than movewrap. It can be downloaded from the. Vector variables:In GameMaker, a motion vector is a speed (in pixels per step), combined with a direction. In mathematics, a vector can be added to a position to get a new position.

Two vectors can be added together to combine their motion. An understanding of Vectors can give you a very useful way to move instances around in GameMaker.

DirectionThe current direction of motion of the instance, in degrees. 0 = right/”east”. 90 = up/”north”. 180 = left/”west”. 270 = down/”south”.

Direction model for Box2D PhysicsYou’ll need to compensate for this if you ever need to convert a GM Object’s direction to a Box2D fixture’s direction:GameMaker direction = Box2D direction + 90Box2D direction = GameMaker direction – 90Related to direction is imageangle, which is a built-in GML variable that is used to rotate the sprite belonging to an instance. If you only change an instance’s direction, it will move in that direction but the sprite will continue to be drawn with its orientation unchanged. If you want the sprite to rotate, you can set imageangle = direction. This is why it is usually best to draw your sprites so that they face right, so that imageangle = 0 looks correct for direction = 0. SpeedThe speed in pixels per step in the current direction.

Negative = moving “backward”; positive = moving “forward” with respect to the current direction. HspeedThe horizontal component of the instance’s speed, in pixels/step. Negative = moving left; positive = moving right.

VspeedThe vertical component of the instance’s speed, in pixels/step. Negative = moving up, positive = moving down.Up being negative often seems counter-intuitive to new programmers, since “up” should be “higher”. So shouldn’t a high y correspond with a higher vertical position? You might think so, but no. The reason for this is that computer displays start drawing every new frame at the top of the screen, and work their way down. So they start at the 0th row of pixels, and count upward toward the bottom row of pixels. If this isn’t a natural concept, you’ll just have to get used to it.

Just remember that the room coordinate system starts with its origin at top left, at (0,0), and counts upward to positions that are vertically down from the origin. Tip: speed, hspeed, and vspeedThe values of speed/ direction, hspeed, and vspeed are interdependent, and are updated automatically by the game each step when one of their values changes.For example, if you set and speed = 8 direction = 0, your instance will be moving to the right at a speed of 8px/step. If you look at the value of hspeed, it will also be 8, even though you never directly set hspeed.This means that hspeed and vspeed and speed are always accurate to the actual motion of the object in the game. This is very useful.For example, if your instance is not moving at an orthogonal angle (eg, 0, 90, 180, 270), and you need to know the horizontal or vertical component of motion for some reason, without hspeed and vspeed, in order to know its precise horizontal or vertical speed, you would need to perform trigonometry functions in order to isolate the horizontal and vertical components of the object’s motion vector.

But, thanks to hspeed and vspeed, this is already done for you, “for free.”One thing to note about this is that changes to hspeed or vspeed can change direction, but changes to absolute speed do not change direction.To illustrate this, take an instance in your game moving at speed = 8, direction = 0. It is moving 8px/step to the right. To reverse its motion, you could use either speed = -speed, or direction += 180. In the first case, you’ve reversed speed but in the other you’ve reversed direction. Visually, the result is identical (unless the sprite’s imageangle is also updated, or you swap spritefacingright with spritefacingleft).If we watch the variables, we see that it’s possible to have negative speed, so that it’s possible to move “backward” at a negative speed, but that when hspeed or vspeed reverses, direction also reverses. An object with a negative hspeed will always have a direction between 90 and 270; positive hspeed, a direction between 0-90 or 270-360. And object with a negative vspeed will have a direction between 0-180; positive vspeed, between 180-360.This can be easier to understand with the following demo.

Your browser does not support iframes. Go to to experience the demo.Use the left/right arrow keys to increase/decrease hspeed. Up/Down arrows to increase/decrease vspeed. W/S to increase/decrease absolute speed, A/D to increase/decrease direction. Watch how the values relate to each other as they change.

GravityIn GameMaker, gravity works by creating acceleration in a given direction (270 down) at a given rate. The gravity variable is the amount of speed that is added to the instance each step, while gravitydirection is the direction in which this speed is added.Gravity is very useful as a component of jumping mechanics, or for ballistic trajectories of flying objects.You can also use gravity for more novel purposes, such as to simulate the force of a strong wind pushing against objects, blowing them away.Gravity can be a little tricky to get right. We think of gravity in the real world as a force which attracts objects with mass towards one another. In GameMaker, it is just a vector, a force which adds speed in a given direction. This vector is added to the instance each step, accelerating it, so even a little gravity can add up quickly and come to dominate the instance’s motion. The official documentation recommends using a small value, such as 0.2. Over time, the acceleration due to gravity will add up, leading to very high speeds, which can lead to problems with collision detection (see below).

Often it’s desirable/necessary to cap an instance’s speed with a “terminal velocity” so speed doesn’t get too crazy. You can do this using the GML functions min, max, or clamp.In GameMaker, gravity does not have a location that the instance is pulled towards — it does not attract an instance to a particular point in the room; it just adds speed in the direction set by the instance’s gravitydirection. (You can fake a gravity toward some object or point in the room, if you update gravitydirection each step, and use pointdirection(x, y, attractorx, attractory)In the real world, we think of gravity as a force which affects everything equally, but in GameMaker, gravity is set on a per-instance basis. This means that you can set different forces and/or direction for each instance. This could be used, for example, to create MC Escher spaces in 2D where gravity works differently for each object, depending which surface they are standing on. Or for creating some objects that are affected by gravity while others are not.You can set gravity to a negative value, which is equivalent to setting gravity in the opposite direction (gravitydirection += 180).You cannot have multiple gravity forces acting on a single instance through the built-in GML gravity variable. You can, however, write your own “gravity” functions that work in the same way as gravity, by using motionadd each step to add a little bit of speed and direction.

FrictionDeceleration, slowing the instance, until speed reaches 0. Friction is useful for simulating slippery surfaces like ice, where an object shouldn’t stop immediately, or viscous surfaces, like mud, where constant effort is necessary in order to keep moving.

Friction has no direction; by its nature it works in opposition to the present direction of the instance it is working on, by reducing its speed until it reaches 0.You can set negative friction, which is essentially the same as setting gravity in a direction which constantly matches the instance’s direction. TechniquesYou can change the values of these functions in any mathematical way you can imagine. Understanding the math is important to getting the behavior you expected. Position assignment.

Imageangle = n; //sets rotation angle of instance's sprite.imageangle = n; //sets rotation angle of instance's sprite.Instance instantaneously faces new direction.You can, of course, also add/subtract degrees to direction, as well:direction += n; // turns the direction of motion by n degreesimageangle += n; //rotates the sprite by n degrees Rotate towardFor automatic rotation:Step event:(If you want “chunky” turning, like a clicking mechanism, use this code in an alarm event instead of the Step event.). Speed = min (maxspeed, speed + accel );speed = min(maxspeed, speed + accel);Speed increases each step by accel pixels/step until maxspeed is reached. Note maxspeed and accel are not built-in variables, and must be created and assigned values in the program.As you can see, most normal motion can be expressed through simple value assignment (the equals sign) or through addition or subtraction.Other math is possible, of course.

These numbers are just numbers, and you can perform any math on numbers. You can get some interesting effects using multiply, divide, exponents, square root, logarithms, trig functions, etc. In many different ways to modify position, speed, or direction.Oftentimes it’s useful to multiply a base speed by another number to get a range of relative speeds for walking, running, or whatever. Linear interpolation and extrapolation with the lerp functionOne gml function that is very useful for movement is lerp, the linear interpolation function. Lerp(a, b, amt) returns a value between a and b that is interpolated by the amount (when amt is between 0 and 1). If your goal is simply to make a ball that will bounce, that’s not too hard.

There are many ways to do it, but is probably the easiest. In general, the GameMaker documentation available at is the best place to start when you want to learn how to do something.There’s also good tutorials built into GM:S, if you look at the projects in the Tutorials tab you can see clearly how many different types of games are built.

If you’re new to GameMaker, they’re the best place to start.If you actually want to draw a line that traces the path the ball takes when it bounces, that’s a different matter. Your best bet would be to ask your question on the, in the Questions & Answers forum.chrissanyk. If you haven’t yet, try asking this on the. Lots of people will help.This could be tricky if you need to calculate the motion with each step, but could be very easy to do if you use Paths, which I didn’t cover in this article.The easiest non-paths way to do it would be to provide an initial motion to the projectile, by setting speed and direction, and give the projectile some gravity.

A linear velocity plus downward acceleration due to gravity will result in a parabolic arc emerging naturally, although to get it to be positioned exactly where you want it, at the speed you want it, could take some trial and error tweaking.Chris Sanyk. Thanks so much for the kind words.When I was learning the basics in GameMaker, I found that as I learned things, I'd forget them again if I didn't use them all the time. After a while, I realized that taking notes and explaining to myself how stuff worked helped a lot — as much as reading the Help file, if not more so. And I realized that this had value beyond simply writing it for myself, so I put it up on my website to share with people, hoping it could benefit many.To this day, this is the most popular thing I've written, and it's always good to hear from people who found it useful.csanyk.

Free Sprites For Game Maker

We'll start this section of the Quick Start Guide by looking atsprites which are generally one of the first things you'llneed when making any project in GameMaker Studio 2. Asexplained in the section on, a sprite is an image that canbe animated (although it doesn't have to be) and then drawn to thescreen. In general a sprite will be associated with an object, butyou can draw sprites on their own, either from code or in the roomeditor Asset Layer (more on this later).

You can also createa sprite to be used as a tile set, but we'll explore thatoption later too.When you createa new project from the,your resource tree will only contain empty folders for theresources you may need, so you need to right-click on the sprite resource folder andthen select the option Create. This will create a new spriteand open the sprite editor for you (if the sprite editor does notopen, simply double-click on the new resource):As you can probably see, the top left of the window has a fieldfor the 'Name' of the sprite.

All sprites (and all other resources)must have a name given to them so that you (and GameMaker Studio2) can identify them easily, although you should note that thisname is really just a variable which holds an ID value that'points' to the resource, in this case a sprite. It's best to giveeach sprite a descriptive name so that you can identify at a glancewhether a particular resource is a sprite or an object or anythingelse, and many people do this by prefixing or suffixing theresource with the letters ' spr' - for example, 'sprBall'.Note that resource names are limited to using only letters, digitsand the underscore symbol ' in a name of a sprite (and any otherresource) all resources must start with a letter, not a number.The other featuresof the sprite editor we'll discuss at the end of this section, butfirst we need to explain how to draw a sprite. This is done in theImage Editor. The image editor is a very powerful tool forcreating the graphics in your game and is opened by clicking theEdit Image button.

Coments are closed