Reputation: 3
I'm trying to use shared object but i don't know how do i save event listeners (e.g. if a mouse event has already removed before saving). Let's say that "onThis" function removes a mouse event
function onThis(event:MouseEvent):void
{
levAll.theLev.gotoAndStop(2);
boxAll.levHit.removeEventListener(MouseEvent.CLICK, onLevHit);
}
//save function
function onSaveBtn(event:MouseEvent):void
{
sharedObject.data.levData = levAll.theLev.currentFrame;
// save that boxAll.levHit mouse event removed
}
//load function
function onLoadBtn(event:MouseEvent):void
{
levAll.theLev.gotoAndStop(sharedObject.data.levData);
// load that boxAll.levHit mouse event removed
}
Amir, sorry, i just saw your answer but i don't know how to use your code example in a full project. I have tried the following code (see EDIT). It works although it does not seem very elegant, especially if i use it to all Listeners i have. Could you please give me an example on this? Thanks!*
EDIT
//public class
var hiton:Boolean;
//initialization
hiton = true;
boxHit.panel.addEventListener(MouseEvent.CLICK, onBoxPanel);
boxHit.addEventListener(MouseEvent.CLICK, onBoxHit);
saveBtn.addEventListener(MouseEvent.MOUSE_DOWN, onSaveBtn);
loadBtn.addEventListener(MouseEvent.MOUSE_DOWN, onLoadBtn);
// functions
function onBoxPanel(event:MouseEvent):void
{
hiton = false;
}
function onBoxHit(event:MouseEvent):void
{
if(hiton){
//do something
}
}
function onSaveBtn(event:MouseEvent):void
{
sharedObject.data.hitData = hiton;
}
function onLoadBtn(event:MouseEvent):void
{
hiton = sharedObject.data.hitData;
}
Upvotes: 0
Views: 1383
Reputation: 621
You shouldn't ever need to "save" your event listeners for some buttons in a shared object in the way that you are describing; you must be approaching this from the wrong angle.
It's hard to say what you should be doing instead, since you haven't really explained what you're trying to accomplish and your variable names and function names aren't meaningful. However, in general, what you want to do is save the state of the object that the button interacts with, and add or remove the event listener based on that state.
function onThis(event:MouseEvent):void
{
levAll.theLev.gotoAndStop(2);
boxAll.levHit.removeEventListener(MouseEvent.CLICK, onLevHit);
boxAll.levHit.wasClicked = true;
}
function onSaveBtn(event:MouseEvent):void
{
sharedObject.data.levData = levAll.theLev.currentFrame;
sharedObject.data.clickedLevHit = boxAll.levHit.wasClicked;
}
function onLoadBtn(event:MouseEvent):void
{
hiton = sharedObject.data.hitData;
if (!sharedObject.data.clickedLevHit) {
boxAll.levHit.addEventListener(MouseEvent.CLICK, onLevHit);
}
}
Upvotes: 0
Reputation: 768
I'm not sure that I understand it completely but maybe
var EventExists:Boolean ;
EventExists=boxAll.hasEventListener(MouseEvent.CLICK);
sharedObject.data.levData = EventExists;
Upvotes: 1