Jump to content


Photo

Creating Multiple Instances At Once


  • Please log in to reply
3 replies to this topic

#1 Matchifax

Matchifax

    GMC Member

  • New Member
  • 55 posts

Posted 20 October 2008 - 04:36 AM

Hey. This isn't really a question so to say, I just need an idea of how I can get around this problem.

Here's my situation: When I jump, a certain object (objBlock) gets destroyed. When I land again, objBlock gets recreated at the same place. I have a code that works fine for when there's only one block on the screen, but when I have multiple blocks, things start to get messy.

This is the code I have currently: It is under the step event of another object.

if instance_exists(objPlayer) //This is to prevent the game from trying to read a variable before it is declared.
	{if (objPlayer.action = objPlayer.jumping)
		{with (objBlock) instance_destroy() }
	else
		{if !instance_exists(objBlock)
			{instance_create(objBlockSpace.x, objBlockSpace.y, objBlock)}}}

Basically, this is saying that if Player is jumping, destroy the block. When the player isn't jumping, and while the block hasn't been created yet, create it at that certain spot (BlockSpace).

Now, here's where I run into my problem. When there are multiple blocks on the screen, only one block is created. Obviously because the game thinks thats all it wants due to the "if instance_exists(objBlock)" code.

I've been trying to think a way to fix this for the last few hours, and haven't gotten any further than that. I probably just need a break. Does anybody have any suggestions for code I could use as a replacement for the above?


EDIT: Deactivating the instances did the trick :P

Edited by Matchifax, 20 October 2008 - 12:16 PM.

  • 0

#2 DZiW

DZiW

    GMC Member

  • GMC Member
  • 728 posts

Posted 20 October 2008 - 05:37 AM

Hi Matchifax,
1. <When I jump> what is the code from? What event? [STEP?] [COLLiSiON?]
2. creating/destroying requires more cputime than just hiding (deactivating),
so I'd rather deactivate every individual 'destroyed' block for awhile (see ALARMs)
3. is the code from objPlayer? Why re-check already existing object?
4. how do you destroy that very block(s)? I see no ID assignment

Cheers
  • 0

#3 EasyBeak

EasyBeak

    GMC Member

  • New Member
  • 87 posts

Posted 20 October 2008 - 06:08 AM

Instead of destroying and re-creating the instances you could set visible to 0. The advantage of doing that would be that you don't have to keep track of the blocks position.

Also, if you want it to act as though there is no block there when they are not visible, in the player's collision event with the block put:

if other.visible = 1
{
//enter collision code here
}

Hope that helps you out.
  • 0

#4 Matchifax

Matchifax

    GMC Member

  • New Member
  • 55 posts

Posted 20 October 2008 - 09:40 AM

Thanks for the replies.

DZiW:

1. My jump code is in the step event. As in, if a certain button is pressed, my character will jump.
2. Thanks for the tip. :P How do you only 'deactivate' objects though?
3. No, the code isn't from objPlayer. It is from a different object (which is related to the whole event by the way) "Why re-check already existing object?" I'm sorry, what do you mean by that? Which part of the code are you referring to?
4. The code destroy's all instances of the block, not just 1. My problem was, thy would all get destroyed, but only 1 would get re-created.


EasyBeak: About the second part: Is it possible to do that, since I've already put the code into the collision event? Would I first have to change it to step event, and then add that code in?
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users