Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,007
    Thanks
    59
    Thanked 10 Times in 10 Posts

    returning out the parent function from a foreach function

    This is typescript but works similarly to JS.

    How can I get the main function hasEnemyLeftBounds to return true. In the for each it currently only leaves the current for each


    Code:
       hasEnemyLeftBounds(): bool {
            this.enemies.forEach(function (enemy: GameObjects.GameObjects.Enemy) {
                if ((enemy.x <= 0 || enemy.x >= Game.CANVAS_WIDTH)) {
                    return true;
                }
            });
            return false;
        }


  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Presumably it should return true if any one of the enemies has left? In JS:

    Code:
    hasEnemyLeftBounds(): bool {
            var hasLeft = false; // TypeScript: bool hasLeft = false; ??
            this.enemies.forEach(function (enemy: GameObjects.GameObjects.Enemy) {
                if ((enemy.x <= 0 || enemy.x >= Game.CANVAS_WIDTH)) {
                    hasLeft = true;
                    // has TypeScript a 'break' statement to break out of the forEach?
                }
            });
            return hasLeft;
        }
    Currently, it will continue searching even if the current enemy has left. You might investigate to see if 'break', or similar, will break out of the forEach loop; there's no point checking the rest. Added: Yeah, a 2 second search tells me that you can just add break; to break out of the loop. Added Again: Actually, I don't think you can(?).
    Last edited by AndrewGSW; 01-05-2013 at 07:51 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    var wibble: bool; // TypeScript
    wibble = false;
    Seems a bit unnecessary to me
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #4
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,007
    Thanks
    59
    Thanked 10 Times in 10 Posts
    seems I should just use a for (int i" instead for simplicity


  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by nikos101 View Post
    seems I should just use a for (int i" instead for simplicity
    Unless you are unfortunate enough to have hundreds of enemies then it is not a big deal. But, yes, the forEach is designed to look at each/EVERY item, so it does not have an inbuilt method (or simple way) to break out of the sequence.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    This TypeScript/JS/Array business is confusing me a little, but if this.enemies is an array then you might be able to do

    Code:
    for (index in this.enemies) {
        if (this.enemies[index].x < 0 || etc..) {
            hasLeft = true;
            break;
        }
    }
    return hasLeft;
    Last edited by AndrewGSW; 01-05-2013 at 08:17 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,380
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Code:
    function hasEnemyLeftBounds(enemies) {
           return enemies.every( function(enemy) {
                    return enemy.x <= 0 || enemy.x >= Game.CANVAS_WIDTH ;
            } );
    }
    where enemies is an array and you want to know if all enemies satisfy the condition.
    every() terminates upon the first falsy return value, so you don't need a collector or status var like you would if you tried to use .forEach(), and you can bail-out early, which is much faster in practice.

    if you want all enemies that satisfy the condition, replace the word "every" with "filter".

    if you want to end iteration when the first condition is trueish (as opposed to falsy), use .some() instead of .every().
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

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