How do I compare frames of two different object instances?

  • I want to create an event that compares frames of two different instances of a same sprite and detects when there's a match. For example compare frames of the two instances that are on screen and see if they are the same.

    Do you think it's possible to do that?

  • maybe use for loop (for all the possible variants)

    [quote:21kjielb]

    System | For "loop" from 0 to [maximumframe]

    System | Pick Sprite where anim.animationFrame = loopindex

    System | Sprite.PickedCount >= 2

    >> //Insert Code here

    for each frame number,

    it will pick all the sprites that have this frame playing.

    if two or more Sprites are picked, the code will be runned.

    All the Sprites you need are already picked.

  • It should be something like

    Event: Object is on screen

    other condition: if instance1.frame = instance2.frame

    do this

  • they're two instances of the same sprite.

  • Yes, that's fine. The index deals with that. One small correction, compare Sprite(0) to Sprite(1).

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Looks interesting but what if I have like 10 instances and I want to detect a frame match of let's say two instances overlapping? Is there a way to tell the program that I want to check only the two instances that are overlapping?

  • Do 'Is overlapping" first.

  • The problem with using index (Sprite(0).AnimationFrame=Sprite(1).AnimationFrame) is that it compares first 2 instances on the layout, not the 2 picked instances.

    It's fine if you only have 2 instances in total, but if there are more instances, then this code will not work correctly:

    if Sprite is overlapping Sprite  // picked 2 overlapping instances
      if Sprite(0).AnimationFrame=Sprite(1).AnimationFrame   // comparing different instances, wrong!
    [/code:1c4jwd0g]
    
    You'll need to do something like this:
    [code:1c4jwd0g]
    local variable f=0
    if Sprite is overlapping Sprite  // picked 2 overlapping instances
      System Pick Sprite instance 0  // select first of the picked instances   
        f = Sprite.AnimationFrame     //  save its frame number in variable f
      System Pick Sprite instance 1       // select seconds of the picked instances
         if Sprite.AnimationFrame=f       // compare their frames
    [/code:1c4jwd0g]
    
    There is a trick you can use to be able to pick 2 instances of the same object in one event and work with them independently - create a family and add your sprite object to it.
    So you'll have Sprite and FamilySprite.
    Then you can do the whole thing much easier:
    
    [code:1c4jwd0g]
    if Sprite is overlapping FamilySprite    
         if Sprite.AnimationFrame=FamilySprite.AnimationFrame       
    [/code:1c4jwd0g]
  • Um, what's wrong with using filtering?

    object.frame=f

    -object is overlapping object, do fo

  • newt you are right, I got a bit carried away

    EDIT: actually, no, you are not right.

    The task was to find 2 sprites that have matching frames, not to find 2 sprites with frame=f (some fixed number).

    And still, the trick with using a family to refer to another instance of the same object can help in many different situations.

  • Well I really wasn't talking about that, and you may even need that in certain circumstances.

    I was just pointing out that the picking system works to make things less complicated.

    Also, yes this is assuming you know the frame beforehand, which is quite common for a matching game.

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