• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Moving sprites on an asset layer

M

mochipon

Guest
I'm using the code almost exactly as it is provided in the examples from the help file, but it won't do anything to the sprites on that layer.
My code:
Code:
var lay_id = layer_get_id("Assets_FG_0_2");

var spr_id = layer_sprite_get_id(lay_id, "spr_block_attach");
var asset_x = layer_sprite_get_x(spr_id);
layer_sprite_x(spr_id, asset_x*1.3);
Obviously, the layer where the sprite assets are on is named Assets_FG_0_2 in the IDE, and the sprite which I'm trying to move is named "spr_block_attach" without the quotes. It's currently the only sprite with that name on that layer (I keep wondering what would happen if there were more than one; which id would layer_sprite_get_id return!?)

I tried substituting the "asset_x" in the last line with a number, but that wouldn't move anything either. The sprite itself remains exactly where I placed it in the IDEs room editor.
What am I missing/misunderstanding here?

Edit:
I'll elaborate a little, and explain what I'm trying to achieve.
I'm creating a parallax scrolling effect, where the foreground scrolls faster to give the impression that the objects are closer to the POV. To that end I'm using "layer_x("Assets_FG_0_2",x*-0.2);" to make the layer scroll faster, which DOES work.
However, with a room/level of a certain size, I need to take the faster scrolling of the foreground into consideration when placing assets within the IDE. That is, when I want a certain foreground asset to be in a certain place, when the player is in a certain spot. So my idea was to place the assets in the IDE where I want them to be when the player is at that exact spot, and spread them out into the x and y direction by code in a way that will anticipate and negate the faster scrolling of the foreground layer.
Of course, my experiments got stuck when I couldn't get the "layer_sprite_x" command to do anything...
 
Last edited by a moderator:
M

mochipon

Guest
*bump*
Does no one have any idea about how to use the layer_sprite_x command properly?
 
That code looks like it should work to me.
Have you checked that you are getting a valid id, use show_debug_message() to check the sprite id.

If the id is valid, try some other functions in it like layer_sprite_change() and see if that works. If it does, either you are setting the sprite position somewhere else back to its original spot, or could be a bug with layer_sprite_x()

I haven't used the function myself yet, not at PC at the moment so can't test right now unfortunately.
 

NightFrost

Member
The problem is trying to refer to a sprite layer element with sprite name, which will not work. When you drag a sprite in IDE to an asset layer, it cannot be referred to with sprite name, because if you drag ten, which one you want? You'll notice that each gets a unique name, the asset name. For example, "graphic_7DBCAD85." This is the name you want to use. The command you're using gives a mention of this:
layer_sprite_get_id(layer_id, asset_name)
layer_id: The unique ID value of the layer to target
asset_name: The unique name of the asset on the layer as defined in the room editor
Emphasis mine. If you find the names cumbersome, you can double-click them on the layer properties list and rename. And, as far as I can tell, YYG has seen fit not to add command to find names of these assets, so you can't figure in code what their asset names are (or search assets by name for that matter). Which you don't really need to, as you can layer_get_all_elements anyway and then act on those whose layer_get_element_type equals to layerelementtype_sprite.

Note that the manual seems to use "asset" and "element" interchangeably, but is always referring to same asset layer elements/assets that wrap the sprites... as far as I can tell. I haven't yet taken a really deep look at the layer system.
 
M

mochipon

Guest
Thanks NightFrost for your answer!
Your explanation really helped me understand how these commands work. In my opinion it would be useful if an explanation like yours was included within the manual.
Perhaps it's just me, but the manual confused me with the descpription of "layer_sprite_get_id", because it says there it returns the "unique ID of the sprite element". That's why I kept wondering 'what if there's more than one sprite on that layer?' It didn't seem to make sense.
In the end, it worked the way I wanted it when using the array solution you suggested.
I didn't have to use layer_sprite_get_id. That makes me wonder what "layer_sprite_get_id" is even used for, or what it was returning...

I'd also tried using the ID of the sprite (when placed in the room) direclty. As in graphic_......, but although the IDE recognizes the ID as you type it in, giving you a list of all the graphic IDs in red letters, afterwards the letters turn blue as if that was just a variable, and the game won't compile because that variable doesn't exist. Something like
Code:
layer_sprite_x(graphic_5C45D4, 300);
apparently won't work, even if that ID exists in the room.
 
Top