www.webdeveloper.com
Results 1 to 4 of 4

Thread: Running two instances of the same function

  1. #1
    Join Date
    Mar 2014
    Posts
    1

    Running two instances of the same function

    Hello!

    I'm pretty new with javascript so sorry if this is a stupid question.

    I have made a class. In this class i have added a Prototyped function "Game.prototype.ShowMessage"

    Im calling this function from another prototyped function like this (Doing it twice)

    Code:
    this.ShowMessage(["Loading game", "Please wait..."]);
    this.ShowMessage(["Loading game", "Please wait...","Loading gameLONGER", "Please wait...LONGER"],true);
    Now i'm expecting to see two messages where one them will show twice as long as the first one called. But it seems like javascript is running in some kinda "sharing mode" which then suspends both functions when the first one is complete.

    It works fine running these until the first call is done, then the other second call stops aswell.

    Here is the function i'm trying to run twice (Note its the return false line which breaks both function. i tried to remove the return but it broke anyways).

    Code:
     ///TODO CANNOT RUN "CONCURRENT" WITH MULTIPLE MESSAGES :/
            /// Shows a specefied message on the middle of the screen (Blinking), Also have a forever option which can be TRUE OR FALSE(default), IF true use callback to stop. 
            Game.prototype.ShowMessage = function (messageArray, forever, callback) {
                if (typeof forever == 'undefined') forever = false;
    
                // Since javascript cannot work in ??paralell?? We have to create a queue
    
    
                var flashText = new Pixi.Text("",
               {
                   font: "70px Arial",
                   fill: "white",
                   stroke: '#000000',
                   strokeThickness: 4
               });
                flashText.anchor = { x: 0.5, y: 0.5 };
                flashText.position.x = Conf.Data.width / 2;
                flashText.position.y = Conf.Data.height / 2;
                flashText.alpha = 0;
                flashText.forever = true;
                flashText.message = messageArray;
                flashText.tpmcallback = callback;
                flashText.countTo = messageArray.length;
                this.getGroup("Environment").addChild(flashText);
    
    
                position = { alpha: flashText.alpha };
    
                var tween = new TWEEN.Tween(position)
                    .to({ alpha: 1 }, 2000)
                    .easing(TWEEN.Easing.Elastic.InOut)
                    .onUpdate(tweenUpdate)
    
                var tweenBack = new TWEEN.Tween(position)
                    .to({ alpha: 0 }, 2000)
                    .easing(TWEEN.Easing.Elastic.InOut)
                    .onUpdate(tweenUpdate)
                    .onComplete(tweenComplete);
    
    
                tween.chain(tweenBack);
                tweenBack.chain(tween);
    
                function tweenUpdate() {
                    flashText.alpha = position.alpha;
                }
    
                function tweenComplete() {
                    if (messageArray.length == 0) {
                        tween.stop();
                        return false
                    }
    
    
                    if (messageArray.length > 0) {
                        var msg = messageArray.shift();
                        flashText.setText(msg);
                    }
    
    
                    if (forever == true) {
                        messageArray.push(msg);
                        callback(tween); //May be used to call tween.stop();
                    }
                }
    
    
                tween.start();
            }
    Here i have a callback which is runned every time a "tween" is done. It checks if the "Forever" flag is set and if it is it pushes the Sentance which is in the input array back into the array. what this means is that this function should in theory NEVER stop. but instead it breaks when the other one breaks.

    Going deeper: i'm talking about this snippet of code:
    Code:
    function tweenComplete() {
                    if (messageArray.length == 0) {
                        tween.stop();
                        return false
                    }
    
    
                    if (messageArray.length > 0) {
                        var msg = messageArray.shift();
                        flashText.setText(msg);
                    }
    
    
                    if (forever == true) {
                        messageArray.push(msg);
                        callback(tween); //May be used to call tween.stop();
                    }
                }

    Can anyone see what that may be the problem here?
    What should i do, not do to make these two functions run separate from each other?

    Thanks
    Driiper

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    248
    **SPAM** caused by the inability to delete previous reply! :P
    Last edited by tech_soul8; 03-13-2014 at 04:19 AM.

  3. #3
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    248
    I don't completely understand what is that you're trying to do exactly?

    Maybe something like this?

    Code:
          function Test() {
             this.forever;
          } 
          Test.prototype.showMsg = function(a,forever) {
             var i = j = 0;
             this.forever = forever;
             
             if (this.forever) {
                while(j <= 100) {
                   for (i; i < a.length; i++) console.log(a[i]);
                   i = 0;
                   j++;
                }
             } else {
                for (i; i < a.length; i++) alert(a[i]);
             }
          }
          
          var o = o2 = new Test(), a = ["Loading game!!!", "Please wait..."];
          o.showMsg(a,true);
          o2.showMsg(a);
    You have two instances of the Test class. One instance invokes the showMsg function and logs the results 100 times (didn't want to create an infinite loop). Other instace of the Test class uses the same showMsg function but alerts only two messages.

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    248
    Quote Originally Posted by driiper View Post
    Im calling this function from another prototyped function like this (Doing it twice)

    Code:
    this.ShowMessage(["Loading game", "Please wait..."]);
    this.ShowMessage(["Loading game", "Please wait...","Loading gameLONGER", "Please wait...LONGER"],true);
    Which one prototyped function? To be able to do this, you have to know what's the value of the this keyword in various situations. You'll also have to know how exactly inheritance works in JavaScript.

    For example: If you're trying to call the ShowMessage function from another instance (object) which doesn't inherit from Game.prototype then that instace doesn't have the ShowMessage function and you cannot invoke it like that.

    Also note that the problem may be somewhere in your TWEEN and Pixi constructors and since you didn't provide the entire code we cannot see all the properties initialized by those two constructor functions.

    One more thing to watch out for:

    Code:
    var tween = new TWEEN.Tween(position)
                    .to({ alpha: 1 }, 2000)
                    .easing(TWEEN.Easing.Elastic.InOut)
                    .onUpdate(tweenUpdate)
    You have to be careful when forgoing the use of semicolon at the end of statement. If you're not sure how JavaScript will parse the code you should include semicolon at the conclusion of each of your statements.
    Last edited by tech_soul8; 03-13-2014 at 05:44 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles