Mirror image of the hero and his animation states in the mirror hanging on the wall (GMS 1.4)

Either use "with" or that for loop because they're both meant to do the same thing under different scopes. Let's say you have 10 bullets that have been fired, the mirror is actually going to draw 100 of them in stacks of 10 because you're telling every single bullet to perform that same action.
 

Imiglikos

Member
Either use "with" or that for loop because they're both meant to do the same thing under different scopes. Let's say you have 10 bullets that have been fired, the mirror is actually going to draw 100 of them in stacks of 10 because you're telling every single bullet to perform that same action.

When I remove this "with", the bullet does not form on the mirror then ... so I had to use it. I do not know another solution and it works now



This is how it works and the projectile is displayed on the mirror

GML:
var _xx1 = x
var _yy1 = y


with obj_bullet  {

for (i = 0; i < instance_number(obj_bullet); i += 1) {
    obj= instance_find(obj_bullet, i);
    draw_sprite_ext(spr_bullet,0,obj.x - _xx1+ 32 ,obj.y  - _yy1 - 70,1,1,0,image_blend,.5)

}
   }

Here in this variant it does not work and the projectile is not displayed on the mirror

GML:
var _xx1 = x
var _yy1 = y





for (i = 0; i < instance_number(obj_bullet); i += 1) {

    obj= instance_find(obj_bullet, i);

    draw_sprite_ext(spr_bullet,0,obj.x - _xx1+ 32 ,obj.y  - _yy1 - 70,1,1,0,image_blend,.5)


}
 

Nidoking

Member
The most obvious mistake in the second case is that you need "obj.image_blend" to match roughly what the first case was doing. It still only draws one bullet per bullet instead of (number of bullets) bullets per bullet, but if you want the number of sprites to be squared, I think your want is a bit off.
 

Imiglikos

Member
Yes image_blend needs this function and alpha. Hence the conscious choice of draw_sprite_ext.
I don't know how to solve the problem of generating more bullet on the mirror
 

Imiglikos

Member
Hi


On the surface, I try to draw a background that I would like to be behind the mirror image of the hero on the mirror, but despite using the object and setting the depth, the background is in front of the mirror image of the hero.

I have the mirror set to depth +10 and the hero's mirror image is on this depth, and the background that he draws on the mirror I set depth +20, so it should be behind the mirror image of the hero, not in front of


obj_mirror

Draw

GML:
///Mirror
// draw sprite mirror
draw_sprite(sprite_index,0,x,y);

// mirror position


_xx = x;
_yy = y;


_surf = surface_create(300,200); // mirror size


surface_set_target(_surf);
draw_clear_alpha(c_black,0);
draw_sprite_ext(obj_hero.sprite_index,obj_hero.image_index,obj_hero.x - _xx ,obj_hero.y - _yy - 70,obj_hero.image_xscale,obj_hero.image_yscale,0,image_blend,.5)


// we draw a bullet on the surface
var _xx1 = x
var _yy1 = y



for (i = 0; i < instance_number(obj_bullet); i += 1) {
    obj = instance_find(obj_bullet, i);
    draw_sprite(spr_bullet, 0, obj.x - _xx1, obj.y  - _yy1 );
}


 
                                            

                                                                                      

surface_reset_target();

draw_surface(_surf,_xx,_yy); // draw  mirror

surface_free(_surf) // clean mirror
                                            
                                            

//we draw a sprite of an object that is not in the editor room
if (instance_exists(obj_background_surface)) {
spriteToDraw = obj_background_surface.sprite_index;
} else {
 targetObject = instance_create(0,0, obj_background_surface);
spriteToDraw = targetObject.sprite_index;
with (targetObject) {
      instance_destroy();
  }
}



//we draw a background on the surface



var draw = 0;

with (obj_hero){
    
    
    if distance_to_object(obj_mirror) <= 20 {
    
        
        draw = 1;
        
    }
}


if draw {

draw_sprite_ext(spriteToDraw,0,x ,300,image_xscale,image_yscale,0,image_blend,.5)



}
 
Code is executed in the same order as you would read a book. It doesn't just order things in the logical way you expect it to. If you put one book down on a desk and then put another one on top of it, there's no surprise as to why the first book you placed is no longer visible. Depth affects drawing between instances, like stacking books, but it won't cause the code tin overlap, like stacking pages between two books. Your comment in the code states that you draw the background on the surface, but if you read over the entire event, you'll see that's impossible.
 

Imiglikos

Member
Code is executed in the same order as you would read a book. It doesn't just order things in the logical way you expect it to. If you put one book down on a desk and then put another one on top of it, there's no surprise as to why the first book you placed is no longer visible. Depth affects drawing between instances, like stacking books, but it won't cause the code tin overlap, like stacking pages between two books. Your comment in the code states that you draw the background on the surface, but if you read over the entire event, you'll see that's impossible.
What should I do? that the background is drawn on the mirror surface and is behind the mirror image of the hero?
 
