Reputation: 139
This is AS3 question. I have Class "addadd", "Symbol1"(Button1), "Symbol2"(Button2). In "addadd" I have 3 functions. Function "StanishevFadd" creates object and add Event Listener, function "stanishevFremove" removes Event Listener and function "frameDOstanishev" that will be triggered when the event is added. So, I obviously want to trigger the event from Symbol1 and stop the same event from Symbol2. Unfortunately doesn't work this way. I am totally confuzed. For example if I want to add and remove the event listener from Symbol1 only - YES works, but I am not able to create event from Symbol1 and remove the same event from Symbol2 in class addadd. Please help guys, please!
public class addadd
{
var stanishev:stanishev_line = new stanishev_line;
public function addadd()
{
}
public function stanishevFadd()
{
Main.display.addChild(stanishev);
stanishev.gotoAndPlay("start");
stanishev.addEventListener(Event.ENTER_FRAME, frameDOstanishev);
}
public function stanishevFremove()
{
stanishev.removeEventListener(Event.ENTER_FRAME, frameDOstanishev);
}
public function frameDOstanishev(e:Event)
{
trace (stanishev.currentFrame);
}
}
//------------------------------
public class Symbol1 extends SimpleButton
{
var call_creator:addadd = new addadd;
public function Symbol1()
{
// constructor code
addEventListener(MouseEvent.MOUSE_OVER, eventResponse);
}
function eventResponse(e:MouseEvent)
{
call_creator.stanishevFadd();
}
}
//----------------------
public class Symbol2 extends SimpleButton
{
var call_creator:addadd = new addadd;
public function Symbol2()
{
// constructor code
addEventListener(MouseEvent.MOUSE_DOWN, eventResponse2);
}
function eventResponse2(e:MouseEvent)
{
call_creator.stanishevFremove();
}
}
Upvotes: 0
Views: 1283
Reputation: 6961
OK, I am not sure what the overall purpose of this whole thing, so I am going to call this thing OddView. I'm going to try to get close to what it looks like you are trying to do in a more OO way.
I believe it should be possible for you to encapsulate all of this behavior without involving your Main Class, and there are a lot of reasons not to involve it, especially not with static variables like you're doing.
public class OddView extends Sprite { protected var _btn1:SimpleButton; protected var _btn2:SimpleButton; protected var _stanishev:StanishevLine;//note I changed your Class name to be more in line with AS3 standards public function OddView() { super(); } public function get btn1():SimpleButton { return _btn1; } public function set btn1(value:SimpleButton):void { if (value != _btn1) { if (_btn1) { removeEventListener(MouseEvent.MOUSE_OVER, goToState2); } _btn1 = value; if (_btn1) { _btn1.addEventListener(MouseEvent.MOUSE_OVER, goToState2); } } } public function get btn2():SimpleButton { return _btn2; } public function set btn2(value:SimpleButton):void { if (value != _btn2) { if (_btn2) { removeEventListener(MouseEvent.MOUSE_OVER, goToState1); } _btn2 = value; if (_btn2) { _btn2.addEventListener(MouseEvent.MOUSE_OVER, goToState1); } } } public function get stanishev():StanishevLine { return _stanishev; } public function set stanishev(value:StanishevLine):void { if (value != _stanishev) { if (_stanishev) { cleanUp(null); } _stanishev = value; initStanishev(); } } public function initStanishev():void { if (_stanishev) { _stanishev.visible = true; _stanishev.goToAndPlay('start'); addEventListener(Event.ENTER_FRAME, showStanishevFrame); addEventListener(Event.REMOVED_FROM_STAGE, cleanUp); } } public function goToState1(e:Event) :void { goToAndStop(1); } public function goToState2(e:Event):void { goToAndStop('State2'); } public function showStanishevFrame(e:Event):void { if (stanishev) { trace('current frame', stanishev.currentFrame); } } public function cleanUp(e:Event):void { removeEventListener(Event.ENTER_FRAME, showStanishevFrame); removeEventListener(Event.REMOVED_FROM_STAGE, cleanUp); } }
Note that I'm assuming that you're going to apply OddView as the Base Class of a library symbol. I'm not including any instantiation or positioning logic, since I tend to use the stage for those things.
Note that the reason I check for existence on the stage instances is that it is theoretically possible for someone to assign OddView as the Base Class and not put a btn1, btn2, or stanishev on the stage of that library symbol.
However, this Class is written assuming you're going to handle the visual parts of those 3 items by placing btn1, btn2, and stanishev on the stage where you want them to be.
Also note that I strongly suspect that most of this is completely unnecessary. It's quite possible that you could handle most of this by simply using the over state of btn1. However, that doesn't completely account for why you want to only remove the event listener and take no other action when btn2 is cicked. So I went for "overkill" in the absence of real info about what you're actually trying to accomplish.
Note on the edits
Note what I'm doing there in the setter--if there was already a value stored, we remove the listeners that were on that old, outgoing, listener. Then if the incoming value is not null, we add the new listener.
I still suspect you don't need to do so much, but considering the information you have provided, this should help point you in the right direction.
Upvotes: 1
Reputation: 4233
You could make the variable stanishev
in you addadd
class a static variable. However I would not recommend doing this, one reason being you can only have one instance of stanishev
. Hopefully @Amy Blankenship will tell you how to completely redesign your program, because it needs to be redesigned. This approach will work though, you should also add conditionals to the functions that add and remove eventListeners to stanishev
to see if stanishev
already has an eventlistener or does not have one.
public class addadd
{
public static var stanishev:stanishev_line = new stanishev_line;
public function addadd()
{
}
public function stanishevFadd()
{
Main.display.addChild(stanishev);
stanishev.gotoAndPlay("start");
stanishev.addEventListener(Event.ENTER_FRAME, frameDOstanishev);
}
public function stanishevFremove()
{
stanishev.removeEventListener(Event.ENTER_FRAME, frameDOstanishev);
}
public function frameDOstanishev(e:Event)
{
trace (stanishev.currentFrame);
}
}
public class Symbol1 extends SimpleButton
{
public function Symbol1()
{
// constructor code
addEventListener(MouseEvent.MOUSE_OVER, eventResponse);
}
function eventResponse(e:MouseEvent)
{
addadd.stanishev.stanishevFadd();
}
}
public class Symbol2 extends SimpleButton
{
public function Symbol2()
{
// constructor code
addEventListener(MouseEvent.MOUSE_DOWN, eventResponse2);
}
function eventResponse2(e:MouseEvent)
{
addadd.stanishev.stanishevFremove();
}
}
Upvotes: 0