How do I set functions with dynamic objects parameters

Just started using Construct 2? Post your questions here

Post » Thu Feb 19, 2015 3:42 pm

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
B
4
Posts: 5
Reputation: 210

Post » Thu Feb 19, 2015 3:52 pm

You want to use a Family to abstract your object types. Then you can do this on the Family object.
ImageImageImage
B
70
S
19
G
156
Posts: 3,393
Reputation: 90,897

Post » Thu Feb 19, 2015 4:41 pm

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 :

Code: Select all
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;
}
}

Thanks for your help !
Rosedestemps
B
4
Posts: 5
Reputation: 210

Post » Thu Feb 19, 2015 5:17 pm

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.
You do not have the required permissions to view the files attached to this post.
ImageImageImage
B
70
S
19
G
156
Posts: 3,393
Reputation: 90,897

Post » Thu Feb 19, 2015 5:53 pm

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
B
4
Posts: 5
Reputation: 210


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests