0

I have this problem with removing objects data on easelJs.

I have a canvas where I create a segment called overlay, then I have the ability yo create and delete small points called "seeds. What I'm trying to do is store the x,y coordinates into an array only if the seeds places are in the hitarea of the overlay image.

this is the code:

     function addSeed(){

               seed = new createjs.Bitmap("seed.jpg");
               seed.alpha = 0.5;
               seed.x = stage.mouseX-10 ;
               seed.y = stage.mouseY-10 ;
                       addSeedData(); 
                       stage.addChild(seed);
                       stage.update(); 
                       storex.push(seed.x); 
                       storey.push(seed.y);
                       i+=1;
                      }


     function removeSeed(){
            if(stage.getNumChildren()>childCounter){


            removeSeedData();
            storex.pop(seed.x);
            storey.pop(seed.y);

            stage.removeChildAt(stage.getNumChildren()-1);
            stage.update();

                                        }
         }

     function addSeedData(){
           overlayPt = seed.localToLocal(10, 10, overlay);
           if (overlay.hitTest(overlayPt.x, overlayPt.y)== true)
                {
                 overlaySeedDatax.push(seed.x);
                 overlaySeedDatay.push(seed.y);
                }
           }
     function removeSeedData(){

          if (overlay.hitTest(overlayPt.x, overlayPt.y)== true)
                {
                 overlaySeedDatax.pop(seed.x);
                 overlaySeedDatay.pop(seed.y);
                }
          }

I can accurately add and remove seeds from innside the overlay object butttt when I add one seed inside then add another outside than add inside and then a fourth outside... then delete all the seeds, the 2 seeds still remain in the overlaySeedData array

Ryan Williams
  • 623
  • 3
  • 11
  • 27

1 Answers1

0

I think your problem is with your usage of the pop function. This function only removes the last element of the array. So it looks like you might be removing the last array element and not the one you really intended to remove. When deleting elements you should really be doing it by specifiying its index.

How do I remove a particular element from an array in JavaScript?

You can check out the splice function for a way to remove array elements by their index.

Community
  • 1
  • 1
Josh
  • 1,230
  • 2
  • 16
  • 31