1
\$\begingroup\$

I want my 2D sprite to become bigger when moving down the screen (closer to the camera), smaller when walking away and to maintain the same size when moving left or right.

The scaling along the y axis is fine but when I move left the sprite remains facing right. I have tied the y_scale to the x_scale so the sprite retains the correct proportions. Here is the problem:

For example, if the image_yscale is currently 9 and I want to move left, setting the image_xscale to -1 causes the sprite to shrink again. How can I flip the sprite along the x_scale while retaining the current size?

Here is my code:

// Sprite Scaling

if keyboard_check(vk_up)
{
    image_yscale -= 0.01
}

if keyboard_check(vk_down)
{
    image_yscale += 0.01
}

image_xscale = image_yscale

if image_yscale < 1  
{ 
    image_yscale = 1 
}

Thanks for any help in advance.

\$\endgroup\$
  • 1
    \$\begingroup\$ If you have some code that works partially, you should add it here (edit the question), this would probably help point you in the right direction! \$\endgroup\$ – Vaillancourt Sep 30 '16 at 12:44
  • \$\begingroup\$ How about the KISS principle? Why not just switch to a different sprite which is natively larger? Save in the cost of resize computations. \$\endgroup\$ – Krythic Sep 30 '16 at 16:08
  • \$\begingroup\$ I need smooth scaling of the sprite size so it's not really appropriate for this, thanks anyway. \$\endgroup\$ – SBLux Sep 30 '16 at 16:21
  • 1
    \$\begingroup\$ I have rolled back the question to a previous version. Please do not change the scope of the question content as it makes the answers no longer relevant. You can add how you fixed your issue as an answer, which is perfectly acceptable for this site :) A less preferred way to add the answer is to do an edit and clearly mark that's how you fixed your question but leave the question intact :) \$\endgroup\$ – Vaillancourt Sep 30 '16 at 16:57
  • \$\begingroup\$ Also, on modern hardware accelerated rendering scaling doesn't really cost much. Swapping textures might even cost more. \$\endgroup\$ – Tyyppi_77 Sep 30 '16 at 16:58
1
\$\begingroup\$

So I think you want to change the line image_xscale = image_yscale to something like the following:

image_xscale = image_yscale

if keyboard_check(vk_left)
{
    image_xscale *= -1
}

Basically simply setting the x_scale to -1 when the player is walking left won't cut it, because that shrinks the sprite down as you said. As negative scales are considered as flipping the sprite around, this code should do the trick. Basically we use the value from y_scale, but invert it if the player is walking left.

EDIT: To fix the sprite flipping back to facing right when the player releases the left arrow, you need to introduce a boolean value that stores the last direction key held. So change your code to something like this:

image_xscale = image_yscale

if keyboard_check(vk_left)
{
    faceLeft = true    
}
if keyboard_check(vk_right)
{
    faceLeft = false
}

if faceLeft
{
    image_xscale *= -1   
}

Here faceLeft is a member value of your player object (or something similar, I'm not familiar with GameMaker).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That is great and it works while moving the player to the left, but the sprite reverts back to facing right when I am not pressing any keys. Any idea how to fix that? \$\endgroup\$ – SBLux Sep 30 '16 at 15:34
  • \$\begingroup\$ That would be fixed by introducing an additional boolean. I'll edit my answer. \$\endgroup\$ – Tyyppi_77 Sep 30 '16 at 15:41
  • \$\begingroup\$ Brilliant thank you! I have one more question if you are not bored yet, I will update the original post \$\endgroup\$ – SBLux Sep 30 '16 at 16:22
  • \$\begingroup\$ Don't edit your question with the solution. If you have more trouble, you should post a new question, as this question has now been answered. \$\endgroup\$ – Tyyppi_77 Sep 30 '16 at 16:56
0
\$\begingroup\$

In all honest, as much as i love GameMaker, i would not recommend this style of "2d" to be done using GameMaker.

But onto my approach to it!

After much thought, and some tinkering you probably want the character to gradually get bigger? Lets say yes.

So you have something in place for scaling? Good i don't have to re invent the wheel.

So we take your character, maybe use a Platformer physics Based on the broad question, im doing my best here So set up your platformer physics, and have them separate to the scaler. Now as for sprite fliping, your code is probably getting your scale_sprite; and move_object mixed up, so to fix this write a complete new method that does not affect the sprites size, and instead flip the sprite based on direction moved.

Use this;

image_index = 1;

Then every time your player pressed to move in a direction, flip it by using 1, or -1.

This is the way i would do it, but without knowing how you have implemented the scaling, i cant be 100% accurate with my answer.

I hope this helped, just throw a comment if you need any more help.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Ok so this shouldn't affect sprite rotation, do you have any other code that touches or handles the sprite? \$\endgroup\$ – Ryan white Sep 30 '16 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.