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
Harry_Dodgson
Posts: 151
Registered: ‎10-26-2010
My Device: Bold 9900
My Carrier: AT&T

How can I make a sleep() function work in Actionscript on the simulator

Hi,

 

I've been trying all sorts of combinations, but I can't seem to make something that does:

 

change sprite

show sprite

sleep for a 1/2 second

change another one

show it

sleep for a second

...

show last change

 

While I can delay execution, the updates don't show until the last one completes.  That means I click the mouse and several seconds later the display shows the final result, not any of the intermediate steps that I'd like to show along the way. 

I've tried setInterval, simple counter loops, and while loops based on values updated in TimerEvents.

I really don't want to mess up an easy to follow program by putting a state machine into the TimerEvent function to kick off each part of a display update.

 

Harry

 

--------------
Accepted PlayBook Applications: Marmi-doos, BASIC, Run and Hide
Please use plain text.
Developer
pedgarcia
Posts: 119
Registered: ‎03-10-2010
My Device: Bold 9000
My Carrier: Rogers

Re: How can I make a sleep() function work in Actionscript on the simulator

I'm not experienced with ActionScript whatsoever, but based on what you're saying, you're trying to add loops inside an event handler, and normally those things don't work well on event-driven platforms. The problem is that, if you take a long time inside an event, all other events would be delayed. When some of them demands screen updates, the updates won't happen until the whole process is finished, which seems is happening to you.

 

It is not clear if you're trying to show Sprites as a full window, like a slide show, or your sprite is a animation on the main screen. Either way, I think the best to do is to implement an Update event handler, and make the timing calculations using the frame rate.

 

To create this event handler, you should add a listener like:

 

 

stage.addEventListener(Event.ENTER_FRAME, updateEvent);

 

 

 

As default, this event will be called 30 times per second. Based on that, you can calculate the time between each sprite.

 

I don`t believe you can avoid creating some kind of state machine, but you could simply add all Sprites to an array at the beginning of your application, and everytime the desired time is elapsed, you could remove the current Sprite and add the n+1 from your array. 

 

The only rule that you should follow is never add some kind of Sleep inside an event handler.

 

I hope it helps.

 

Regards

 

 

 

 



(*) Kudo-me if you really think I helped you!
My plug: I'm doing a Mobile Experiment, reported on my Blog: Check it out!.
---------------------------
Paulo Garcia
http://www.mobilecreators.com - Twitter: http://twitter.com/mobilecreators
Please use plain text.
Developer
shawnblais
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: How can I make a sleep() function work in Actionscript on the simulator

If you're using a Timer, make sure you're listening for the TIMER event., and not TIME_COMPLETE, the latter is when yout timer has completed all it's loops, where the former will fire each time the timer reaches 0.

 

It does sounds like a simple ENTER_FRAME handler is all you need though.

Please use plain text.
Developer
Harry_Dodgson
Posts: 151
Registered: ‎10-26-2010
My Device: Bold 9900
My Carrier: AT&T

Re: How can I make a sleep() function work in Actionscript on the simulator

As an example (not doing this game yet, though my wife would like it if I did).

 

When I wrote "Lynx Othello" for the Atari Lynx, the player chooses a blank spot to place their piece,  Then I put it there, wait a bit so they see it's there.  Then I go around flipping pieces one at a time as appropriate.   There are waits in between each flip and slightly longer waits when I go from one row/column/diagonal to the next one.  That way the player can see I'm not cheating by flipping them all at once.

 

With what I'm doing now, I'm not doing the same thing each time, so it's more complicated.  The player clicks the screen.  I do some of routine A in a loop which only delays if it actually changed something.  Then I do routine B which also delays if the screen updates and so on.  When it is done, it goes back to waiting on the next mouse click.

 

So if I understand correctly, I need to check a flag in the frame event routine to see if it needs to do routine A and if so, starts it.  When routine A sees it has to delay, it saves it's state and exits.  then the next time period comes by and routine A is called to continue.  When routine A is done, it sets the flag to do routine B and the process continues.  I would also have to add a flag to the mouse click handler to disable it while all this is going on.  Talk about intentional obsfucation...

 

I would think that a multi-tasking system would have a nicer way to do this, like javascript has the Thread.sleep and Thread.release controls.

 

--------------
Accepted PlayBook Applications: Marmi-doos, BASIC, Run and Hide
Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: How can I make a sleep() function work in Actionscript on the simulator

The Timer class is probably your best bet to delay an execution or to animate something since most of the effects class (move,resize,fade,etc.) are not yet available.  You can also subscribe to the frame events, but will need to manage a fair amout of state information since you mention you have different logic paths based on user action.  Aslo, if processing takes more than a frame length (1/30 second), you might get some hicups (again, depends on exactly what you are tyring to do).

Please use plain text.
Developer
shawnblais
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: How can I make a sleep() function work in Actionscript on the simulator

Nono, we don't animate things ourselves in Flash!

 

Use one of the many great AS3 tween libraries availlable. The built in Flex stuff is less performant and more work anyways.

 

My personal favorite is GTween,  to an animate something it's dead simple:

 

new GTween(target, duration, props, opts);

 

Or:

 

new GTween(mySprite, .5, {x: 100, alpha: 1, scaleX: 2}, {onComplete: completeHandler, ease: Back.easeOut})

 

 

 

TweenMax is another popular one.

Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: How can I make a sleep() function work in Actionscript on the simulator

Native effects (resize,move,fade,etc.) do not appear to be apart of the BB SDK (please correct if wrong).  Do you have a suggested 3rd party effects library that works with the BB SDK?

Please use plain text.
Developer
haimez
Posts: 23
Registered: ‎10-26-2010
My Device: 9650
My Carrier: VZW

Re: How can I make a sleep() function work in Actionscript on the simulator

Without having the SDK in front of me, couldn't you dynamically create a function variable (var update:Function = function ) that is built specifically to handle the current state of the board?

It seems like if you did it that way you could base your timing around the frame events and front-load the looping to before you're performing graphical operations.
Please use plain text.
Developer
shawnblais
Posts: 439
Registered: ‎10-25-2010
My Device: Not Specified

Re: How can I make a sleep() function work in Actionscript on the simulator

There's nothing special about the BbSDK its just a set of regular flash display objects, all display objects have alpha, scale, x, y, z etc. Fadeout is just: new GTween(target,1, {alpha:0}) Move is simply: new GTween(target, 1, {x:100,y:100}) You're overthinking it a bit...
Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: How can I make a sleep() function work in Actionscript on the simulator

As far as I can find in the documentation, GTween is not currently supported in the BB SDK.  It appears to be supported in Flex SDK 4.x as part of the new Spark framework, but the BB SDK does not currently support all of the 4.x framework (except for items under flash.*).  If you can show differently, I would personally love to take advantage of some f/x that is commonplace in Flash.

 

If someone from BB can chime in on this, it seems to be a regular discussion of what exactly the BB SDK includes and does not include.  A table of classes in the MX and Spark frameworks of what is curently supported, replaced by the BB SDK and what might be supported (product roadmap), would be very helpful.

 

Example:

mx.controls.Button (use qnx.ui.buttons.LabelButton)

mx.effects.WipeDown (planned for 0.9.x release)

mx.effects.Fade (planned for 0.9.x release)

etc.

Please use plain text.