how to differentiate between two instances of the same sprite?

0 favourites
  • 11 posts
From the Asset Store
This is a Dungeon Master tool & the 1st of 12 Combat Engines from the Building Combat Engines for Browser Games workshop
  • I'm making a craft game.

    Basically, when a sprite collides with another sprite, a new sprite is created.

    All elements of the crafting game are stored as animation-frames of a single sprite.

    Thing is who collisions with who is relevant :

    - water poored on fire = coal

    - fire thrown on water = steam.

    So I need to know which sprite is active and which one is passive.

    So far, I have a sprite 'Element' and a family made of this unique sprite called 'Elements-fam'.

    When I check:

    - 'Element': on collision with "Elements-fam"

    Am I sure that Element is the active one and Elements-fam the passive one ?

    Note that :

    - passive sprite can be in motion

    - after collision, passive sprite can be considered as an active sprite if collision with active sprite sets it in motion. A chain reaction may occur

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • - water poored on fire = coal

    - fire thrown on water = steam.

    How do you know which item in the pair is thrown on another item? If you use Drag and Drop behavior, then in "On Drop" trigger you will have your active instance picked.

    Element On Drop <- this Element instance is active
    Element is overlapping Elements-Fam <- this Elements-Fam instance is passive
    

    Is this what you are asking, or did I misunderstand the question?

  • Actually, sprites are dropped from the top of the screen and pile at the bottom. So when falling sprite collides with another, its dragndrop behaviour is disabled since drop had occured. And more, when a sprite falls, it can shake the whole pile and create a chain reaction of collisions.

    So we can assume that maybe, active sprite is the one that has motion and passive the one that has not (sprite has physics behaviour). And if both are in motion, active one is the one with the highest speed

  • Ok, but I imagine the logic is still the same - the first event picks the active instance. The sub-event picks the passive (overlapping) instance.

  • Actually, since dragndrop is disabled on active sprite, I only have one event left : on collision

    Logic should consider two equivalent sprites that differ only by their velocity. Active sprite is the one with higher speed.

    What about something like :

    - when instance A of Sprite collides with instance B of Sprite

    - Compare instance A speed to instance B speed.

    - If instance A speed > instance B speed -> destroy both sprites and create instance Sprite C with animation frame 1

    - If instance A speed < instance B speed -> destroy both sprites and create instance Sprite C with animation frame 2

    result of speed comparison is not a problem but I have a hard time creating the mechanics to set this comparison up.

  • You can create a function Craft, which will take two arguments - active element and passive element.

    On collision, compare the speed:

    If A.speed>B.speed, then call Craft(A.animationName, B.animationName)

    If A.speed<B.speed, then call Craft(B.animationName, A.animationName)

  • How do I compare speed ? I found this formula (in one of your post I guess), will it do ?

    Speed = distance(0, 0, object.physics.VelocityX, object.physics.VelocityY)

    And most of all, how do I consider the two colliding sprite instances among all the instances that are in the game ? So far, I used the trick of having a sprite and a family with only this sprite but I guess there's a smarter way to achieve this.

  • I don't know of a smarter way.

    Use "Sprite On Collision with Family" event. Inside it compare their speeds using the distance() expression. Based on that - spawn a new element.

  • I didn't manage to have it work with speed comparaison so I tried something to understand how Construct considers sprites and families.

    I have a sprite Token and an family Ball made only with a Token sprite.

    In this example, Token falls from the top of layout when clicked on. When it collides with another Token, test is :

    -Token on collision with Ball

    i made a test to see how Construct considers the sprites, as Token or Ball and then write either state on the sprite.

    Most of the time, faling sprite is considered as Ball and motionless sprite as Token.

    But it appears it is not that simple, because sometimes it can be the other way or even two Token or balls...

    File is here :

    dropbox.com/scl/fi/dhbsl4so4nob83ynz9rxw/jeu.c3p

  • Yeah, you never know which of the two instances will trigger the collision. That's why I suggested to use a function.

    You compare the speeds, and based on the result call the function using either instance A or B as the "active" one.

  • OK, I got it working. But result can make the game very very difficult this way.

    Sprites react differently to each other, depending on their speed.

    Thus, A moving on B stationary gives C

    and B moving on A stationary gives D

    This is clear when aiming at A with B, but when A and B are stacked in the game and a sprite is thrown onto the stack, A and B have 2 ways of reacting depending on the speed they take with the shock.

    This is unpredictable for the player and can be a nightmare when it comes to strategizing.

    I'm still experimenting because it's interesting.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)