HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.96k stars 432 forks source link

Emitter exists value different from as3 flixel #146

Closed impaler closed 11 years ago

impaler commented 11 years ago

Hello I was porting photonstorm's code to Haxeflixel, in the code a FlxGroup of FlxEmitters is used and when you call getFirstAvailable() on the group we get a null. If I put the exists value to false before I call getFirstAvailable it works but after each emitter is called (all 40) it becomes null again.

As3 http://code.google.com/p/flash-game-dev-tips/source/browse/trunk/Flash%20Game%20Dev%20Tips%20Source%20Code/Tip%204/src/Fx.as

Start of Haxe version https://github.com/impaler/HaxeFlixelDemoes/blob/master/BulletManager2/source/Fx.hx#L31

Beeblerox what do you think the best way to make use of emitters in groups like this? I can only think to emulate the as3 behavior by calling kill() again in an appropriate place.

Beeblerox commented 11 years ago

@impaler I'm sorry. I'm out of context a liitle. What is desired behavior in this example?

impaler commented 11 years ago

Sorry I explained it badly. I am porting this article http://www.photonstorm.com/archives/1180/flash-game-dev-tip-4-bullet-manager-part-2

All photonstorm is doing is using FlxEmitters in a FlxGroup so that they can be used like the bullets in an object pool for recycling. Each FlxEmitter will be used as an explosion effect for the enemy ships when they are hit.

http://code.google.com/p/flash-game-dev-tips/source/browse/trunk/Flash+Game+Dev+Tips+Source+Code/Tip+4/src/EnemyManager.as#49

HaxeFlixel has a difference to as3 flixel when we call getFirstAvailable() on the FlxGroup of Emitters as the 'exists' property has different behaviour than original as3. So when an emitter has completed it does not call kill() and the getFirstAvailable() returns a null and throws a error.

//Error after the size of group (40) https://github.com/impaler/HaxeFlixelDemoes/blob/master/BulletManager2/source/Fx.hx#L74

This is what Haxeflixel needs;

//exists == true in HaxeFlixel http://code.google.com/p/flash-game-dev-tips/source/browse/trunk/Flash%20Game%20Dev%20Tips%20Source%20Code/Tip%204/src/org/flixel/FlxEmitter.as#109

//I am having trouble porting this logic http://code.google.com/p/flash-game-dev-tips/source/browse/trunk/Flash%20Game%20Dev%20Tips%20Source%20Code/Tip%204/src/org/flixel/FlxEmitter.as#243

This is all I came up with here to test to put in update to loop through the particles so we can call kill, at the right time. However I am not happy with it as its pretty expensive compared to the as3 code. Can you think of a lighter way?

    var basic:FlxBasic;
    var i:Int = 0;
    var allExists:Array<Bool> = new Array();
    while(i < length)
    {
        basic = members[i++];
        if (basic != null)
        {
            allExists[i++] = basic.exists;
        }
    }

    if ( !Lambda.has(allExists, true)) {
        FlxG.log("all particles are complete");
        kill();
    }
Beeblerox commented 11 years ago

@impaler I think this tutorial uses older version of flixel (2.43) and the logic of emitter has been changed. Will try to fix this behavior. Thanks for such detailed explanation

Beeblerox commented 11 years ago

@impaler try my fix. maybe it will help

impaler commented 11 years ago

Thanks this works well

Beeblerox commented 11 years ago

I found that my fix for this issue could conflict with the logic related to on property value. We could pause/resume particle imission by changing the value of on property (in as3 version of flixel), but with my fix setting on = false means kill() that emitter. I need to think about it more. Maybe we'll introduce new methods and variables (i don't want it but i'll do it if i have to)

Beeblerox commented 11 years ago

I'll probably make it work like in version 2.43:

  1. FlxEmitter will be killed automatically if _explode == true and when ((lifespan > 0) && (_timer > lifespan))
  2. FlxEmitter with _explode == false won't be killed automatically at all This solution doesn't require any new variable or method. But I'll add new issue for automatic emitter deactivation and will try to solve it in the next release (1.08)
Beeblerox commented 11 years ago

@impaler Does last commit work for you?

impaler commented 11 years ago

@Beeblerox Yes and a trace on kill debugs at the right time and only once, its worth this extra variable.

I'll let you know how the next one goes http://www.photonstorm.com/archives/1191/flash-game-dev-tip-8-building-a-shoot-em-up-part-3-return-fire it looks fun to port.

Beeblerox commented 11 years ago

@impaler Great! There will be problem in porting this tutorial, since FlxCollision's pixelPerfectCheck() method works only on flash target. It's very hard to port this method on cpp taget

Beeblerox commented 11 years ago

@impaler I'll try to port it again but can't be sure in success. Plus pixelPerfectCheck() method would be slow, since it depends on BitmapData's draw() method

impaler commented 11 years ago

@Beeblerox ok thanks for the heads up, it would be a shame not to have. I'd be happy to test how slow it is on android.

Beeblerox commented 11 years ago

@impaler I've implemented pixelPerfectCheck() method for cpp target in last commit. So you can test it :)