Help with understanding functions

Get help using Construct 2

Post » Thu Jul 11, 2013 10:18 am

Hey everyone!

This will be my first post and I'm very excited about creating my own game. I have some previous JavaScript experience but it's been a while. Anyway, at the moment I'm trying to understand how functions work in C2 but i can't seem to get a grip on some things.

Below you will find an image of my current test. Basically what I'm trying to do is to have a toggle function. When an object is clicked, it will call a function which will fade in another object (an image, some text or whatever). If the object already is visible, i want it to fade out... easy right?



So my problem is this. I want to pass three values into the function, the target object, the fade in time and the fade out time. These should set the the target objects behavior fields to those values. So in order to get it to work i have been struggling to understand how the functions work in C2.

If i have a function object and set its name as fadeToggle, is this the same as:

function fadeToggle(targetObj, in, out)
{
    // set fade in and out values
}


OR

var fadeToggle = function(targetObj, in, out)
{
    // set fade in and out values
}


Aren't these then set in GLOBAL scope? What i want to do is have something similar to this:

var gameObject = {
    fadeInTime: 0,
    fadeOutTime: 0,
    fadeToggle: function(targetObj, in, out) {
          // set fade in and out values
    }
};


Anyway, after trying for a while i can get an object to appear when i click a button and then disappear when i click it again, but not with proper fading. It fades in and out alright. But then when i click it again nothing happens. Both are set as "restart fade".hdnine2013-07-11 10:21:57
B
7
S
2
Posts: 38
Reputation: 555

Post » Thu Jul 11, 2013 11:00 am

Hey @hdnine, and welcome around here !

First of all, having a background in JS too, I went through the same at first. The first thing you shouldn't do is try to find a direct link between how you would write it in JS and in C2. It's not the same and you won't be able to do it like this.

My first tip on your problem is : you cannot change a fade time during runtime. When you create your objects, you have to add a behavior Fade on it, and set its time to fade in and out from the editor. If you want to have a fade in AND a fade out, but not at the same time, not triggered together, just add 2 behaviors Fade to your object and set them to respectively fade In and fade Out. You can rename behaviors too, to make it more readable.

Then, you will be able to write a function "ToggleFade". Here is a sample capx to show how I would do it : capx
B
17
S
8
G
4
Posts: 461
Reputation: 6,127


Return to How do I....?

Who is online

Users browsing this forum: timoteomorelli and 1 guest