Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 47
Registered: ‎01-26-2011
My Device: Not Specified

Wait for a event before continuing

 


 

do
            {
                genPattern();
                getUser();
            } while (correctPattern());
                trace("You lose.");

 

public function getUser():void
        {
            red.addEventListener(MouseEvent.MOUSE_UP, addEntered);
            blue.addEventListener(MouseEvent.MOUSE_UP, addEntered);
            yellow.addEventListener(MouseEvent.MOUSE_UP, addEntered);
            green.addEventListener(MouseEvent.MOUSE_UP, addEntered);
            trace(i);
        }

 


 


When I run this, it runs getUser(), however it does not wait on the MouseEvent, therefore nothing is stored in correctPattern() so it exists the loop.

How can I make it wait till the user does the mouse event before continuing?

 

Developer
Posts: 117
Registered: ‎12-03-2010
My Device: Not Specified

Re: Wait for a event before continuing

[ Edited ]

euh...

addMouseEvent is like you tell that you would like to listen mouse event,

that doesn't mean that you are "waiting" mouse event!

 

You may need take a look about event-driven stuff...

 

For waiting, you could try: 

(1) set a flag to false

(2) loop till flag to true

(3) on listener ( when mouse event raise ), change the falg to true.

 

But this is very bad design.

You should not loop and wait.

You should try think about like, I do this action when mouse event raise,

so code your action on mouse listener.

 

 

---------------------
a) If you like my response/post, please provide a Kudo (white star to the left).
b) If my post solved your problem please click on the Accept as Solution button.

Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Wait for a event before continuing

ActionScript is an event driven system.  The event looping is done for you.  You just subscribe to an potentil event trigger and then you will receive it.  This is pervasive for the entire system.  The only main difference is the type of events that get sent.

Developer
Posts: 196
Registered: ‎02-10-2011
My Device: Playbook 16GB and Z10

Re: Wait for a event before continuing

Alright, I am trying to figure out a way to do this as well because throughout my app I have several URLLoaders and quite honestly I don't think its necessary to make a whole function for the listener, because doing so means I have to make several of the variables in that current function global so that the next function can add to them.

 

I just think that doing listeners like this results in a lot of unneccessary code with an obnoxious amount of global vars.

 

I would like to know what a professional would do to keep his/her code clean if he/she had to add display objects throughout their code based on URLLoaders with this kind of setup?

_________________________________________________
Apps
Go Music
Flashlight Fast Free/Pro
Fitbit for BlackBerry PlayBook
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Wait for a event before continuing

hey,

 

im not really a professional but here's what i do. I usually have a lot of "global" variables which i set (private or protected depnding on what type of class it will be) outside of the consturctor. this gives me easy access to any objects that i need at any given point. and you are correct in saying that you shouldnt have to create a new global variable for every time you call a listener. in AS3 it is recommended that you re-use your obejcts and variables instead of creating new ones whereever possible.

 

you can further clean your code by creating a lot of other classes and not just one main class for your application. this way everything is seperated and nicely organized so when you run into an error, you just have to go to a specific class and find the error instead of looking through a lot of lines of code in just one main class.

 

i usually create a new listener function for each object that i add a listener to. it allows me to keep track of whch object is listening and isnt at any given time. this again lets me track errors faster without having to try to guess what happened.

 

if you really dont want to go down that path, you can awlays use the e.target and the e.currentTarget object references. this will reference whatever object the listener is attached to. you can do a check like this:

 

 

if (e.currentTarget is MyCustomClass)
{
    //do stuff

}

 

to know what obejct called it. to me its a little more complicated than just creating seperate listeners. hope that helps shed some light. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 196
Registered: ‎02-10-2011
My Device: Playbook 16GB and Z10

Re: Wait for a event before continuing

Ok I think I understand now.  For example, if I am adding items to the stage I should only make a var for each unique item on the display, then when I transision to a new scene then I can just reuse those same objects but giving them new properties?

 

That way I can make functions (or functions linked together by listeners) to build scenes.  Still seems kind of silly to have to link functions together by listeners instead of having the ability to wait on the event dispatcher to dispatch either an error or a success event, but I can do a flow like this.

_________________________________________________
Apps
Go Music
Flashlight Fast Free/Pro
Fitbit for BlackBerry PlayBook
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Wait for a event before continuing

what i do is everytime i have a new "page" or "screen" its a new class. if i need to leave that screen i keep it in the background. and if i need to re-visit hte page, i reset all the variables that i used and add it back as the top most page.

 

im not sure i understand the listener part though, can you clarify the linking?

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 196
Registered: ‎02-10-2011
My Device: Playbook 16GB and Z10

Re: Wait for a event before continuing

The app I am working on does this...

(pseudo-code)

 

URL request

URL load

loader.addListener(complete, additems)

 

fuction additems 

// use the data from the url response to build a couple items

// then call a different url

URL request

URL load

loader.addListener(complete.addotheritems)

 

and so on.  I have to do this several times throughout the app to get data from several different urls, so for me to build it correctly I have to keep adding an event listener and create a function to handle the data, instead of just waiting for the complete event and then using the data in the current function.  Thats what I mean by linking them together.  I have to end my function with an adding of an event listener to just go to another function simply to get data from a URL load.

_________________________________________________
Apps
Go Music
Flashlight Fast Free/Pro
Fitbit for BlackBerry PlayBook
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Wait for a event before continuing

you could some kind of recursive function, but you have to ensure you have an exit plan. other wise it would keep looping through the funciton. so you can do something like e.currentTarget to reference the current object doing the URLLoader function.

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman