Legacy GM [Solved] Moving right to left

C

chaslinux

Guest
In an alarm[1] event called obj_ufospawner I have the following code:

Code:
// set the direction of the UFO
var ufo_dir = irandom(1);
var x_start = 0; // set the x start point
var y_start = random(400) + 60; // set the y start point

if (ufo_dir == 0)
{
    direction = 0; // go right
    x_start = -16;
}
else
{
    direction = 180; // go left
    x_start = room_width + 16;
}

alarm[1] = 300;
if(!instance_exists(obj_ufo))
{
    instance_create(x_start,y_start,obj_ufo);
}
What's happening is that if ufo_dir == 0 the sprite gets drawn and moves to the right. But if ufo_dir == 1 the sprite doesn't seem to appear.

At first I thought that maybe ufo_dir never equaled one, so I added a room_goto(); function to the else. Sure enough once in awhile I'd be redirected to the room I set in the room_goto(); function.

Direction 180 should be the left heading. Since I created the instance_create() outside of the evaluation I'm thinking it should be working. Feel like I'm missing something very obvious (face palm).

Thanks...
 

Alexx

Member
You've made a great start.
As well as setting a direction for the UFO you also need to set some speed for it, or otherwise change the x position each step.
There is a built variable:

Code:
speed
Which can use.
Have a go yourself, if you need some more code just ask.
 
C

chaslinux

Guest
You've made a great start.
As well as setting a direction for the UFO you also need to set some speed for it, or otherwise change the x position each step.
There is a built variable:

Code:
speed
Which can use.
Have a go yourself, if you need some more code just ask.
Actually in the obj_ufo Create event I've set the speed, should it be in the obj_ufospawner event instead? Here's the obj_ufo Create code:
Code:
// set the sprite
sprite_index = spr_ufo;

// half the animation speed
image_speed = 0.5;

speed = random(5); // how quickly the UFO speeds across the screen
 

Relic

Member
Can’t see any issues in your code yet. Don’t suppose you do something silly like destroy the UFO when it’s x position is greater than the room width?
 
C

chaslinux

Guest
Can’t see any issues in your code yet. Don’t suppose you do something silly like destroy the UFO when it’s x position is greater than the room width?
Yes, but only if the hspeed is also greater than 0. And I tried commenting out the code and still don't see the UFO.

In obj_ufo I have an Outside Room event with the following code:

Code:
if (hspeed < 0 && x < 0)
{
    if (global.ufotime > 100)
    {
        global.ufotime -=100; // this is a relative variable -100 wouldn't be
        alarm[1] = global.ufotime;

    }
    instance_destroy();
}

//if (hspeed > 0 && x > room_width)
//{
//    if (global.ufotime > 100)
//    {
//        global.ufotime -=100; // this is a relative variable -100 wouldn't be
//        alarm[1] = global.ufotime;
//    }
//    instance_destroy();
//}
I tried commenting out all the code and oddly the ufo appears left to right and destroys itself when it goes off the screen, but doesn't apper right to left. Wonder if there's some GM caching thing going on?
 
Last edited by a moderator:

Alexx

Member
You are setting a direction, but not applying it to the UFO.
You can use something like:
Code:
if(!instance_exists(obj_ufo))
{
   ufo=instance_create(x_start,y_start,obj_ufo);
   ufo.direction=0;
}
 
C

chaslinux

Guest
Thanks Alexx, it's rather nasty code (below) but it's now working. I was trying to only have one instance_create for both directions. Initially when I tried things it still wasn't working, then it ufo spawned from the right, but not the left... so I set up a draw event to show if obj_ufo exists and that showed me that it did even when there was nothing on the screen. I uncommented my outside room event and things worked.

Here's the obj_ufospawner alarm[1] now:

Code:
// set the direction of the UFO
var ufo_dir = irandom(1);
var x_start = 0; // set the x start point
var y_start = random(400) + 60; // set the y start point
var ufo = 0;

if (ufo_dir == 0)
{

    if(!instance_exists(obj_ufo))
        {
            x_start = -16;
            ufo=instance_create(x_start,y_start,obj_ufo);
            ufo.direction = 0; // go right

        }
}

else
{

    if(!instance_exists(obj_ufo))
        {
            x_start = room_width + 16;
            ufo=instance_create(x_start,y_start,obj_ufo);
            ufo.direction = 180; // go left

        }
 


}

alarm[1] = 300;
In the obj_ufospawner object I added the draw event with the following:

Code:
str1 = "obj_ufo exists";

draw_text(60,200,string(alarm[1]));

if(instance_exists(obj_ufo))
{
        draw_text(200,200, str1);
}
else
{
        string_delete(str1,1,15);
}
The string obj_ufo exists appears when the ufo appears and disappears when the ufo disappears, both directions. And the ufo is working both directions.

Thanks again!
 

Attachments

Top