Flashguy’s Blog
Wicked Actionscript

Custom Timer Class with pause functionality

August 18th, 2010 by Gabor Wraight

In my current Project I came across an issue that bugged me for a while.

The thing is, I'm building a game where a timer is used to jump to the next level(e.g. every 30 seconds the level goes one up). Unfortunatly, the game has a pause mode if you press "p" on the keyboard. I needed to pause and not stop the leveltimer, to be able to continue with the time left in the current cycle. Otherwise you could easily cheat by pausing the game just before you get to the next level. If you would start the normal timer again, you would again have 30 seconds untill the next level.

So here's what I came up with:

Actionscript:
  1. package de.rockbox.utils
  2. {
  3.  
  4. import flash.events.TimerEvent;
  5. import flash.utils.Timer;
  6. import flash.utils.getTimer;
  7.  
  8. public class RBTimer extends Timer
  9. {
  10. private var initDelay:Number;
  11. private var startTime:int;
  12. private var timeLeftCurrentCicle:int;
  13.  
  14. public function RBTimer(delay:Number, repeatCount:int=0)
  15. {
  16. super(delay, repeatCount);
  17.  
  18. // save the initial delay for later use
  19. initDelay = delay;
  20.  
  21. // save start time to calculate leftover time when paused
  22. startTime = getTimer();
  23.  
  24. // listener to reset the delay to its initial value
  25. addEventListener(TimerEvent.TIMER,onTimer);
  26. }
  27.  
  28. private function onTimer(event:TimerEvent):void
  29. {
  30. // reset the delay to ist initial value
  31. delay = initDelay;
  32.  
  33. // reset & save start time to calculate leftover time when paused
  34. startTime = getTimer();
  35. }
  36.  
  37. public function togglePause():void
  38. {
  39. // toggle pause
  40. if(running){
  41. // get the current time
  42. var timeNow:int = getTimer();
  43.  
  44. // calculate the elapsed time since the timer was started
  45. var elapsed:int = timeNow - startTime;
  46.  
  47. // calculate the time left in the current ccle
  48. timeLeftCurrentCicle = delay - elapsed;
  49.  
  50. // stop the timer
  51. stop();
  52.  
  53. // set delay to the time left in the current cycle
  54. delay = timeLeftCurrentCicle;
  55. }else{
  56. // reset & save start time to calculate leftover time when paused
  57. startTime = getTimer();
  58.  
  59. // start the timer with the new delay
  60. // as soon as TimerEvent.Timer is dispatched,
  61. // the delay is reset to its initial value.
  62. start();
  63. }
  64. }
  65. }
  66. }

Use this class just like the standard timer class:

Actionscript:
  1. var timer:RBTimer = new RBTimer(1000);
  2. timer.start();
  3.  
  4. // to toggle pause
  5. timer.togglePause();

Posted in Actionscript 3.0 | No Comments »

New Web2Print Job

August 12th, 2010 by Gabor Wraight

Designskins.com

Designskins.com is a website which lets you choose from a large list of devices such as your mobile phone. You can then customize a skin for your device using images from the gallery or even upload custom images. Add text and some icons and send your skin to the cart. The server then generates a pre-press concidering all the elements on your skin. The guys from designskins then print your skin on their very own foil. As soon as you get your skin via post, you just stick it onto your device and show off in front of your friends.

The Project is built entirely in Flashbuider(Flex) using only state of the art techniques. Feel free to check it out and play with them.

Posted in Actionscript 3.0, Flashbuilder, Flex, Work | No Comments »

New Office

July 7th, 2010 by Gabor Wraight

Last Thursday we moved into our fabulous new office.

You can now find me at:

Deisenhofener Straße 1
D-81539 München
co Hello AG

+49 178 2441939
+49 89 244163391

Looking forward to seeing you...

Posted in Work | No Comments »

Flashbuilder – setting Text in the Richtext Component

March 16th, 2010 by Gabor Wraight

Just to file this for myself. Peter deHaan wrote this great Post on the many different ways of setting text in a spark RichText Component.
Check it out here.

My favourite is getting HTML Markup from a String into the RichText Component. Perfect for localisation.

Actionscript:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- http://blog.flexexamples.com/2009/08/11/setting-text-in-a-spark-richtext-control-in-flex-4/ -->
  3. <s:Application name="Spark_RichText_text_test"
  4.         xmlns:fx="http://ns.adobe.com/mxml/2009"
  5.         xmlns:s="library://ns.adobe.com/flex/spark"
  6.         xmlns:mx="library://ns.adobe.com/flex/halo">
  7.  
  8.     <fx:Script>
  9.         <![CDATA[
  10.             import spark.utils.TextFlowUtil;
  11.         ]]>
  12.     </fx:Script>
  13.  
  14.     <fx:Declarations>
  15.         <fx:String id="htmlTextAsMarkup"><![CDATA[<p>The quick brown <span fontWeight="bold">fox jumps over</span> the lazy dogg.</p>]]></fx:String>
  16.     </fx:Declarations>
  17.  
  18.     <s:RichText id="richTxt"
  19.             textFlow="{TextFlowUtil.importFromString(htmlTextAsMarkup)}"
  20.             horizontalCenter="0" verticalCenter="0" />
  21.  
  22. </s:Application>

By the way, you should definitely bookmark his Blog. It's my best resource on Flashbuilder and Flex Examples.

Posted in Flashbuilder | No Comments »

