3.5.0-17 0 Newbie Poster

Couldn't find an actionscript section so I hope this is the next best section for such discussion. I'm trying to create an isometric game in adobe builder with AS3 along with the As3IsoLib isometric library. Currently upon clicking a unit you want to move I've got four boxes to popup in four direction either side of the selected unit (where you can move to).

I've added an event listener for all the boxes to call to, passing which box has called it.

      var BOX1:IsoBox = new IsoBox();var BOX2:IsoBox = new IsoBox();
      var BOX3:IsoBox = new IsoBox();var BOX4:IsoBox = new IsoBox();
      var BoxArray:Array = new Array(BOX1,BOX2,BOX3,BOX4);
                                               //Creating boxes and putting them into an array
      var LocArray:Array = new Array(player.x+50,player.y-150/*<NE*/,player.x+150,player.y-50,/*<SE*/player.x+50,player.y+50,/*<SW*/player.x-50,player.y-50/*<NW*/)//start with +50X and -50Y. That will be at same tile as player. Then adjust accordingly.

      var count:int = 0;  

      for each (var occr:IsoBox in BoxArray)
             {

             occr.setSize(100,100,1);
             occr.moveTo(LocArray[count],LocArray[count+1],1); //ADDING +50 to the players X and SUBTRACTING -50 to the players Y gets it to the same tile.

             DrawBox(occr);
             count = count+2;

             occr.addEventListener(MouseEvent.DOUBLE_CLICK, TileClicked(occr));

             }

      }

function TileClicked(Tile:IsoBox):void
     {

     player.x = Tile.x;
     player.y = Tile.y;

     }

But even though I haven't clicked any of the four boxes the 'TileClicked' function gets executed by the event handeler and it moves the player. I tried adding the event handler when I was defining all the boxes at the very top but the same problem happens.

Dani AI

Generated

For : the handler is being executed immediately because the function is being called when you add the listener instead of passing a function reference. In AS3 addEventListener expects a listener function with the MouseEvent signature; do not invoke it (no parentheses) when registering. Also enable double-click on the box objects if you use MouseEvent.DOUBLE_CLICK.

A simple, correct pattern is:

occr.doubleClickEnabled = true;
occr.buttonMode = true; // optional, shows hand cursor
occr.addEventListener(MouseEvent.DOUBLE_CLICK, onBoxDoubleClick);

function onBoxDoubleClick(e:MouseEvent):void {
  var box:IsoBox = e.currentTarget as IsoBox;
  player.x = box.x;
  player.y = box.y;
}

Notes and quick tips:

  • Use e.currentTarget (not e.target) to get the object the listener was attached to. Cast it to IsoBox.
  • If you prefer single clicks, use MouseEvent.CLICK instead of DOUBLE_CLICK.
  • If you need to pass extra parameters to the handler, wrap the call in an anonymous function when adding the listener, but store that function reference if you later want to remove the listener (anonymous functions are harder to remove).
  • If the handler still runs on creation, add trace statements where you add listeners and inside the handler to confirm who is calling it. Also check there are no other calls to the move code and that the boxes are mouseEnabled = true and on the display list.

This fixes the immediate execution problem and keeps your click handling predictable and easy to remove later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.