[SOLVED] Why cycle doesn't work?


(Eva Droch) #1

first class

package {
    import net.flashpunk.FP;
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;

    public class MyLoot extends Entity {
        [Embed(source='assets/wall.png')] 
        private const LOOT:Class; 
        FP.console.enable();
        public var n:Boolean = false;
 
        public function MyLoot(posX:int, posY:int){ 
            graphic = new Image(LOOT); 
            setHitbox(46, 46);
            type = "loot";
            x = posX * 46; 
            y = posY * 46;
        }
        override public function update():void {
                for (var i:int = 0; i < n; i++){ // Why this cycle does not start
                    if (destroy()){
                        var aa:MyWorld = new MyWorld();
                        aa.nn();
                    }else {
                        n = false;
                        break;
                    }
                }
        }
        public function destroy():void{ // this function working
            FP.world.remove(this);
            n = true;
            trace(n);
        }
    }
}

second class

package {
    import net.flashpunk.World;
 
    public class MyWorld extends World {
        public function MyWorld() {
                add(new MyPlayer(3, 8));
                add(new MyStone(3, 4));
                add(new MyLoot(5, 10));
                add(new MyRestart(10, 0));
            for (var i:int = 0; i < 13; i++){
                add(new MyWall(i, 1));
                add(new MyWall(i, 13));
            }
            for (i = 1; i < 13; i++){
                add(new MyWall(0, i));
                add(new MyWall(12, i));
                
            }
            for (i = 2; i < 11; i++){
                add(new MyWall(9, i));
            }
            for (i = 5; i < 13; i++){
                add(new MyWall(7, i));
            }
        }
        public function nn():void{
            add(new MyLoot(5, 11));
        }
    }
}

(Mike Evmm) #2

You are doing i<n — since n is a boolean, this doesn’t make much sense, but flash automatically casts true to 1 and false to 0. Therefore, since i is already 0 (i.e. >= 0), the loop doesn’t run.


(Jacob Albano) #3

I’m not sure what that loop is supposed to do, to be honest. You’re also checking the return value from a function that returns void, which will always be false (void == undefined and undefined is a falsy value).

Even if the loop did run, your loot would be removed from the world on its first update.

What is your goal here? Maybe we can come up with a better approach. :o)


(Mike Evmm) #4

Taking a second look at the loop code, I think what you’re looking for here is a do while loop ?


(Eva Droch) #5

Now I see where there was an error

    override public function update():void {
            for (var i:int = 0; i < n; i++){ // Why this cycle does not start
                if (destroy()){
                    var aa:MyWorld = new MyWorld();
                    aa.nn();
                }else {
                    n = false;
                    break;
                }
            }
    }
    public function destroy():void{ // this function working
        FP.world.remove(this);
        n = true;
        trace(n);

I check if(destroy()} before cycle starting, but start position it’s n = true in destroy()


(Eva Droch) #6

I teach AS3 through practical code examples, so that half of what I write - does not work -_-

I needed a cycle that memory is not jumping like mad, if i write

var aa:MyWorld = new MyWorld();
aa.nn();

I can see in the console that memory very quickly jumps.

I have done differently

loot class

if (collide("player", x, y)){
var aa:MyWorld = new MyWorld();
aa.nn();
}

world class

public function nn():void{
trace("nn on");//output tracing nn on
add(new MyLoot(5, 11));/* but the new MyLoot is not added (   probably due to the fact that, Adds the Entity to the World at the end of the frame.)*/
}

(Jacob Albano) #7

That’s how I learned everything I know. No worries. :relaxed:

You can force entities to be added and deleted by calling updateLists() on a world. It’s not recommended in all cases, but this looks like a place it might be helpful:

public function nn():void {
    trace("nn on");//output tracing nn on
    add(new MyLoot(5, 11));
    updateLists();
}

(Eva Droch) #8

Thank you, I will continue to experiment :blush: