Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Can't Remove sprites of a Group in update


mk_03
 Share

Recommended Posts

So, when i tried to remove from my my group "groundobjects" works fine only the first time.
I will use a database to conect and get coordinates, there's no problem in that. I add the object to the group and with a timer i remove and add other.
The first time works fine, then, when i add other sprite in this case with random positions the other are not removed.
What im doing wrong?
Ah im using a zoom function that i find in this forum
Let my code...

 

        <script type="text/javascript">
            var x = "<?php echo $_SESSION['coord_x']; ?>";
            sessionStorage.setItem("c_x", x);
            var y = "<?php echo $_SESSION['coord_y']; ?>";
            sessionStorage.setItem("c_y", y);
            alert(" " + x + "," + y + " ");
            function getWidthAndHeight() {
                var w = (this.width) / 2;
                var h = (this.height) / 2;
                sessionStorage.setItem("Ancho", w);
                sessionStorage.setItem("Alto", h);
                return true;
            }

            function loadFailure() {
                alert("'" + this.name + "' failed to load.");
                return true;
            }
            var myImage = new Image();
            myImage.name = "assets/gray.jpg";
            myImage.onload = getWidthAndHeight;
            myImage.onerror = loadFailure;
            myImage.src = "assets/gray.jpg";


            var oldcamera;
            var worldScale = 1;
            var currentBounds;
            var mapSizeMax;
            var worldwidth = 1024;
            var worldheight = 720;
            var mapSizeX = 8000;
            var mapSizeY = 4000;
            var prevScale = {};
            var nextScale = {};
            var zoompoint = {};
            var mapSizeCurrent;
            var distance = 0;
            var olddistance = 0;
            var distancedelta = 0;
            var easing = 0.1;

            var game = new Phaser.Game(worldwidth, worldheight, Phaser.AUTO, '', {preload: preload, create: create, update: update});

            function preload() {
                game.scale.pageAlignHorizontally = true;
                game.scale.pageAlignVertically = true;
                game.scale.setScreenSize(true);
                game.load.image('clouds', 'assets/gray.jpg');
                game.load.image('dude', 'assets/body.png');
                game.load.image('diamond', 'assets/diamond.png');

            }
            function create() {
                timer = game.time.create(false);
                timer.loop(5000, updateStars, this);
                timer.start();

                worldScale = 1;
                stageGroup = game.add.group(); // this group will contain everything except the UI for scaling
                backgroundobjects = game.add.group();
                groundobjects = game.add.group();

                mapSizeMax = mapSizeX;
                mapSizeCurrent = mapSizeMax;
                background1 = game.add.sprite(0, 0, 'clouds');
                backgroundobjects.add(background1);
                stageGroup.add(backgroundobjects);
                sprite = game.add.sprite(sessionStorage.getItem("c_x"), sessionStorage.getItem("c_y"), 'dude');
                groundobjects.add(sprite);
                stageGroup.add(groundobjects);
                currentBounds = new Phaser.Rectangle(-mapSizeX, -mapSizeY, mapSizeX * 2, mapSizeY * 2);
                game.camera.bounds = currentBounds;
                game.camera.focusOnXY(sessionStorage.getItem("Ancho"), sessionStorage.getItem("Alto"));
                game.input.mouse.mouseWheelCallback = function (event) {
                    var wheelDelt = game.input.mouse.wheelDelta;
                    if (wheelDelt < 0) {
                        mapSizeCurrent -= 150;
                        mapSizeCurrent = Phaser.Math.clamp(mapSizeCurrent, worldwidth, mapSizeMax);
                    } else {
                        mapSizeCurrent += 150;
                        mapSizeCurrent = Phaser.Math.clamp(mapSizeCurrent, worldwidth, mapSizeMax);
                    }
                    worldScale = (mapSizeCurrent / mapSizeMax);
                };
            }
            function update() {
                //touch zoom
                if (game.input.pointer1.isDown && game.input.pointer2.isDown) {
                    olddistance = distance;
                    distance = Phaser.Math.distance(game.input.pointer1.x, game.input.pointer1.y, game.input.pointer2.x, game.input.pointer2.y);
                    distancedelta = Math.abs(olddistance - distance);

                    if (olddistance > distance && distancedelta > 4) {
                        mapSizeCurrent -= 150;
                    } else if (olddistance < distance && distancedelta > 4) {
                        mapSizeCurrent += 150;
                    }
                    mapSizeCurrent = Phaser.Math.clamp(mapSizeCurrent, worldwidth, mapSizeMax); //prevent odd scalefactors - set a minimum and maximum scale value
                    worldScale = (mapSizeCurrent / mapSizeMax);

                    //calculate point between fingers (zoompoint.x and zoompoint.y)
                    if (game.input.pointer1.x < game.input.pointer2.x) {
                        zoompoint.x = game.input.pointer1.worldX + (Math.abs(game.input.pointer1.worldX - game.input.pointer2.worldX) / 2);
                    } else {
                        zoompoint.x = game.input.pointer2.worldX + (Math.abs(game.input.pointer1.worldX - game.input.pointer2.worldX) / 2);
                    }
                    if (game.input.pointer1.y < game.input.pointer2.y) {
                        zoompoint.y = game.input.pointer1.worldY + (Math.abs(game.input.pointer1.worldY - game.input.pointer2.worldY) / 2);
                    } else {
                        zoompoint.y = game.input.pointer2.worldY + (Math.abs(game.input.pointer1.worldY - game.input.pointer2.worldY) / 2);
                    }
                } else {  // wheelzoom
                    zoompoint.x = game.input.mousePointer.worldX;
                    zoompoint.y = game.input.mousePointer.worldY;
                }

                // move camera / pan
                if (game.input.activePointer.isDown && !game.input.pointer2.isDown) {
                    if (oldcamera) { // if moving the world always continue from the last position
                        game.camera.x += oldcamera.x - game.input.activePointer.position.x;
                        game.camera.y += oldcamera.y - game.input.activePointer.position.y;
                    }
                    oldcamera = game.input.activePointer.position.clone();
                }
                // adjust camera center and zoom here
                else {
                    oldcamera = null;
                    rescalefactorx = mapSizeX / (mapSizeX * stageGroup.scale.x); // multiply by rescalefactor to get original world value
                    rescalefactory = mapSizeY / (mapSizeY * stageGroup.scale.y);

                    prevScale.x = stageGroup.scale.x;
                    prevScale.y = stageGroup.scale.y;

                    nextScale.x = prevScale.x + (worldScale - stageGroup.scale.x) * easing;
                    nextScale.y = prevScale.y + (worldScale - stageGroup.scale.y) * easing;

                    var xAdjust = (zoompoint.x - game.camera.position.x) * (nextScale.x - prevScale.x);
                    var yAdjust = (zoompoint.y - game.camera.position.y) * (nextScale.y - prevScale.y);


                    //Only move screen if we're not the same scale
                    if (prevScale.x != nextScale.x || prevScale.y != nextScale.y) {
                        var scaleAdjustX = nextScale.x / prevScale.x;
                        var scaleAdjustY = nextScale.y / prevScale.y;
                        var focusX = (game.camera.position.x * scaleAdjustX) + xAdjust * (rescalefactorx);
                        var focusY = (game.camera.position.y * scaleAdjustY) + yAdjust * (rescalefactory);
                        game.camera.focusOnXY(focusX, focusY);
                    }

                    //now actually scale the stage
                    stageGroup.scale.x += (worldScale - stageGroup.scale.x) * easing;   //easing
                    stageGroup.scale.y += (worldScale - stageGroup.scale.y) * easing;
                }
            }
            function updateA() {
                groundobjects.destroy();
                stageGroup.remove(groundobjects);
            }
            function updateStars() {
                updateA();
                sprite = game.add.sprite(100+Math.random() * 900, 100+Math.random() * 900, 'dude');
                groundobjects.add(sprite);
                stageGroup.add(groundobjects);
            }
        </script>

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...