Respawn doesn't work for certain actors after reloading Scene

adrianocapcom

  • Posts: 8
Hello, I'm a new Stencyl users and so far I'm loving the platform.
I feel that Stencyl allows me to create things I've dreamt of for my whole life (grew up in the 80's and love 2D games).

I was working on a couple of game prototypes and I'm often encountering the same problem, related to actors and sounds.
Some of them don't reload after reloading scene, or switching to the same scene.
ie: Main Scene, after death, reloads and doesn't spawn a certain enemy actor which should appear every 3 sec (when created).

Here how the behavior looks like.

Code: [Select]
    /* ======================== When Creating ========================= */
        createRecycledActor(getActorType(13), 543, 0, Script.MIDDLE);
        createRecycledActor(getActorType(7), 543, 0, Script.MIDDLE);
        runPeriodically(1000 * 3, function(timeTask:TimedTask):Void
        {
            createRecycledActor(getActorType(13), 543, 0, Script.MIDDLE);
            createRecycledActor(getActorType(7), 543, 0, Script.MIDDLE);
        }, null);
    }

I don't know what I'm doing wrong, but this is becoming frustrating and I don't want to give up.

Any help?
I'd love to be able to chat with someone and if possible schedule a one on one lesson. Anyone in the NYC area?
I'd love to learn more.

Thanks.
A

Rares

  • Posts: 216
Because you make actors when creating. Do it in Always. How often do you want it to be? How many of each actors?
Last version (0.1.5) of my now cancelled game:
http://www.stencyl.com/game/play/27241

adrianocapcom

  • Posts: 8
Thx for your reply.
This behavior I was discussing it's attached to the scene.
I've tried the When Always instead and it creates a sort of visual feedback that spawns out continuously new actors and slows down dramatically the CPU and freezes the game. (Also this works on the first time I play the level, and when it reloads it stops to work.

The crazy thing about the code I attached is that the first actor after when created spawn correctly but it seems to ignore the "Do Every 3 sec" function.

LIBERADO

  • *
  • Posts: 2714
Code: [Select]
    /* ======================== When Creating ========================= */
        createRecycledActor(getActorType(13), 543, 0, Script.MIDDLE);
        createRecycledActor(getActorType(7), 543, 0, Script.MIDDLE);
        runPeriodically(1000 * 3, function(timeTask:TimedTask):Void
        {
            createRecycledActor(getActorType(13), 543, 0, Script.MIDDLE);
            createRecycledActor(getActorType(7), 543, 0, Script.MIDDLE);
        }, null);
    }

I just tried this code and it works perfectly after reloading or switching to the same scene.
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

Rares

  • Posts: 216
in always.
set actorcount1 to 0

for every actor of type/group/whatever
increment actorcount1 by 1

while actorcount1<max of that actor
do every x seconds
create actor
Last version (0.1.5) of my now cancelled game:
http://www.stencyl.com/game/play/27241

adrianocapcom

  • Posts: 8
I've tried for a couple of hours to put together that but..No luck. I've tried what you were suggesting, hoping I got it right: should it look like this?

Code: [Select]
{
    public var _Actorcount:Float;
    public function new(dummy:Int, engine:Engine)
    {
        super(engine);
        nameMap.set("Actorcount", "_Actorcount");
        _Actorcount = 0.0;
    }
    override public function init()
    {
        /* ======================== When Creating ========================= */
        _Actorcount = asNumber(0);
        propertyChanged("_Actorcount", _Actorcount);
        createRecycledActor(getActorType(13), 543, 0, Script.MIDDLE);
        createRecycledActor(getActorType(7), 543, 0, Script.MIDDLE);
        /* ======================== When Updating ========================= */
        addWhenUpdatedListener(null, function(elapsedTime:Float, list:Array<Dynamic>):Void
        {

            if (wrapper.enabled)
            {
                _Actorcount = asNumber(0);
                propertyChanged("_Actorcount", _Actorcount);

                for (actorOfType in getActorsOfType(getActorType(13)))
                {
                    if (actorOfType != null && !actorOfType.dead && !actorOfType.recycled)
                    {
                        _Actorcount += 1;
                        propertyChanged("_Actorcount", _Actorcount);
                    }
                }

                while ((_Actorcount < 2))
                {
                    runPeriodically(1000 * 3, function(timeTask:TimedTask):Void
                    {
                        createRecycledActor(getActorType(13), 543, 0, Script.MIDDLE);
                    }, null);
                }
            }
        });
    }
    override public function forwardMessage(msg:String)
{}}

This one doesn't work. it creates a black screen, never loads the game and have to force quit Flash Builder.
What am I doing wrong?
Could you show me how it should look like in blocks terms?

