[NEWBIE, PLEASE HELP] How to change sprites on collision

J

Jadie

Guest
Hi,

I am new to Gamemaker and I am making my first 8Bit pixel art game. I am trying to get my player to change sprite animation when collecting an item. I have tried to use a variable to do this in the collision event for the player and the object its is colliding with, but nothing is changing. Please see below!

Thanks,

Jadie

Code:
//STEP EVENT TO MAKE THE PLAYER MOVE //

playerSpeed = 2;
playerState = 1;



MOVELEFT = keyboard_check(vk_left);
MOVERIGHT= keyboard_check(vk_right);
MOVEUP = keyboard_check(vk_up);
MOVEDOWN = keyboard_check(vk_down);
NOBUTTON = keyboard_check(vk_nokey);




if (MOVELEFT){
    playerState = 1;
    x -= playerSpeed;
    image_xscale = -2;
}

if (MOVERIGHT){
    playerState = 1;
    x += playerSpeed;
    image_xscale = +2;
}

if (MOVEUP){
    
    sprite_index = spr_up;
    y -= playerSpeed;
}

if (MOVEDOWN){
    
    sprite_index = spr_down;
    y += playerSpeed;
}

if (NOBUTTON){
    sprite_index = spr_standing;
}

with (obj_eardef){
playerState = 2;
}


// PLAYER COLLISION EVENT WITH OTHER OBJECT//

playerState = 1;

if playerState = 1 { sprite_index = spr_walking;
}

if (playerState == 2){
    sprite_index = spr_earwear;
}
 
A

Alex_Beach

Guest
You should have 2 separate collision events if you are colliding with 2 different objects. In the first one, all you need is: sprite_index=spr_walking. In the second event: sprite_index=spr_earwear. With your variable you set up, it's set to 1 before the if statements, so you will only get the walking sprite
 
J

Jadie

Guest
Hi Alex,

when you say I need 2 collision objects, does that mean that i don't need the step event? I basically only want a default walk and then then they pic up the headphones, the sprite is wearing them when they walk.

Sorry for extra questions, I am super new to this, but appreciate the help!

Thanks,

Jadie
 
K

Khanon

Guest
Hi, Some info are missing there ...

It's about yours earing/walking/standing sprites ...
When you make a fixed sprite or an animated sequence of sprites,
like any products of programmation : you have to make choices.
for the [phone] equiped sprite, you have two way of doing things.
1st method >>> draw sprite 2 directions + draw sprite 2 directions with phone equiped >>> total : 4 sprites
with this method, the hero character will be self.versionning his sprite.

2nd method >>> draw sprite 2 directions + draw subsprite 2 directions >>> total : 4 sprites
with this method, the hero character will host only his own animation (regardless of the equipment)
another item will host his own animation too (equipment only)
You can guess between the two methods ... your choice and your affinity with the code will be impacted.
If you can precise wich method you want to exploit, it will help too for helping you.
 
Last edited by a moderator:

Miradur

Member
Hi, here is a piece of modified code from YoYo-Dungeon:

Code:
In the collision-event of "obj_eardef"        // collision with Player
global.player_run = true

In the collision-event of "other-objects"    // collision with Player
global.player_run = false


Player-Events

Create-Event
global.player_run = false


if( keyboard_check(vk_left)){
    if( global.player_run ) {
        sprite_index = spr_earwear;        // we run
        x -= playerSpeed;
        image_xscale = -2;
        image_index = 0;                // if you need
        image_speed = 1.0;                // if you need
    } else {
        sprite_index = spr_walking        // we walk
        x -= playerSpeed;
        image_xscale = -2;
        image_index = 0;                // if you need
        image_speed = 1.0;                // if you need
    }
}

... and so on

Miradur
 
Top