How do I click the same object again and again and have it do different things each time?

0 favourites
  • 7 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hello friends,

    I have a sprite object, and each time I click on it I want it to call a function, or more specifically to cycle through a series of functions that all have the same name, with an increasing number on the end. I'm using C3 full version with built-in functions.

    SCENARIO

    1. I have a series of functions - funct_01, funct_02, etc., up to funct_10

    2. There is a sprite which is the trigger object. The first time I click on it, it calls funct_01.

    3. I would like each successive click to call the next function - funct_02, then funct__03, etc.

    4. After funct_10 it would loop back to funct_01 and continue the cycle.

    I know this is a specific case, but I am also interested to learn about this behavior in general. Say I wanted to click a text object over and over, and have it do totally different things each time (say, first click spawned an object, next click changed the font color, next click destroyed the text). I don't understand the basic concept.

    Many thanks for any assistance.

  • You could have one function and conditions within the function based on a parameter. You pass a different number through the function each time and in the function the conditions look like if param=1 do this, if param=2 do this.

    If the functions are vastly different and a large number of events then you can call each function based on an instance variable of the object using similar conditions, if object.var=1 run function 1, if object.var=2 run function 2.

  • You'll want something to keep track of the "state" of an object, in this case how many times an object has been clicked. This is usually done in an instance variable. Every time it is clicked or activated, increment the variable.

    On object clicked, set object.clickedcount to object.clickedcount+1

    If object.clicked count > lastoption, set object.clickedcount to 0

    You'll then refer to this variable when deciding what action to take, or you can pass it to a function as a parameter as lionz suggested.

    An advanced trick is to use a conditional expression to loop a counter and reset it to 0, which results in the same as above but within a single event.

    On object clicked, set object.clickedcount to object.clickedcount+1>lastoption?0:object.clickedcount+1

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks very much to both of you. I got it working they way you see here. A bit of a hybrid, but it's using the click count, so that's a great new tool for me to have learned.

    Since the fundamental change I want to make is so simple (replacing some numbers), I shifted away from using a function for that.

    I'm making a simple arithmetic engine for my students, for visualization. They choose a first number (from a row with ten number choices), an operator and a second number, and it produces the result. This behavior, clicking an icon over and over again, is to allow them to change the sets of ten number choices for the first and second numbers in the equation. They repeatedly click a button and it presents them with new sets of ten numbers to use in the engine.

  • I learned so much doing this. Very happy!

  • I read this story over and over and I still don't understand it, you're describing too many things we can't see. What do you need help with?

  • I read this story over and over and I still don't understand it, you're describing too many things we can't see. What do you need help with?

    Hi lionz, sorry, the first part got answered successfully, and the bit that I said I still could use help with, I made a separate post and that was successful too.

    The basic question was how do I click on something repeatedly and have it do something different each time, which was resolved by using the click count and making the click count a condition.

    Thanks for responding.

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