Look at how the code is structured. You're saying that you're drawing the background to the surface, but you're not. You've already ended the surface and drawn it before the background is even referenced. What gives you the impression the background is drawn to the surface?
 

Imiglikos

Member
Look at how the code is structured. You're saying that you're drawing the background to the surface, but you're not. You've already ended the surface and drawn it before the background is even referenced. What gives you the impression the background is drawn to the surface?

You was right I draw the background behind the surface ... I have already improved it, but still the background is in front of the mirror hero and not behind the mirror hero.
Maybe the background should be painted on a separate surface? so that I can use depth?



GML:
///Mirror
// draw sprite mirror
draw_sprite(sprite_index,0,x,y);

// mirror position


_xx = x;
_yy = y;


_surf = surface_create(300,200); // mirror size


surface_set_target(_surf);
draw_clear_alpha(c_black,0);
draw_sprite_ext(obj_hero.sprite_index,obj_hero.image_index,obj_hero.x - _xx ,obj_hero.y - _yy - 70,obj_hero.image_xscale,obj_hero.image_yscale,0,image_blend,.5)


// we draw a bullet on the surface
var _xx1 = x
var _yy1 = y



for (i = 0; i < instance_number(obj_bullet); i += 1) {
    obj = instance_find(obj_bullet, i);
    draw_sprite(spr_bullet, 0, obj.x - _xx1, obj.y  - _yy1 );
}


//we draw a sprite of an object that is not in the editor room
if (instance_exists(obj_background_surface)) {
spriteToDraw = obj_background_surface.sprite_index;
} else {
targetObject = instance_create(0,0, obj_background_surface);
spriteToDraw = targetObject.sprite_index;
with (targetObject) {
      instance_destroy();
  }
}



//we draw a background on the surface



var draw = 0;

with (obj_hero){
   
   
    if distance_to_object(obj_mirror) <= 20 {
   
       
        draw = 1;
       
    }
}


if draw {

draw_sprite_ext(spriteToDraw,0,x ,300,image_xscale,image_yscale,0,image_blend,.5)



}

                                           

                                                                                     

surface_reset_target();

draw_surface(_surf,_xx,_yy); // draw  mirror

surface_free(_surf) // clean mirror
 
Last edited:
As noted earlier, code executes in order. You are drawing the player, then the bullets, then the background. If you had stickers of those three elements and you stuck them on the wall on top of each other, which one would be in front?
 

Imiglikos

Member
As noted earlier, code executes in order. You are drawing the player, then the bullets, then the background. If you had stickers of those three elements and you stuck them on the wall on top of each other, which one would be in front?
aaaaa ok now i understand. i have to keep the order of drawing on the surface.
Though I thought it would be enough to assign an object / layer to the sprites and set the depths and draw on the surface.
Thanks for your help. Now it's okay



I also have a question about the background painted on the surface, how do I get such a slight transition effect? for example, the hero approaches the mirror, the background appears, but not like now, but with a transition effect, something like alpha / fade and disappears just like he moves away from the mirror?
 
Last edited:
Remember that depths only account for the Draw events of different instances, not the code within them. To bring back the book analogy, think of it like cutting out all the pages in a book. You can reorder the pages to completely break the flow of the story, but the words on each individual page won't be out of order. Just because you change the depths and layers, you would still draw the background on top because it draws after everything else in the surface in this particular event.


Use a variable to control the alpha. When the player gets within range, slowly increment the variable with a low value to go from 0 to 1. When the player gets out of the range, decrement the variable to 0.
 

Imiglikos

Member
I set parallax on the background and placed the obj_mirror object on the background some moving background element. The background and the mirror move at the same speed to give the impression that they are on the same layer
I have a question about this topic, what is the reason the hero's reflection on the right ends before I reach the edge of the mirror? On the left side it's okay, but on the right side it cuts off.



GML:
///Mirror
// draw sprite mirror parallax
draw_sprite(sprite_index,0,x+view_xview/7.5,y+view_yview/7.5);

// mirror position


_xx = x;
_yy = y;


_surf = surface_create(300,200); // mirror size


surface_set_target(_surf);
draw_clear_alpha(c_black,0);
draw_sprite_ext(obj_hero.sprite_index,obj_hero.image_index,obj_hero.x - _xx + 32 ,obj_hero.y - _yy - 70,obj_hero.image_xscale,obj_hero.image_yscale,0,image_blend,.5)


// we draw a bullet on the surface
var _xx1 = x
var _yy1 = y



for (i = 0; i < instance_number(obj_bullet); i += 1) {
    obj = instance_find(obj_bullet, i);
    draw_sprite(spr_bullet, 0, obj.x - _xx1 + 32, obj.y  - _yy1 - 70 );
}

                                                                                                                         

surface_reset_target();

draw_surface(_surf,_xx+view_xview/7.5 , _yy+view_yview/7.5); // draw  mirror parallax surface

surface_free(_surf) // clean mirror
 

Imiglikos

