How do I swap values?

0 favourites
  • 11 posts
From the Asset Store
Full game Construct 2 capx code source to post on Google Play
  • I have sprite object "box" and I placed its instances in grid like image below. "box" has an instance variable and each box contains a value in its variable. what I want to do is when I click any box then I want to swap value in all boxes like this 1=2, 3=4, 5=7 I want to swap only edge box values. How can I do this?


  • Can't see any image but I guess it's a simple condition, on clicked, if box.variable=1 set box.variable to 2 ?

  • lionz see image now. its not simple because grid positions and number of grids are changeable. I don't want to calculate it manually.

  • How are you placing or generating your box positions in the first place?

    It would be most straightforward in a 1d array. You can increment the index in a loop until you run into an empty cell, or 0. Subtract 1 from that index and you'll have the position of the last number in a given set, which you can then swap with whatever you started with. The same loop can continue iterating through the entire array to swap every set of numbers it finds.

  • oosyrag grids are manually placed on layout. on different layouts there will be different grids pattern

  • If the grids are placed manually beforehand on all layouts then it is simple to set the instance variables on the boxes that need swapping.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • can it be done through events. there are many grids in single layout and there are many layouts.

  • I was able to get the each edge box through loop. For this I created another sprite and for each loop I placed it to box.bboxLeft. On sprite overlap box and box.picked count = 1 I got the left Edge. same for Right. Now I got all the edges but I want to swap values of edge boxes.

  • lionz oosyrag

    this file finds edge box.

  • So you need swap logic? You should also give the boxes some kind of ID or like an initial order number. When you select the first box, you can store its value temporarily to a local variable and also save the ID of the box. Then when you find the end box, set that box's value to the local variable value, then set a local variable to the end box's value, pick the first box based on that stored ID and set that box's value to the second local variable's value. Or you can do similar logic but with functions and pass through picking values as parameters.

  • thanks lionz

    I had tried everything you said except one thing I was not saving box id. Now I its working.

    Thanks again

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