Help with understanding functions

0 favourites
  • 2 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • 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?

    <img src="http://i.imgur.com/I5d7fu7.png" border="0" />

    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".

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

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