Splitting A TypedGroup?



  • This is what currently happens:

    https://twitter.com/RobClemmonsJr/status/1009911439379886080

    These are the functions I'm using:

         FlxG.collide(FallingBlocks, Floor, floorStill);
    	FlxG.collide(FallingBlocks, StillBlocks, completeStill);
    
        public function floorStill(block:Block, still:FlxSprite ):Void
    {
    	FallingBlocks.forEach(function(bok:Block):Void
    	{
    		
    		bok.immovable = true;
    		bok.solid = true;
    		bok.still = true;
    		bok.velocity.y = bok.velocity.x = 0;
    		
    		bok.canMove = false;
    		
    		StillBlocks.add(bok);
    		//FallingBlocks.remove(Block);
    		FallingBlocks.remove(bok, true);	
    		canFall = 0;
    	});
    	
    	canFall = 0;
    }
    
    public function completeStill(block:Block, still:Block ):Void
    {
    	FallingBlocks.forEach(function(bok:Block):Void
    	{
    		bok.still = true;			
    		bok.solid = true;
    		bok.immovable = true;
    		bok.velocity.y = bok.velocity.x = 0;			
    		bok.canMove = false;
    		
    		StillBlocks.add(bok);
    		//FallingBlocks.remove(Block);
    		FallingBlocks.remove(bok, true);	
    		canFall = 0;
    	});
    	
    	canFall = 0;
    }
    

    Both blocks are added to fallingblocks in a standard timer spawn function:

    var obs:Block = FallingBlocks.recycle(Block, function()
    		{
    			return new Block(280, 0, Random.int(0,2));				
    		});
    

    shrugs
    It SHOULD work like I think it should. And yet it does what the above gif in the tweet displays. So, I don't know why the effects are only being applied to either one side, or one block, when foreach should apply to all and with the same instances?



  • you should use ID vars that refer to instances. at playstate, when its time to drop another block from the top screen, you create an instance of group at playstate, new (ID, x, y) where ID is a var that increments in number each time a block falls. so at group class, at update(), you add or remove that instance to group1 or group2 at the collision function. group1 is red images, blue is ID 2, etc. then `if collide (group, group2, function)'.

    so at the group class which holds all images that fall, at update(), if (ID == anyNumberYouWantToCheckVar) collision (group, group1, function). Id is passed to the class when the instance is created. at that class, without an id check, all instances will be checked in that group. could that be the problem?

    I always use IDs when working with instances.



  • Well. As far as the id thing goes. the block class has 3 attributes when created, block(x,y,swatch) where swatch dictates what color a Block is. In a previous score function, or what I have typed out, the stillFunction, I have it set so that:

    collide(groupA,groupB, stillfunction)

    In the old scoreFunction, this would then play out as:

    public function scoreFunction(falling:Block, still:Block)void
    if falling.swatch == still.swatch
    { falling.kill, still.kill, do score stuff}
    else
    {falling.velocity.x&y = 0 set more stuff to prevent the blocks from moving}

    So, I think I'm already doing an id system that you describe? If that's the case, then the same thing displayed occurs WITH that system.

    So I do want the pair of blocks to be stopped at the same time if either of them hits something. Would an id along with the color check help with that?

    OR would doing direction collision(if block.object.floor collide with block.object.ceiling) help at this point?



  • This topic is deleted!



  • yes, it would help because what the code is doing is checking all instances at once. the second falling red object is checked with the other red object that is already on the ground. an ID helps in many ways. at ID 10 you can make a super block fall. see you have ID for the color but not for the instance. unless I am overlooking something, you need an instance ID. you can add and remove a group but without an instance ID you would add or remove everything at once because at the update() function all instances is checked at the same time. an ID helps to tell them apart.



  • Well, since it seemed, simple to add, I went ahead and added a couple things.

    In the playstate, I added two variables, for short lets call them:

    nowFalling, fallingPartner. They are both ints.

    Then in the Block class I added a new parameter, IB:int, since the haxeflixel object already has an id property. Gave the class a new public var now:int and at create had now = IB.

    Then in the spawn function where I create the two blocks, I pass the IB parameter as a Random int from 0 to 999. THEN I set falling and fallingPartner to block1 and block2s 'now' variable.

    Back in the collision function, I do:

    groupForEach(bok:Block)
    if bok.now == nowFalling
    do stuff

    if bok.now == fallingPartner
    do stuff

    And the same thing happens. So, I dunno what I'm doing wrong here.

    Is this not what you suggested or were you thinking of something different?

    Edit: Added the rest of the conditionals for the if statement from the previous iterations and same thing.



  • pass id var from playstate to the falling block class then in the constructor do ID = id. then you can use the ID var any place at that class. when you respawn an object, you are respawning the instance.

    IDs are useful if you want to explode a row of blocks, at the collide function, you can get the ID from the group, so (group1, group2, collide function), then at that function, do falling block var = group1.ID.

    I would do a collision on all sides of each block. the ID var should = ib which was passed from playstate.



  • this code will reset the instance and only that 1 block. if you need to destroy 2 or more then use each ID or just destroy the group. remember, to hide an object use if (ID = blockNum) visible = false;

    override public function update(elapsed:Float):Void 
    	{
    		if (visible == false)
    		{
    			_spawnTimeElapsed += elapsed;
    			
    			if (_spawnTimeElapsed >= Reg._spawnTime)
    				reset(_startX, _startY);	
    			
    			return;
    		}
    
    override public function reset(x:Float, y:Float):Void 
    	{
    		super.reset(x, y);
    		allowCollisions = FlxObject.ANY;


  • I had a similar problem with the class properties not working when they should be. this code worked for me...

    	public static function allBlocks(a:FlxSprite, e:FlxSprite):Void
    	{
    		if (Std.is(e, Block1 ))
    		{
    			var block:Block1 = cast(e, Block1);
    			block.xSpeed = 400;
    		}


  • Okay. I think it's time to call it. The same thing happens with each suggestion I try. So I dunno what the problem is. I'll probably come back to this project again eventually, but I think I'm done being frustrated for a while.

    Thanks for all your help @galoyo, I appreciate it!


Log in to reply
 

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