Grid Interaction?

0 favourites
  • 4 posts
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • Am just starting to work with C3 and am wondering how to go about setting the interaction of a grid of multiple objects (could be buttons or sprites). Imagine a tic-tac-toe board: how would one collectively set the action of a group of squares to toggle their appearance between X and O? Setting up an action on each object seems inefficient. Is there some way to apply the toggle action to a group of objects, sort of like a class behavior?

    I'm new to C3 so apologies if this info is already explained somewhere.

    Thanks,

    • Scott
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • First, you need to understand the difference between objects and instances of an object. This a mistake that many people make - creating lots of identical objects instead of instances of one object.

    In tic tac toe all cells should be just one sprite object - tttSprite.

    Add 3 frames - one empty, second with "X" image and third with "O", set animation speed to 0.

    Add an instance variable to this sprite, name it "cellState", type text.

    Place 9 instances of this sprite on the layout to form a grid.

    Now when players click tttSprite, you can set its cellState to either "X" or "O" and then update either just this instance or the whole grid using this code:

    tttSprite -> Compare instance variable cellState=""  -> Set animation frame to 0
    
    tttSprite -> Compare instance variable cellState="X"  -> Set animation frame to 1
    
    tttSprite -> Compare instance variable cellState="O"  -> Set animation frame to 2
    [/code:3bwie8gx]
    
    First event will [i]pick [/i]all instances with empty cellState and set frame to 0 for them.
    Second event will pick all instances with cellState="X" and so on.
    
    Picking is also an extremely important concept in Construct 2/3. I suggest you read these tutorials:
    [url=https://www.scirra.com/manual/71/instances]https://www.scirra.com/manual/71/instances[/url]
    [url=https://www.scirra.com/manual/75/how-events-work]https://www.scirra.com/manual/75/how-events-work[/url]
  • Thanks for the example and pointers. I know about the use of instances, but just to confirm my understanding: when manually creating instances, command (or alt) + drag on an object creates an instance of the object, while "clone" creates an entirely new object, yes?

  • Yes, you can see the list of objects on the right bar.

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