Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Detect which object is clicked out of several objects.

Explorer ,
May 01, 2014 May 01, 2014

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.

TOPICS
ActionScript
484
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , May 04, 2014 May 04, 2014

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, dirtblo

...
Translate
LEGEND ,
May 03, 2014 May 03, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 04, 2014 May 04, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 04, 2014 May 04, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 04, 2014 May 04, 2014
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines