Skip to main content
Inspiring
May 2, 2014
Answered

Detect which object is clicked out of several objects.

  • May 2, 2014
  • 1 reply
  • 473 views

I want to make a game where it randomly generates a map, and players can click objects of the map. I want something to happen to that object, and I want the game to detect which object has been clicked, and temporarily assign that object a name so that I can do things to that object, I want the name to overwrite when I click a new object, and want to be able to retreive what I have done to the other objects. Any help would be appreciated.

This topic has been closed for replies.
Correct answer AngryGamerProductions

Sure.  If you create the objects dynamically  you can add the listeners while creating the objects.  They could all share the same event handler function if they will all be processed in a similar manner.


Thanks, you pointed me in the right direction, here was my result:

public var target

        public function DirtBlock() { //dirt block spawned

            trace("SPAWNED")

            this.addEventListener(MouseEvent.CLICK,fl_click)

           

        }

       

         function fl_click(event:MouseEvent):void { //dirt block clicked

            var dirtblock:DirtBlock = new DirtBlock();

            trace("CLICKED")

            trace(event.currentTarget) //traces which object was clicked(Object, dirtblock)

            target = event.currentTarget// sets the object to "target" so it can be manipulated

            target.visible = false

1 reply

Ned Murphy
Legend
May 3, 2014

You can use the target or currentTarget (if you assign listeners to each object) property of the clicking event to determine which object has been clicked and to manipulate that object.

Inspiring
May 4, 2014

Would there be a way to use a loop to add event listeners to each object? (I could have thousands of objects)

Ned Murphy
Legend
May 4, 2014

Sure.  If you create the objects dynamically  you can add the listeners while creating the objects.  They could all share the same event handler function if they will all be processed in a similar manner.