Also after reloading the scene, the music produces click and pops, almost looks like the buffer size  is going low..and something is slowing  down the sound. Sometimes, when the level reload, I hear the sound for a fragment of a second and then it mutes abruptly.

I want to learn so bad and avoid mistakes but I guess it will take a bit of time

sdieters

  • Posts: 2068
first off, welcome to stencyl!

Quote
Because you make actors when creating. Do it in Always. How often do you want it to be? How many of each actors?

the "runPeriodically" sound like a periodic task which keeps being executed unless you use the cancel block.
so you can simply place this in the when created event otherwise it will be executed every frame. use this method instead (see attachment). the toBeCreatedActor actor type attribute enables you to select an actor that is being spawned in the scene every 3 seconds (you can set this in the behavior tab of your scene). the maxActors is ofcourse the maimum amount of actors that you want to have on screen, while the actorCount1 keeps track of how many there are on screen. the maxActors number attribute should also be set in the scene's behaviors tab.
now basicly what happens is this:

when this scene is loaded, every 3 seconds he will first check if actorCount 1 is less then maxActors, if this is true, he will create an actor and increases the actorCount1 number attribute by 1. if actorCount 1 is equal to maxActors, he will simply skip the creation of an actor and waits 3 seconds before trying it again. now in the second event, everytime you kill one of these actors it will decrease the counter. please note that this will only work if you spawn the same actor instead of different ones, but with this setup you are only able to select one actor to spawn in the scene's behavior tab.

i have a youtube channel on which i have a small tutorial serie on how i created a completely customizable actor spawn behaviour which also explains periodic spawning of actors and keeping track of the amount of actors in your scene. you can find the link to my channel in my signature below. the tutorial serie is called "fully customizable actor spawn". if you have any more questions please let me know.
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p

adrianocapcom

  • Posts: 8
Hello Sdieters and thanks for the tip.
I watched your video and I'm fascinated by your system, makes everything way more clear and organized and I applied the advanced Spawning to another game I'm working on too. Love your channel!

About this specific issue though, I was able to setup everything correctly, attributes etc: it works the first time the scene is played and after the reload NO object shows up.

I'm wondering if this has something to do with the fact that at the launch the scene isn't able to reset the actorCount1 to 0.

To fix this I've tried to add a "set actorCount1 to 0" in When Created but nothing changes.
I've tried to switch to When Updated but there was no difference, the second time the scene is loaded no spawning happens for the selected toBeCreatedActor.

To reduce the margin of error I'm focusing on one actor at the time and trying to make it work.

I don't want to give up and feel like if I fix this I'll be the happiest person ever :)))
Attached some Screenshots.
A

sdieters

  • Posts: 2068
i'm glad you like my channel!

im not sure why things dont work when you reload the scene since everytime you do, all the attributes willr eset to their default value which is 0 for actorCount1.

looking at your obstacle, i have a strong feeling this is another flappy bird clone? am i right with this?
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p

adrianocapcom

  • Posts: 8
Ahahahah, you are completely right.

The reason why I'm programming this is pure practice.
I just want to learn and I felt that this was a type of game relatively simple to design.

Any idea on how to fix this? Things I could try?
Have you ever experienced something similar?
It seems that no matter what I do, reloading the scene I'm never able to respawn.
I start to feel that there is something wrong with my version of Stencyl? Using 3.1
Feeling frustrated right now

sdieters

  • Posts: 2068
have you tried to look for a tutorial?
there are plenty of them around.

http://lmgtfy.com/?q=stencyl+flappy+bird+tutorial

no offence, but i dont feel like explaining things again, since i have done that to many times since the original game came out hehe.
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p

adrianocapcom

  • Posts: 8
Yes, I watched several and then followed this in detail (twice) and for some reasons I can't find a way out.

http://blog.stencyl.com/?p=1367

In my case I end up with things working good, but only the first time the scene is loaded.
I don't want to waste your time, and before to ask for help on this forum I spent a lot of time trying and retrying…without luck

sdieters

  • Posts: 2068
send me your project file and i will try to figure out what your problem was.
cant let a new member getting frustrated that quick hehe
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p

adrianocapcom

  • Posts: 8
Thank you!
I just sent you a message with the link to the file

sdieters

  • Posts: 2068
you didnt tell me about the events you placed directly on the scene. because you never unpause the game, the code will never work again. try disabling those events and test the game. if it works now, you know your problem. also, the way you pause your game is way to hard on the CPU. try not to use that many always events. you should only use them if you eally need to check stuff in realtime like keypresses, collisions (but they are handled by collision events) etc... stuff like pausing the game should be done by custom events. you can learn about them also in my Stencyl 101: custom events video.
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p