Username or E-mail
Password (Forgot?)
New? Sign Up
Join or Sign In
Forums Stencylpedia Stencyl TV Translations Go Pro - Plans & Pricing Download Stencyl


Switch to Book Layout

1: Getting Started

  • Getting Started
  • Crash Course
  • Crash Course 2
  • StencylForge

2: Building Logic

  • What is a Behavior?
  • Creating a Behavior
  • Attributes
  • Game Attributes
  • Attribute Types
  • Events
  • Our Behaviors

3: Actors

  • What are Actors?
  • Animations
  • Motion & Forces
  • Physics
  • Controls
  • Collisions & Groups
  • Killing vs. Recycling
  • Tweening
  • Effects
  • Chapter 3 Challenge

4: Scenes

  • Scene Basics
  • The Camera
  • Tilesets
  • Regions
  • Drawing Text & HUDs
  • Changing Scenes
  • Music, Sounds & Channels
  • Backgrounds
  • Customizing Actors
  • Chapter 4 Challenge

5: Game Mechanics

  • Saving
  • Pausing
  • 3rd Party Services
  • Web Requests

6: Advanced Topics

  • Get/Set Attributes
  • Custom Events
  • Blending Modes
  • Lists
  • Custom Blocks
  • Continuous Collisions

7: Testing & Tuning

  • Testing Games
  • Optimizing Performance 1
  • Optimizing Performance 2

8: The Last 10%

  • Flash Publishing
  • Standalone Apps
  • iOS App Store
  • Chrome Store
  • Making Money

M1: Mobile - Intro

  • Getting Started
  • Testing on your Device
  • Flash -> iOS Guide

M2: Mobile - Basics

  • Atlases
  • Drawing Text
  • Retina Display
  • Accelerometer
  • Joystick
  • Universal Games

M3: Mobile - Services

  • iAds
  • Game Center
  • In-App Purchases

M4: Mobile - Publishing

  • Debugging
  • Publishing to the App Store
  • Optimizing Performance
  • Promoting your Game

A: Troubleshooting

  • Showstoppers
  • General FAQ
  • iOS FAQ
  • The 90% Memory Warning
  • Recovering Broken Games
  • Can't Export to SWF
  • Reloading Documents
  • Generating Logs
  • Flash Security Settings
  • How to Report Bugs

B: How-To Guides

  • Importing Assets
  • Scene Designer
  • Code Mode
  • Font Editor
  • Pencyl (Image Editor)
  • Tile Editor (Shapes)
  • Game Cleaner

C: Reference

  • Glossary
  • Block Reference
  • Useful Shortcuts
  • Stencyl API

D: Resources

  • Stencyl TV
  • Abigayl's Guides
  • Giving Critiques
  • Creating Extensions
  • Translating Stencyl
  • Credits

3.0 Drafts (In Progress)

  • What's New in Stencyl 3.0?
  • Setup (Android)
  • Setup (Desktop)
  • Setup (iOS) - Concepts
  • Setup (iOS) - Mac
  • Setup (iOS) - Windows
  • Testing iOS on Windows
  • iOS Troubleshooter

  • Mobile App Scaling
  • Full Screen Mode
  • Simple Physics
  • Backgrounding an App

  • iOS App Store
  • Mac App Store
  • Windows Store
  • Google Play
  • HTML5

  • Android Ads
  • Android Purchases (WIP)
  • 4" Form Factor (iPhone 5)
  • Mobile Input
  • Mobile Features

  • Extending the Engine
  • iOS / Android Extensions
  • Developing the Official Extensions
  • Developing the Engine

  • iAds (Revised)
  • Game Center (Revised)
  • iOS Purchases (Revised)
  • Atlases (Revised)
  • Drawing Text (Revised)
  • Joystick (Revised)
  • Accelerometer (Revised)
  • Sounds (Revised, WIP)
  • Debugging (Revised, WIP)
  • iOS Performance (Revised)
Level: Beginner

Creating and Destroying Actors

Contents

  • Introduction
  • Normal vs. Recycled Actors
  • How To: Creating Actors
  • How To: Destroying Actors
  • Events

 

Note for Stencyl 3.0 and above

 

Recycled Actors are now the default and only choice in 3.0. Creating and destroying Actors automatically opt for the recycled option. We're keeping this article around for users of older versions of Stencyl and for educational purposes in informing you about what is happening under the hood.


Introduction

Imagine that you’re playing an action game such as Mega Man. The main character fires bullets to the side. These bullets hit enemies, which die after being hit a few times. These enemies fire bullets back at you.

In any modern game, actors get created and destroyed at a rapid rate. This article talks about how you create actors and destroy them in a way that maximizes your game’s performance.

 

Normal vs. Recycled Actors

Stencyl supports two types of actors:

  • Normal Actors
  • Recycled Actors

Normal actors follow an expected lifecycle - they’re created, they do stuff and they finally die.

Recycled actors are different. While normal actors are destroyed when you kill them, when you “kill” a recycled actor, it goes into a pool of recycled actors.

The next time you create a “recycled” actors, it will check the pool and see if that actor has been created before. If it has, it will pull the “new” actor from the pool instead of making the new actor from scratch. Thanks to recycling, the penalty for creating and destroying a recycled actor is close to nothing.


How is this useful?

