Jump to content
  • Advertisement
Sign in to follow this  
P0jahn

A button that spawns blocks when colliding with subject.

This topic is 1939 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Resolved!

 

I have a game object, say a button. When the button is pressed by for example the main character(i e, they are colliding), it will spawn a bunch of blocks with an interval between each spawn. When the button is no longer pressed down, the blocks start to disappear. Here is how it looks ingame(that turret to the left is the button):

GIFTemp_EA8_00_00_00_00_00_10.gif

 

As you can see, when the button is pressed, a bunch of blocks are spawned at a fixed position. When the button is no longer pressed, the blocks start to disappear. I have coded this in my game, but there is bug I have not been able to resolve for days and it is getting frustrating.

 

The bug appear when you stand on the button and spam quick jump(i e, collide, not collide, collide, not collide and so on).  The bug effect is that the blocks(often the first and second) are not ever getting removed/despawn.

 

Here is the function, called from the game loop:

        private GameObject[] users, blocks;
        private int index, spawnCounter, removeCounter, spawnDelay, removeDelay;	
        private boolean cleared, decrease;

        @Override
	public void eventHandling() 
	{
		if(0 > index)
			index = 0;
		
		if(isColliding(users))
		{
			if(index <= blocks.length - 1 && ++spawnCounter % spawnDelay == 0)
			{
				world.add(blocks[index]);
				
				decrease = index < blocks.length - 1;
				index++;
				cleared = false;
			}
		}
		else
		{
			if(!cleared && ++removeCounter % removeDelay == 0)
			{
				if(index > blocks.length - 1)
					index--;
				
				if(decrease)
				{
					index--;
					decrease = false;
				}
				
				if(index <= 0)
					cleared = true;
				
				world.remove(blocks[index]);
				index--;
			}
		}
	}

Why am I receiving this erratic behavior?

Edited by P0jahn

Share this post


Link to post
Share on other sites
Advertisement

decrease looks dodgy to me. I can't see where it is declared or how it is used, but it may cause index to be decremented twice, whereas you only ever call remove once. I'd look at that first.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!