Skip to main content
Participant
February 25, 2012
Answered

How do you click on object and then unclick that onbject to then click on another object?

  • February 25, 2012
  • 1 reply
  • 1792 views

I am building a simple game in which you must click on a square to activate it, meaning when you click on it you have control of its movement. I then want to be able to click on the square again so that you don't have control of it, and then you can click on another object in the game. What is the code to allow this? I believe its an eventListener for a mouse event.

This topic has been closed for replies.
Correct answer Ned Murphy

Clicking either way involves assigning an event listener for the CLICK event, as in...

yourSquare.addEventListener(MouseEvent.CLICK, manageControl);

wherein bth the name of the square that I used (yourSquare) and the name of the event handler function (manageControl) are named however you want them named.  The real work gets done in the event handler function...

function manageControl(evt:MouseEvent):void {

     // this is where your code will go to determine if the square has been clicked to control it versus clicked to give up control.

}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 25, 2012

Clicking either way involves assigning an event listener for the CLICK event, as in...

yourSquare.addEventListener(MouseEvent.CLICK, manageControl);

wherein bth the name of the square that I used (yourSquare) and the name of the event handler function (manageControl) are named however you want them named.  The real work gets done in the event handler function...

function manageControl(evt:MouseEvent):void {

     // this is where your code will go to determine if the square has been clicked to control it versus clicked to give up control.

}

mrb7416Author
Participant
February 25, 2012

I got that to work, except I can't unclick. When I click on an object the user has control of it, and then when they click on another object they control both of them at once.

Ned Murphy
Legend
February 25, 2012

That is not the situation you originally described.  You wanted to know how to deal with clkicking the same object and having it controlled versus not controlled depending on what state it is currently in.

If you want clicking another object to disabkle the control of another, then you need to have something store the information as to what object is currently under control so that you can discontinue its control... OR... have a function that disables all objects that you call first when you click an object, then set the control for the clicked object.