Jump to content


Photo

sprite_index in the collision event


  • Please log in to reply
6 replies to this topic

#1 KK Twain

KK Twain

    GMC Member

  • New Member
  • 8 posts

Posted 21 April 2012 - 08:53 PM

First of all, I have GM 8.0 pro, but for various reasons I'm working with GM 7.0 lite.

Anyway, I was programming a simple game since it had been awhile since I used GM, and I ran into an obnoxious bug that I can't kill. I have a collision event where the obj_player contacts a goal of sorts (obj_goal). A variety of things are supposed to happen in this collision, obj_player returns to its starting position, the goal may change its variable, and depending on the goals variable the player may or may not lose a life. All of this works fine. But the goal is also supposed to change sprites during this collision and no matter what I try it just won't work.

So far I have tried using the aforementioned variable to determine in the step event if the sprite_index changes, and I have tried using a similar method in the draw event. In the player object I've changed the variable (during the collision event), and when that didn't work, tried changing the sprite_index itself (using other.sprite_index). I went inside the goal object's collision event and did the same thing (change the variable, didn't work. Change the sprite_index itself, didn't work.)

I've been connected it to a variable because the variable decides whether or not the player loses a life during a collision (which works) and the change in sprites is supposed to alert the player if they're going to lose a life or not (since it's fairly difficult to keep track of mentally which goals are going to kill you.) Which reminds me --there are multiple goals on the screen at the same time, in case that is part of the problem. (All the changing the variable in one does not change the variable in others).

Here's the actual (current) code:

//In the collision event of obj_player
if (other.claim==0)
{
    other.sprite_index=spr_claim;   
    x=288;
    y=416;
    hspeed=0;
    other.claim=1;
}
else if (other.claim==1)
{
    x=288;
    y=416;
    hspeed=0;
    life-=1;
}

So yeah, I'm stumped. What really doesn't make sense to me is why this is the only part of the code that doesn't work, when the rest of the code block does.

Any suggestions would be very much appreciated.

Edited by KK Twain, 22 April 2012 - 04:47 PM.

  • 0

#2 Gamer3D

Gamer3D

    Human* me = this;

  • GMC Member
  • 1815 posts
  • Version:GM:Studio

Posted 22 April 2012 - 06:25 AM

First, let's make your code pretty.
if (other.claim)
  life -= 1;
else {
  other.sprite_index=spr_claim; 
  other.claim=1;
}
x=288;
y=416;
hspeed=0;

I suspect your problem might be other.claim not being 1 or 0. In that case, nothing will happen.
  • 0

#3 KK Twain

KK Twain

    GMC Member

  • New Member
  • 8 posts

Posted 22 April 2012 - 05:01 PM

I suspect your problem might be other.claim not being 1 or 0. In that case, nothing will happen.

Which makes sense, I agree, but I know part of the code does work, because the first time the two objects collide, obj_player returns to the coordinates without losing a life, and in the second collision obj_player does lose a life. And looking at my code before your corrections (I do appreciate the corrections by the way. It was sort of pointless to put the new coordinates in both the if and else) the obj_player wouldn't have gone back to the starting position unless other.claim did equal a 1 or 0. (Plus I checked, and obj_goal is created with claim=0, I don't see anywhere that would change).

I changed the code a little:

other.sprite_index=spr_claim;
if (other.claim==0)
{   
    other.claim=1;
}
else
{
    life-=1;
}
x=288;
y=416;
hspeed=0;

My assumptions with this is that the collision should cause the sprite to change to spr_claim regardless of any variables... but it doesn't. Also in the obj_goal's collision event I wrote:

sprite_index=spr_claim;

Which also doesn't work. I'm almost wondering if the two objects don't completely collide... but everything else works except for the sprite change.

If I start with the sprite_index set to spr_claim, that works. And when I went in and changed spr_claim to a different sprite that I use, that didn't work. So I'm pretty sure the sprite itself isn't the problem.
  • 0

#4 The2Banned2One

The2Banned2One

    BANNED

  • GMC Member
  • 232 posts
  • Version:GM:Studio

Posted 22 April 2012 - 05:58 PM

If I start with the sprite_index set to spr_claim, that works. And when I went in and changed spr_claim to a different sprite that I use, that didn't work. So I'm pretty sure the sprite itself isn't the problem.


Can I see your draw code for that object?
  • 0

#5 KK Twain

KK Twain

    GMC Member

  • New Member
  • 8 posts

Posted 22 April 2012 - 06:52 PM

Can I see your draw code for that object?


For obj_goal?

draw_sprite(sprite_index,-1,x,y);

  • 0

#6 torigara

torigara

    GMC Member

  • GMC Member
  • 6507 posts

Posted 23 April 2012 - 01:17 AM

I'd advise to check two things:
- there is no other resource (e.g. object) that happens to be named spr_claim. Use Scripts > Check Resource Names to examine it.
- make sure there is no instance those are accidentally put on top of each other (so one instance isn't covered by another.)

Then, try to watch the instance's sprite_index in the debug mode to see if it actually changes. If it doesn't seem to change, chances are, its sprite_index is kept setting back to the original one every step in some event.

Edited by torigara, 23 April 2012 - 01:18 AM.

  • 1

#7 KK Twain

KK Twain

    GMC Member

  • New Member
  • 8 posts

Posted 23 April 2012 - 03:24 AM

I'd advise to check two things:
- there is no other resource (e.g. object) that happens to be named spr_claim. Use Scripts > Check Resource Names to examine it.
- make sure there is no instance those are accidentally put on top of each other (so one instance isn't covered by another.)

Then, try to watch the instance's sprite_index in the debug mode to see if it actually changes. If it doesn't seem to change, chances are, its sprite_index is kept setting back to the original one every step in some event.

Thank you! I followed your advice and found the problem. Apparently there were two obj_goal objects right on top of each other. So the collision only change the sprite_index of the one on bottom. I didn't notice this before because one set of goals were set up in room0, and the other set was created after the game started. (And it took running the program in debug mode and going "heeey... why are there 10 goal objects instead of 5?" to notice this. Gah.)

To everyone who posted: thank you so much for the help! I really appreciate it.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users