开发者

AS3 DispatchedEvent not being heard by listener

开发者 https://www.devze.com 2023-02-16 13:19 出处:网络
Hey so i have had a look at some other posts but im still not getting anywhere. I have 2 classes, one is Ticket_mc() and the other is TicketPurchaserFSlider(). there is a number stored in TicketPurc

Hey so i have had a look at some other posts but im still not getting anywhere.

I have 2 classes, one is Ticket_mc() and the other is TicketPurchaserFSlider(). there is a number stored in TicketPurchaserFSlider() called ticketsSelected which holds the number of tickets i have selected. Now what i want to do is to be able to increment that number by one from the Ticket_mc() class. and how i set that up was to create an dispatchEvent(new Event("selectFromList")), and then a listener and a function conected to that listener. anway its not working. Here is my code:

    package com.spay.ticketpurchaser {

    import com.spay.ticketpurchaser.TicketPurchaserFSlider;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.EventDispatcher;


    public class Ticket_mc extends MovieClip{
        public function Ticket_mc(){
            addEventListener(MouseEvent.CLICK, clickTicket);
        }

        public function clickTicket(event:MouseEvent){
            dispatchEvent(new Event("selectFromList"));
            gotoAndStop(2);
        }
    }
}

.

    package com.spay.ticketpurchaser{

    import com.spay.ticketpurchaser.Ticket_mc;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.text.TextField;    

    public class TicketPurchaserFSlider extends MovieClip{

        public var ticketsSelected:Number = 0;
        private var ticket:Ticket_mc;

        public function TicketPurchaserFSlider(){           
            ticket = new Ticket_mc();
            ticket.addEventListener("selectFromList",addTicket , false,0,true);
            }

        private function addTicket(event:Event){
            ticket.removeEventListener("selectFromList", ad开发者_开发问答dTicket);
            ticketsSelected ++;
            trace("it works");
        }
   }
}

For clarification, here is the scenerio

I have a ticketPurchaserFSlider class that holds my ticketsSelected var. I have other methods inside the TicketPurchaserFSlider class that increments and decrements the ticketsSelected var and it all works just fine, i then use the document class to addChild(ticketPurchaser) and so on.

I then needed the tickets, and these tickets needed to also be clickable and also increment and decrement the ticketsSelected var in the ticketPurchaserFSlider class. To display the tickets i first create the Ticket_mc and exported for actionsctript. I then created an empty MovieClip and gave it a class called Ticket_Group, this class holds an Array that created 6 instances of the tickets. I then created another empty movieclip and created another class and called it Ticket_Batch, this also holds an Array and creates more instances of tickets (in multiples of 6) the number of tickets it is creating is stored in ticketPurchaserFSlider class and is called maxTickets. i access this number by using the an instance of the ticketPurchaserFSlider and using the . syntax ( ticketPurchaser.maxTickets) i then use an instance of the Ticket_Batch class in the Document class to addChild(tickets), so i have all of my tickets displays from the document class.

now im not sure if this was the best way to display the tickets, but it made logical sense to me and the tickets will be singlely selected and selecable by multiples of 6 (using a seperate button) and in both cases i need to be able to access the ticketsSelected var in the ticketPurchaserFSlider class.

It fires off okay from the clickTicket function.

I hope that has cleared things up. If you think i need a better way of setting up my classes then do say so.

I have posted the files here: www.samuelpay.com/BingoClient.zip so take a look if you want too.


Mouse event listeners called only if their target are in display list. You need to add ticket in display list:

ticket = new Ticket_mc();
ticket.addEventListener("selectFromList",addTicket , false,0,true);
addChild(ticket);


Put a trace inside this function of yours:

public function clickTicket(event:MouseEvent){
    dispatchEvent(new Event("selectFromList"));
    gotoAndStop(2);
}

Let us know if it fires, i.e. if the mouse click is being handled. Then we'll know more about where the problem lies.

Cheers, Timo


Yes it sounds like you have a problem with instances here, you must listen for event on the particular instance. One solution might be to pass the array of the Ticket_mc instances to TicketPurchaserFSlider so you can add the event listeners for each instance. An other, probably better way, is to let Ticket_Batch listen for clicks and then redispatch to TicketPurchaserFSlider.


This is how I would do it (code not tested, might be typos):

package com.spay.ticketpurchaser
{

    import com.spay.ticketpurchaser.Ticket_mc;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.text.TextField;    

    public class TicketPurchaserFSlider extends MovieClip
    {

        //public var ticketsSelected:Number = 0;
        //private var ticket:Ticket_mc;
        private var _tickets:Array;

        public function TicketPurchaserFSlider(tickets:Array)
        {     
            _tickets = tickets;  
            setUpTicketListeners();  
            //ticket = new Ticket_mc();
            //ticket.addEventListener("selectFromList",addTicket , false,0,true);
            }

        private function setUpTicketListeners():void
        {
           for(var i:int = 0; i<_tickets.length; i++)
           {
               _tickets[i].addEventListener(MouseEvent.CLICK, onTicketClick);
           }
        }       

        private function onTicketClick(event:MouseEvent):void
        {
           var currentTicket:Ticket_mc = Ticket_mc(event.currentTarget);
           //do your stuff
        }

        //private function addTicket(event:Event)
        //{
           // ticket.removeEventListener("selectFromList", addTicket);
           // ticketsSelected ++;
           // trace("it works");
        //}
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消