getRandom flxgroup



  • helo everyone.. here is my basic question about getRandom..

    	_korban = new FlxGroup();
    	add(_korban);
    	
    	spawnkorban (20, 20);
    	spawnkorban (80, 80);
    	spawnkorban (120, 120);
    
    private function spawnkorban(x:Int, y:Int):Void {
    	var _anak:FlxSprite = new FlxSprite(x, y);
    	_anak.loadGraphic("assets/images/korban1.png", true, 16, 16);
    	_anak.animation.add("idle", [0, 2, 1, 2, 3, 2], 15, true);
    	_anak.animation.play("idle");
    	_korban.add(_anak);
    	
    	var _ibu = new FlxSprite(x, y);
    	_ibu.loadGraphic("assets/images/korban1.png", true, 16, 16);
    	_ibu.animation.add("idle", [8, 9, 10, 11, 9, 8], 3, true);
    	_ibu.animation.play("idle");
    	_korban.add(_ibu);
    }
    

    how to using getRandom code to spawn my group randomly between _anak and _ibu??



  • What getRandom did you refer to?

    You can access a FlxRandom through FlxG.random and use FlxG.random.bool or FlxG.random.int to spawn them randomly

    FlxG.random.bool example - for 2 types only

    private function spawnKorbanRandomly(x:Int, y:Int) {
        var anak:Bool = FlxG.random.bool();
        if (anak)
            spawnAnak(x, y);
        else spawnIbu(x, y);
    }
    

    FlxG.random.int example - for multiple types

    private function spawnKorbanRandomly(x:Int, y:Int) {
        var korbanId = FlxG.random.int(0, NUM_TYPES_KORBAN - 1);
        spawnKorbanId(korbanId, x, y);
    }
    


  • i am refer getRandom from this flxgroup which explained in this link Flxtypedgroup..

    getRandom(StartIndex:Int = 0, Length:Int = 0):T
    

    Returns a member at random from the group.
    Parameters:

    StartIndexOptional offset off the front of the array. Default value is 0, or the beginning of the array.
    LengthOptional restriction on the number of values you want to randomly select from.

    Returns:

    A FlxBasic from the members list

    my purpose insert them to the group because i want to collide them with another group..
    if i using your way, then i will need collide them one by one and will be longer if it had more member.. isnt it be efficient using getRandom instead??

    its been a week i wondering how to use this getRandom thing.. and there is no tutorial apply this function.. and big thanks to your solution Dlean, if few day i cant figure out how to use getRandom, then i will use your way..



  • I'm still not sure what you really wanna do with getRandom.
    As you can see, getRandom only gets you a Korban which has been created and added to the group. Let's say you have 10 Korbans in the group, getRandom will only get you one of those 10 and NOT CREATE a new one.



  • You still can add them to the group using my way by returning the korban created in each function and add it to the group like this in the FlxG.random.bool example:

    function spawnAnak(x:Int, y:Int):FlxSprite {
        var anak:FlxSprite = new FlxSprite(x, y);
        anak.loadGraphic("assets/images/korban1.png", true, 16, 16);
        anak.animation.add("idle", [0, 2, 1, 2, 3, 2], 15, true);
        anak.animation.play("idle");
        return anak;
    }
    
    function spawnIbu(x:Int, y:Int):FlxSprite {
        var ibu = new FlxSprite(x, y);
        ibu.loadGraphic("assets/images/korban1.png", true, 16, 16);
        ibu.animation.add("idle", [8, 9, 10, 11, 9, 8], 3, true)
        ibu.animation.play("idle");
        return ibu;
    } 
    
    _korbans = new FlxGroup();
    
    private function spawnKorbanRandomly(x:Int, y:Int) {
        var anak:Bool = FlxG.random.bool();
        var korban:FlxSprite =
        if (anak)
            spawnAnak(x, y)
        else spawnIbu(x, y);
        _korbans.add(korban);
    }
    


  • thanks a lot Dlean.. i am too focused to getRandom function because its seem the only possible solution.. i am still a noob and hope you can guide me if i had another problem..:bow_tone1:


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.