Getting a “Shake” Event on the iPhone with Flash CS5

February 8th, 2010 by Gabor Wraight

If you need to find out if a User shakes his iPhone the following might help:

Actionscript:
  1. var accel:Accelerometer = new Accelerometer();
  2. accel.addEventListener(AccelerometerEvent.UPDATE, onAccelUpdate);
  3.  
  4. var lastAccelX:Number;
  5. var lastAccelY:Number;
  6. var lastAccelZ:Number;
  7.  
  8. var shaked:Boolean;
  9. var shakeTreshhold: Number = 1.5
  10.  
  11. function onAccelUpdate(e:AccelerometerEvent):void
  12. {
  13.     if ((e.accelerationX - lastAccelX> shakeTreshhold)||(e.accelerationY - lastAccelY> shakeTreshhold)||(e.accelerationZ - lastAccelZ> shakeTreshhold))
  14.     {
  15.         if(shaked)
  16.             return;
  17.            
  18.         shaked = true;
  19.         // do your shake action here!!!
  20.  
  21.         var t:Timer = new Timer(800, 1);
  22.         t.addEventListener(TimerEvent.TIMER, function onTimer():void{
  23.             shaked = false;
  24.         });
  25.         t.start();
  26.     }
  27.    
  28.     lastAccelX = e.accelerationX;
  29.     lastAccelY = e.accelerationY;
  30.     lastAccelZ = e.accelerationZ;
  31. }

What I do here is listen for accelerometer update Events and compare the values from the last with the current Event. If the delta is higher than the defined treshold value, the phone is shaken. To avoid to many shakes after another I set a timer. I'm not sure, if this is the best practice for doing this, but it works out well in my App.

Posted in Flash CS5, iphone | No Comments »

Determining the direction of a Gesture in Flash CS5 on the iPhone

February 5th, 2010 by Gabor Wraight

If you need to find out in what direction a gesture goes, e.g. Up, Down, Left or Right the following Script might help.

Actionscript:
  1. this.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin)
  2. this.addEventListener(TouchEvent.TOUCH_END, onTouchEnd)
  3.  
  4. var startPoint:Point;
  5. var endPoint:Point;
  6.  
  7. function onTouchBegin(e:TouchEvent):void
  8. {
  9.     startPoint = new Point(e.localX, e.localY);
  10. }
  11. function onTouchEnd(e:TouchEvent):void
  12. {
  13.     endPoint = new Point(e.localX, e.localY);
  14.     var a:Number = endPoint.y - startPoint.y;
  15.     var b:Number = endPoint.x - startPoint.x;
  16.    
  17.     if(a*a> b*b){
  18.         if(a> 0){
  19.             //down
  20.         }else{
  21.             //up
  22.         }
  23.     }else{
  24.         if(b> 0){
  25.             //right
  26.         }else{
  27.             //left
  28.         }
  29.     }
  30. }

Posted in Actionscript 3.0, Flash CS5, iphone | No Comments »

Adding a hyperlink to a Flashbuilder RichText Component

January 23rd, 2010 by Gabor Wraight

First of all be aware to use RichEditableText instead of the RichText. Unfortunatly I couldn't get links to work in the RichText Component. Also set editable="false" focusEnabled="false" in the RichEditableText Component to make it clickable

Actionscript:
  1. <s:RichEditableText editable="false" focusEnabled="false">
  2.     <s:content><s:p><s:span>some Text</s:span><s:a href="http://www.flashguy.de">the link</s:a><s:span>more Text</s:span></s:p></s:content>
  3. </s:RichEditableText>

Posted in Actionscript 3.0, Flashbuilder, Flex | No Comments »

New Job online – www.muellermilch.de

December 16th, 2009 by Gabor Wraight

Muellermilch.de

Together with BergerBaaderHermes I've created the Flash Components for the new Müllermilch Website.
The Site uses a lot of video and cool ajax animations. All in all I mus say it's a great example of the combination of Flash and dynamic HTML.

Posted in Work | No Comments »

Transparent Air application with Flashbuilder

June 28th, 2009 by Gabor Wraight

Just a quick one that annoyed me a bit. After my Vacation I came back and was so happy that the Flashbuilder beta was released. It was really necessary coz I wasn't happy working with Flexbuilder 3 after seeing the Gumbo preview at Max.

So, I started a new AIR Project in Flashbuilder and got stuck imediatly. I wanted a transparent application and this was really easy in Flexbuilder 3.As we now have the Spark framework things have changed a bit. To get an application with no crome and transparency you have to do the following:

  • Set system Chrome to none in the application descriptor file
  • Set transparent to true in the application descriptor file
  • Copy the file ApplicationSkin.mxml from the flex4 library to your project in a folder skins and name it ApplicationAlphaSkin.mxml
  • add the state: <s:State name="normalInactive" /> as first state in ApplicationAlphaSkin.mxml
  • set alpa="0" on the solidcolor of the backgroundRect
  • set the new skin in you application tag: skinClass="skins.ApplicationAlphaSkin"

Hope this helps someone.

Posted in Flashbuilder | 9 Comments »

Long Time no Blogging

June 28th, 2009 by Gabor Wraight

Hey you guys out there.
I'm sorry for not blogging for ages, but i was really busy with some awesome projects and and a long vacation in the US. I'll try to get all the stuff posted here in the next few days!

Posted in General | No Comments »

« Previous Entries