1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GM:S 1.4 how to switch sprite_index but not forever

Discussion in 'Programming' started by SonicTheHedgehog+123, Feb 14, 2020.

  1. SonicTheHedgehog+123

    SonicTheHedgehog+123 Member

    Joined:
    Feb 9, 2020
    Posts:
    33
    The Problem that I have is that when I am switching from my player_sprite to my ducking sprite. I cant change to another sprite because the code is in the step event.
    Here is the ducking code:

    if keyboard_check(ord("S"))
    {
    sprite_index = Ducken
    }
    else
    {
    sprite_index = Fledermaus_Bild
    }
    if sprite_index = Ducken
    {
    hspeed = 0
    }


    is there a way that my sprite goes from ducking to player sprite and I still can switch to other sprites (like an item_sprite)

    Open for any help:D
     
  2. Jordan Robinson

    Jordan Robinson Member

    Joined:
    Jun 22, 2016
    Posts:
    94
    After you set the player sprite, you can just add more if else statements for the other sprites you wish to change to
     
  3. SonicTheHedgehog+123

    SonicTheHedgehog+123 Member

    Joined:
    Feb 9, 2020
    Posts:
    33
    Thank you for replying
    It didnt work.
    I have multiple items in my game and whenever I am touching one it should change to the item sprite.
    However the sprite changes for an instant to the item sprite and then goes again to the player sprite.
    Why does the duck code have priority over the item code?
    I did watch shaun spaldings video about power ups. Here is the code of one of my power ups:
    Code:
    //PowerUp collision with player
    with (Fledermaus)
    {
    room_speed = 30;
    sprite_index = Fledermaus_PowerUp;
    image_speed = 1/3;
    alarm[4] = 300;
    }
    instance_destroy()[/B]
     
  4. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    1,087
    It doesn't have priority, you're just not seeing the logic behind the code. The ducking code, being in the Step event, runs every frame, so as long as you're holding S, the sprite is set to duck. If you collide with the PowerUp, the player's sprite index changes, and then the PowerUp is immediately destroyed. Now, with it no longer existing to continually run that collision event, on the next frame, there is no event to execute, but the player's Step event is naturally still running. S is still held down, and the sprite changes.

    You have to really think about your whole sprite structure from the ground up, factoring in whatever external influences that may exist.
     
  5. SonicTheHedgehog+123

    SonicTheHedgehog+123 Member

    Joined:
    Feb 9, 2020
    Posts:
    33
    Now I understand. I really must learn the algorithm of GML.
    That also means that the sprite should only change if the alarm reaches zero.
    I will find a solution to this.Thank you for explaining my mistake:D
     

Share This Page