Recycled actors are best when they’re created and killed rapidly, such as particles or bullets.

Exercise: If recycled actors sound so great, why don’t we make them the default? That’s a good question! Can you think of a downside to them?

 

How To: Creating Actors

Just these two blocks (Scene > Actors)

Sometimes, you’ll want to refer to the actor that you just created. Use the “last created actor” option under any Actor dropdown to do this.


How To: Destroying Actors

Destroying actors is similar to creating them, in the respect that there are separate blocks for destroying normal actors and recycled actors.

Remember this golden rule when killing an actor: kill an actor in the same way you made it.

Which is to say...

  • If you created a normal actor, kill it.
  • If you created a recycled actor, recycle it.
Note: Killing or recycling an actor is immediate. Don’t attempt to refer to an actor at any point after you’ve killed it, including during timed tasks. For example, don’t do this.

 

Events: Knowing when actors get created or die

Did you know there are events to control what happens when an actor is created or destroyed?

Select one of these events via the “Add Event” button.

 

Summary

  • Recycled actors come from a “pool” of actors. This is good for scenarios that require lots of creating/destroying of actors, such as firing lots of bullets.
  • Kill actors the same way you made them.
  • You can use events to detect the creation or destruction of actors.


Challenge 1: Create A Particle Effect

Why not try creating a particle effect using recycled actors and along the way, compare its performance to doing this with regular actors?

(View Demo)

Many games use particle effects to add visual polish. Although Stencyl will support particle effects at the engine-level in the future, you can roll your own today.

To create particle effects, just spawn (recycled) actors rapidly and “emit” them at a random speed and starting position and have them fade out over time. With some experimentation, you can create a fire and smoke effect with ease.

 

Challenge 2: Create A Bullet Limiter

Many games have a mechanic that allows you to fire bullets, but only a certain number at a time, otherwise the game would become too easy.

Why not try creating a bullet limiter that allows the player to create no more than 3 bullets at once?

(View Demo)

Hint: Use the ‘when an actor of [TYPE] is created/killed’ event!


Last Updated: 2013-01-29 by Jon

22025 have read this article
Disclaimer: The Stencyl Team does not actively monitor comments on articles. If you're seeking help for your game, please ask a question on the forums. Thanks!
12 Comments
katemamba
My game consists of 2 types of deaths of main charcter -1) through dying in pits and 2) direct kill by an enemy . I create actor at start of screen , then recycle him whenever he jumps into pits and dies by enemy. my code and behaviours also support this...but during testing I am Able to recycle the actor (actor dies and new actor is created at start of scene)only when the cause is due to pits, not both by enemy and pits..solutions/fixes please
0 4 months, 3 weeks ago
wilycodger
Is there a way to flush the pool of recycled actors?
1 5 months, 1 week ago
wilycodger
I don't really perceive a frame rate difference between the recycled actor mode and the normal actor mode in the particle demo. I recreated the demo on my machine and also see no substantial difference in performance in my version. I am generating 12 particles per click and fading them out over 4 seconds; in both the recycle and normal modes the frame rate drops off quickly once you go over ~160 particles.
0 5 months, 1 week ago
cloa513
Acsmitte- The Germans lost because they had Hitler as their boss so there was no good strategy- if they had gone straight for the oil supplies in Urals mainly then the Russian war machine would have ground to a halt. Its like a vast war game that you explore without any point- your forces no matter how good assuming they are mortal if spread thinly will always lose. Its like playing Hercules in standard battlefield rather than guerilla like Rambo.
0 6 months, 2 weeks ago
andyclark
Acsmitte's comment got negative votes. I gave it a positive vote. It showed interest in history AND it sounds like he had a War Game on his mind.
0 6 months, 3 weeks ago
Xionis
I believe it works thusly. Creating an actor consumes a larger portion of resources than just having one, same for destroying. So if you are constantly creating/destroying, thats a lot of resources. But, if you create once (with a recycled actor) and then just reuse the same actor over and over, it uses far fewer resources constantly existing than constantly being created/destroyed. But, if you had a fairly complex actor, with many constant events/behaviors that uses a lot of resources while existing, recycling it would mean those resources keep being used even when the actor isn't. That's what I think the difference is.
1 8 months, 3 weeks ago
t4u
i have no idea what is the difference between recylced and created. i mean article needs an example which says in this case creating actor bad thing, recycling actor good thing. is it only about game performance?
0 9 months, 4 days ago
vmetalbr
Not quite sure if this is the con of recycled actors: they are never destroyed so after creation there will always be in the resource pool occupying space, unless they are cleaned up sometime. Having too many will reduce avaliable resources to other tasks...

So, am I right?

0 10 months, 1 day ago
luddet
Recycling things is usually good. However it requires you to have those created, even when they are not used, which allocates memory. So don't recycle more things than necessary.
1 11 months, 4 weeks ago
srivello
Very weak metaphor.
1 1 year, 6 days ago
12Next



Commenting Guidelines

Sign In to Comment

Make Games

  • What is Stencyl?
  • Roadmap
  • Pricing

Play

  • Arcade
  • Showcase

Community

  • Forums
  • Chat
  • Translations

Help

  • Stencylpedia
  • Stencyl TV

About Us

  • Blog
  • Contact Us
  • Press
  • Privacy
Follow Stencyl on Twitter



© 2013 Stencyl, LLC.