How do I set functions with dynamic objects parameters

  • Hi guys,

    I'm trying to make function with several parameters to optimize my code created with Construct2 interface, but I don't understand how it works properly...

    For example, I've 10 different sprites objects which have the same behaviour with different settings like :

      each time one of this 10 objects is clicked, this object is alpha 10, 30 or 50%. if this object is 10% or 30% alpha, play animation A, else, play animation B.

    On my point of view, I can obviously copy and paste with the interface the same code for each object like :

    object1--> is clicked --> alpha 10% or alpha 30% & play animationB1 --> else --> play animationA1

    object2 --> is clicked --> alpha 10% or alpha 30% & play animationB2 --> else --> play animationA2

    object3--> is clicked --> alpha 50% play animationA3...

    but that isn't optimized.

    So, I would like to create a function with the interface like :

    function detectAction(objectSelected, animationSelected, normalAnimation) {

    with this [this is the objectSelected parameter of the function and can be set dynamically, for example with a object list coming from a table]

    if this --> alpha == 10 or alpha==30

    play AnimationSelected

    else

    play normalAnimation

    }

    so I would like the function execute the code for an equivalent of "this" where this is dynamical.

    Like that, I can reuse the function in several case, in for loop...

    but I don't understand how to call an object name dynamically and I don't see any "this" equivalent.

    Could you help me ?

    Many thanks,

    Rosedestemps

  • You want to use a Family to abstract your object types. Then you can do this on the Family object.

  • Hi blackhornet,

    thanks for your reply.

    I had in mind that families will only keep all the objects in the same time ?

    How is that possible to detect a used object coming from family ? with UID ?

    if I had to develop what I would like to do with another language, I would have created something like :

    var activeObject:Number;
    myObjectsTable=[object1, object2, object3];
    myAlphaTable=[10, 30, 50];
    
    function detectAction(objectSelected, animationSelected, normalAnimation) {
    for (i=0, mytable.length, i++){
    activeObject=i;
    if (objectSelected.clicked == myObjectsTable [activeObject]){
    objectSelected.alpha=myAlphaTable[activeObject]; 
    if(objectSelected.alpha == 10 || objectSelected.alpha == 30){
    play AnimationSelected; 
    } else {
    play normalAnimation;
    }
    }
    [/code:2xj6yipy]
    Thanks for your help !
    Rosedestemps
  • You don't even need a function, if you don't need it for other conditions. In the "On touched Family" trigger, the item is already picked. Just do what you want with it. Otherwise, you would pass the UID to a function, and you'd have to pick it again.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi blackhornet,

    many thanks, that's what I'm looking for

    I had in mind it will have been much harder to do !

    Cheers,

    Rosedestemps

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