transitioning scene when all enemies cleared? SOLVED!

infinitum3d

  • Posts: 89
I want my level to end once I've cleared the board of actors.

I've tried (if (not (last actor created) on screen
but if I kill the last created before the next to the last
then it transitions too soon

Any suggestions on how to transition AFTER all the enemies and power ups are  cleared?

« Last Edit: January 14, 2014, 05:41:22 pm by infinitum3d »

Innes

  • *
  • Posts: 1960
There are many ways to do this, so here are some ideas:

Firstly, create a scene attribute (number type) called (e.g.) Enemy Count. Ensure it is set to zero.

If the enemies and powerups have been placed in the scene using the Scene Designer, then you need to do a count of all the relevant actors that exist at the start of the scene by using the [for each] blocks, setting Enemy Count to the total number of actors.

If enemies and powerups are created during the game, then each time you create an enemy or powerup, increment the value of Enemy Count.

At this stage, Enemy Count should be equal to the number of enemies / powerups in the scene. All you need to do now is decrement the value of Enemy Count each time an enemy or powerup is removed from the scene. When Enemy Count = zero, you can transition to the next scene as required.

In my book Learning Stencyl 3.x Game Development: Beginner's Guide (www.StencylBook.com), this scenario is covered step-by-step for collectible items that are placed using the Scene Designer. The book is also suitable for users of Stencyl 2.2.
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

infinitum3d

  • Posts: 89
That's actually quite brilliant. Thanks! Makes perfect sense.

Innes

  • *
  • Posts: 1960
That's actually quite brilliant. Thanks! Makes perfect sense.

You're welcome.

If you are using the first method (using [for each] to count enemies in the Scene Designer), make sure that you leave enough time for the scene to be completely created before the relevant behavior executes. The easiest way to do this is to place the counting loop in a [after 0.5 secs] block.

Although it didn't happen often (rarely, in fact), I found that very occasionally, the count was incorrect. My assumption was that the counting behavior executed before all the actors came into existence in the scene. Placing a short delay before the count fixed the problem.

I should also add that my solution is one of many possible solutions; if someone else has a better / more elegant solution, please do let us know.
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

infinitum3d

  • Posts: 89
I thought about simply creating a variable called Count. Each time an enemy or power up is created, increment the variable.  Each time one is removed, decrement the variable.  The problem is, when the first enemy is created, the Count becomes 1. When it is killed, it is decremented to zero and the scene transitions.

Innes

  • *
  • Posts: 1960
I thought about simply creating a variable called Count. Each time an enemy or power up is created, increment the variable.  Each time one is removed, decrement the variable.  The problem is, when the first enemy is created, the Count becomes 1. When it is killed, it is decremented to zero and the scene transitions.

In that case, you need the answer to a question that is different to the one with which you started this thread! In the original question, you asked to determine when you had cleared the board of actors; i.e. when there were no more actors of a particular type left in the scene (at least, that was my interpretation)!

In order to solve the problem, you need to put into words how you know when the last enemy / powerup has been removed. For example...

Are there a fixed number of enemy / powerup actors?
What is the criteria for creating new powerups / enemies?
What actually defines (in human terms) when the scene is over?

You'll need to provide answers to these questions (and probably more) before a solution can be found.

Perhaps you could give a brief overview of the game to help us along.

Visit www.TheStencylBook.com - the only published book for learning Stencyl.

gplar

  • Posts: 1115
Or you could count them on the fly. Assuming that you start with at least one enemy.

Set NumberOfEnemies to 0

For each actor of type Enemy
Increment NumberOfEnemies

If NumberOfEnemies =0  (Meaning that there are none to count)
Transition

infinitum3d

  • Posts: 89
I think I may have it solved.

I'm making something like a Space Invaders type game with upgrades and powerUps.

In order to clear a level, all the invaders have to be killed, but I don't want the level to transition until the player has a chance to clear the screen of rewards. The way I'm doing this is to determine at the start of the level how many things will be created (and therefore will need to be removed). Then I can decrement to zero from there.

Something like OnCreated-setTotalKills to (invaders + rewards)

infinitum3d

  • Posts: 89
Solved.

I created a Game Attribute called "remainingUntilTransition".
I created a Game Attribute called "invadersRemaining".

At the start of the scene, Created, set InvadersRemaining to random number between 10 and 20.
Set remainingUntilTransition to invadersRemaining

Then on updating a confusing do every loop (aren't all do every loops confusing haha)
so do every 5 seconds
Create Invader
Decrement invadersRemaining by 1

another confusing do every loop Create power up
increment remainingUntilTransition by 1



so every time I kill an invader, decrement
and every time I create a powerUp, increment


there's also an "if" statement saying if invaders remaining is greater than 1 create invader
and an "if" remaining until transition is less than 1 then transition.

I think that covers everything for this thread. . . Thanks for all the help!