Copy link to clipboard
Copied
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.
1 Correct answer
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 contro
...Copy link to clipboard
Copied
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.
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.