Member
I don't know why, when I set the parallax on draw_surface so that the mirror moves according to the mirror spirit, the hero's reflection in the mirror tries to overtake the hero, when I go to the right and if I go to the left the mirror image tries to catch up with the hero
if the sprite parallax and the mirror parallax and the parallax draw_surface are set at the same speed, the hero's reflection in the mirror should also move at the same speed and does not try to overtake or catch up with the hero.

I will make it clearer in the picture below what I mean

example.png
 

Imiglikos

Member
Does anyone know what should be done so that the mirror on the sides does not cut the mirror image of the hero? This phenomenon occurs when I have a parallax set on an object and the mirror moves left and right.

Thank you
 

Imiglikos

Member
Maybe the problem is that the parallax code should be in the step event and not the draw?
I'm trying to solve this problem. If I do not use parallax, the mirror image is in the correct position, but when I use parallax, the mirror image starts in the wrong position and ends in the wrong position and should be in the mirror
 

Imiglikos

Member
Hi
Maybe I will describe my problem in a more detailed and logical way
I have a mirror object that is placed on the wall in the room. When the hero approaches the mirror, his mirror image is shown. This mirror works properly.
However, when I turn on the parallax on the mirror object (because I want the mirror to move at the same speed as the wall with the background), the mirror image of the protagonist starts not in the mirror but in front of the mirror and ends behind him the mirror will end.
Of course, I also set parallax on the mirror surface so that the mirror moves flush with the object, but that doesn't help. Still the mirror image of the protagonist starts in front of the mirror, not in the mirror and ends before the mirror ends.
I've tried it in different ways, but still the same .. without parallax it's okay, but with parallax this is what happens.

I am asking you for any help what I should do to set it up so that with parallax the mirror image of the player starts in the mirror and ends at the edges of the mirror

below I put the code without parallax, i.e. the one that works perfectly and the code with parallax where there is a problem with mirror image

obj_mirror no parallax

draw


GML:
///Mirror
// draw sprite mirror parallax
draw_sprite(sprite_index,0,x,y);

// mirror position


_xx = x;
_yy = y;


_surf = surface_create(935,378); // mirror size


surface_set_target(_surf);
draw_clear_alpha(c_black,0);
draw_sprite_ext(obj_hero.sprite_index,obj_hero.image_index,obj_hero.x - _xx + 32 ,obj_hero.y - _yy - 115,obj_hero.image_xscale,obj_hero.image_yscale,0,image_blend,.5)


// we draw a bullet on the surface
var _xx1 = x
var _yy1 = y



for (i = 0; i < instance_number(obj_bullet); i += 1) {
    obj = instance_find(obj_bullet, i);
    draw_sprite(spr_bullet, 0, obj.x - _xx1 + 32, obj.y  - _yy1 - 115 );
}

                                                                                                                        

surface_reset_target();

draw_surface(_surf,_xx , _yy); // draw  mirror parallax surface

surface_free(_surf) // clean mirror

obj_mirror with parallax

draw



GML:
///Mirror
// draw sprite mirror parallax
draw_sprite(sprite_index,0,x+view_xview/7.5,y+view_yview/7.5);

// mirror position


_xx = x;
_yy = y;


_surf = surface_create(935,378); // mirror size


surface_set_target(_surf);
draw_clear_alpha(c_black,0);
draw_sprite_ext(obj_hero.sprite_index,obj_hero.image_index,obj_hero.x - _xx + 32 ,obj_hero.y - _yy - 115,obj_hero.image_xscale,obj_hero.image_yscale,0,image_blend,.5)


// we draw a bullet on the surface
var _xx1 = x
var _yy1 = y



for (i = 0; i < instance_number(obj_bullet); i += 1) {
    obj = instance_find(obj_bullet, i);
    draw_sprite(spr_bullet, 0, obj.x - _xx1 + 32, obj.y  - _yy1 - 115 );
}

                                                                                                                        

surface_reset_target();

draw_surface(_surf,_xx+view_xview/7.5 , _yy+view_yview/7.5); // draw  mirror parallax surface

surface_free(_surf) // clean mirror
 

muki

Member
As with all things visual glitch, a clip would really help. It sounds like a rather complex issue, with the parallax, so it's really difficult to visualize.

Maybe that's just me though. I'm more of an artist/visual person than someone who does logic.
 

Imiglikos

Member
As with all things visual glitch, a clip would really help. It sounds like a rather complex issue, with the parallax, so it's really difficult to visualize.

Maybe that's just me though. I'm more of an artist/visual person than someone who does logic.


I am sending below two videos in one, the problem is illustrated where the parallax is set.


mirror without parallax







mirror with parallax


 

Imiglikos

Member
Haven't any of you used a mirror in-game? and did not struggle with a similar problem as me?
my problem only occurs when I have parallax set up on the mirror. (if I turn it off then everything is fine) I need this parallax because I have moving backgrounds in the game and the mirror is part of the background.
I posted two short videos in the previous post, one of them has this problem that I'm talking about all the time.
This is the movie with parallax on.
 
Top