Splitting A TypedGroup?



  • I have a TypedGroup class with 2 objects in it. When this object collides with something, can I take those 2 objects out of the class somehow and add them to the play state? THEN remove the TypedGroup?



  • I was encouraged to post my code so here it is in three parts.

    The state:
    https://pastebin.com/yUYRhbf1

    The grouped block object:
    https://pastebin.com/jZL6EcZY

    The single block object:
    https://pastebin.com/CTyHmbc6

    Here is the registry:
    https://pastebin.com/68DPs7Ff

    This is what happens when you hit play:
    https://twitter.com/RobClemmonsJr/status/1007369261231366150

    I'm only assuming that is because I have not created the second part of the collision system yet. I think I might have solved my original problem, but we'll see.

    Any idea on how to improve the system as it is?



  • @xhunterko said in Splitting A TypedGroup?:

    collision

    I have a similar code as you. with groups, I use a collision to add or remove them. you can store a group's ID in a reg var if you are having problems using them at another class.



  • Basically what I want to happen is this:

    -Two blocks fall from the screen, one rotates around the other (done)
    -If either of these two blocks hit a sprite that acts as a floor, they both stop.(done)
    -If either of these two blocks hit blocks that have touched the floor, one of the following will happen:
    --If any block is the same color as a block that it hits, they both explode.
    --If any block that is NOT the same color as a block that it hits, it stops moving. (causing the blocks to pile upwards)

    This SOUNDS like it should be simple to handle, but I dunno why it's been so difficult. I've got blocks going through the sides of other blocks (even though I've not set it to only collide on top), recycling weirdness, and spawning nonsense. It's frustrating especially when you think your doing everything correctly.

    Flixel has it set so that:

    collide(object, object) or collide(group,group) (plus their variations)

    I don't know how to do what I want done with flixel. What I'm doing is:

    collide(group, group, functionA)
    public functionA(object, object)

    Because, that's what the examples show? Right?

    @galoyo Thanks for the help, I appreciate it. ^^



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