GameMaker - Sprite will not stop moving backwards

I am very new to game development, and I am using GameMaker Studio as my first engine. I was following along with a tutorial to create an Asteroids-like game from scratch, but right now I am trying to create my own coding. There are small problems, but I believe it may just be my coding. The coding language is GML, its proprietary language.

The first problem is that I cannot get my sprite to stop moving backwards:

motion_add(image_angle,-0.2);
if (speed < 0) speed = 0;


It seems okay, since I am trying to force the sprite to have a motion of zero if the speed starts going negative.

Another thing is that I cannot seem to get the bullets to fire from my ship. The code is under the event "Press <Space>":

bullet = instance_create(x,y,obj_bullet);
bullet.speed = 15;
bullet.direction = image_angle;


The bullet literally just doesn't appear, at all. I know it isn't invisible because firing at an asteroid will not destroy the asteroid.

Thanks for the help everyone!

• Which object is the press space event within? – user64742 Dec 28 '18 at 7:36

For your first question, does motion_add move the object or add speed? I'm guessing what's happening is the object has no speed, but you're still moving it with move_add.

What you probably want to do is something like:

if(speed > 0) motion.add()
else if(speed < 0) speed = 0


This way we only add motion if the speed is positive.

Even better would be to first calculate speed and then calculate position:

speed = speed + acceleration
if(speed < 0) speed = 0
position = position + speed


I have never worked with Gamemaker, so I apologize if I've suggested something it doesn't allow you to do.

For your second question (again, haven't used Gamemaker, so forgive me if these are moot), a few things to check: - Are x and y where you expect them to be? EG, is the bullet being created, just off screen? - Are you sure press is getting called at all? EG, do you have to map it to a key?

If Gamemaker allows you to debug at all, adding a breakpoint on the first line would answer both of these questions (if it breaks, you know the code is being entered, and you can check the values of x and y).

• "Are you sure press is getting called at all? EG, do you have to map it to a key?" It's a built in event. – user64742 Dec 28 '18 at 7:30
• "If Gamemaker allows you to debug at all, adding a breakpoint on the first line would answer both of these questions (if it breaks, you know the code is being entered, and you can check the values of x and y)." No, it does not allow break points. – user64742 Dec 28 '18 at 7:30
• "For your first question, does motion_add move the object or add speed?" Read the documentation. – user64742 Dec 28 '18 at 7:31

According to the game maker studio documentation:

speed has no direction associated it with it as this is governed by the direction value of the instance, but it can have a negative value, in which case the instance will travel in the opposite direction to that set by the direction function (ie: direction - 180°).

This means that most likely the direction rotated by 180 which is why the object keeps moving.

As for the object not spawning that is a separate question and should be posted as a new question. Please do not ask more than one question in a post.

Source: https://docs.yoyogames.com/