• 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!

Legacy GM how do i change sprite subimage upon collision

T

themaniac26

Guest
so I want to be able to change the subimage of the player sprite upon colliding with another object, and also how would I set it up so another object would get launched somewhere in the room. So basically, I want the player to visually lose a part of itself, if it hits an enemy and launch that part off to somewhere in the room and have it so the player can pick up the part to heal up
 

Tsa05

Member
Well, you just do it:

Collision event:
image_index = #

But, if your image_speed is not set to zero, then the subimage is going to change every step, so you might need to set that in your create event.

As for creating the part that shoots off into the room, you'll need to define a new kind of object for that, which has a sprite that looks like the part you want, and which increases hp and goes away when the player touches it.
There's a drag and drop action, and also code, which creates a new object, and you'd put it into the collision event:
instance_create(x,y, obj_name)

You can set properties for the new object like this:
nnn = instance_create(x,y, obj_name);
nnn.speed = 8;
nnn.direction = random(360);
nnn.friction = random(.1)
nnn.image_index = 0;

You see, by storing the "id" of the created part into the variable "nnn", you can then add values to the object to make it move and stuff.
 
T

themaniac26

Guest
Well, you just do it:

Collision event:
image_index = #

But, if your image_speed is not set to zero, then the subimage is going to change every step, so you might need to set that in your create event.

As for creating the part that shoots off into the room, you'll need to define a new kind of object for that, which has a sprite that looks like the part you want, and which increases hp and goes away when the player touches it.
There's a drag and drop action, and also code, which creates a new object, and you'd put it into the collision event:
instance_create(x,y, obj_name)

You can set properties for the new object like this:
nnn = instance_create(x,y, obj_name);
nnn.speed = 8;
nnn.direction = random(360);
nnn.friction = random(.1)
nnn.image_index = 0;

You see, by storing the "id" of the created part into the variable "nnn", you can then add values to the object to make it move and stuff.
thanks also how would I make it so it would randomly choose which subimage for the object that gets launched off into the room, nevermind I figured out how to make it randomly choose which subimage

EDIT: also with it, when I collide with the enemy object a lot of the playerpart objects are spawned, how would I make it so its limited to say 1 is spawned and launched every 3 seconds
 
Last edited by a moderator:

Let's Clone

Member
EDIT: also with it, when I collide with the enemy object a lot of the playerpart objects are spawned, how would I make it so its limited to say 1 is spawned and launched every 3 seconds
Can we see the code you're using to do this? It's a bit tough to give you solid advice without seeing what you have so far.
 
T

themaniac26

Guest
Can we see the code you're using to do this? It's a bit tough to give you solid advice without seeing what you have so far.
here
Code:
/* death */
enemyhitsplayer = instance_place(x,y,enemy_obj)

  
if instance_exists(enemyhitsplayer) {

  image_index = 0
  
  playerpart = instance_create(x,y,playerparts_obj)
  playerpartlaunchspeed = 600 / room_speed
  playerpart.speed = playerpartlaunchspeed
  playerpart.direction = random(360);
  playerpart.friction = random(.1)
  playerpart.image_index = random(4);
  
}
/* sprite subimage animation speed */
image_speed = 0
also I cant get it to change the subimage for the player object
 

Let's Clone

Member
Sorry for the delayed response, I haven't logged in for quite some time... I hope you've resolved the issue by now, but just in case.
This can be done in many ways (which is why I'm so fond of programming =P). I'm not sure what you have that is being separated from the player, but I imagine that you're either drawing a sprite both with and without said piece, or you're rendering them as separate overlapping objects/sprites. The latter would be my recommendation. It would make it pretty simple to just tell that particular object to go flying, and can also listen for the players attempt to recover it.
If you're not handling them separately, then I think you'd only have to create the new object and give it the appropriate behavior at the instance of creation. Regardless, the player and said object shouldn't really need to communicate much, at least not initially.

The problem with the subimage may be because you're changine the image_speed to 0, but you have yet to change the image_index. The three variables that you will likely be missing with will be:
image_speed - speed at which the sprite animates.
image_index - which subimage of the sprite is currently displayed (this is what you're looking for)
image_number - index of the last subimage of the animation (remember to use image_number-1 to handle the offset of counting from 0)
and sprite_index - which is just the sprite that the object is drawing from.

Hope this helped, and sorry if it didn't =P
 
Top