Josh Sager Media: Creative Technologies Blog

Josh Sager Media: Creative Technologies Blog
Creative Technologies Blog
Home | Portfolio |Blog | Presentations | music | About

Archive for March, 2009

Flash Cart: Macgyver Style

Tuesday, March 31st, 2009

Have you ever been on a code mission? You know one of those stubborn solution quest where nothing else matters.  Time passes around you in slow mo like Zack Braff in Garden Sate.  And the one thing that keeps you going is you know in your heart that there’s an answer and that you refuse to let roadblocks, errors, and ignorance stop you from getting something accomplished.

I Don’t know about your but for me they always sneak up out of nowhere.  Like at 4:59pm on a Friday somebody asks you for something that you think is a piece of cake, then Sunday rolls around and you’re still plugging away only to blink you eyes and realize you just missed Family Guy.

Well I just got back from one of these pilgrimages. Afterwards I typically come out of them feeling triumphant, disappointed with the amount of effort I just gave, and yet little more wise.  Usually because I missed something small in the beginning and along the way I discover a neat technique that changes my outlook on development in some way.

Every now and again I get on these little missions. Code journeys where I’m determined to find a way to make something work regardless of what other options might exist. Sometimes these missions are created by my environment and sometimes they are created by me.

This particular code mission was basically the development of a faux shopping cart system.  Just a demo to add and remove products.  Pretty easy right?  Well that’s what I thought. To add a little more fun to the situation I wanted to avoid used components.  I know I know I know it would have been easier, but this is a proof of concept for a much larger piece and compoents were not an option.  If your curious why DM me sometime and I’ll give you the low down.

Anyway it’s not perfect but I’ve got the proof of concept working

  • Multidimensional arrays
  • MovieClips as my receipt placeholders
  • Methods to add, remove, display the cart as well as the total

As the infinitely wise Colin Mook says if it works it’s not wrong.

So have it folks

flashcart.fla
flashcart.swf

  1. // array creation
  2. var productsArray:Array = new Array()
  3. var cartArray:Array = new Array()
  4. var cartClipArray:Array = new Array()
  5. // total
  6. var total:Number = 0
  7. // products
  8. productsArray[0] = {price:3, name:"burger", qty:0}
  9. productsArray[1] = {price:5, name:"drink", qty:0}
  10. productsArray[2] = {price:2, name:"fries", qty:0}
  11. productsArray[3] = {price:10, name:"shake", qty:0}
  12. productsArray[4] = {price:1, name:"cookie", qty:0}
  13. productsArray[5] = {price:2, name:"coffee", qty:0}
  14. // cartClips — These are the movieclips on the stage that reprecent my items added
  15. // to the cart
  16. cartClipArray[0] = cartClip0
  17. cartClipArray[1] = cartClip1
  18. cartClipArray[2] = cartClip2
  19. cartClipArray[3] = cartClip3
  20. cartClipArray[4] = cartClip4
  21. cartClipArray[5] = cartClip5
  22. cartClipArray[6] = cartClip6
  23. cartClipArray[7] = cartClip7
  24. cartClipArray[8] = cartClip8
  25. // clearcart
  26. // a for loop wouldn’t work here — go figure :P
  27. cartClipArray[0].visible=false
  28. cartClipArray[1].visible=false
  29. cartClipArray[2].visible=false
  30. cartClipArray[3].visible=false
  31. cartClipArray[4].visible=false
  32. cartClipArray[5].visible=false
  33. cartClipArray[6].visible=false
  34. cartClipArray[7].visible=false
  35. cartClipArray[8].visible=false
  36.  
  37. // A D D  T O  T H E  C A R T
  38. function addToCart(id:Number){
  39.         // checks if nothing is in the array
  40.         if(cartArray.length<1){
  41.                 cartArray.push(productsArray[id])
  42.                 cartArray[i].qty++
  43.         }else{
  44.                 // check for similar products
  45.                 for(var i:int=0;i0){
  46.                 cartArray[id].qty–
  47.                 if(cartArray[id].qty==0){
  48.                         showTotal()
  49.                         cartArray.splice(id,1)
  50.                         showCart()
  51.                 }
  52.                 showCart()
  53.         }
  54. }
  55.  
  56. // B U T T O N  S T U F F
  57. add_btn.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
  58. add_btn2.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
  59. add_btn3.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
  60. add_btn4.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
  61. add_btn5.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
  62.  
  63. cartClip0.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  64. cartClip1.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  65. cartClip2.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  66. cartClip3.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  67. cartClip4.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  68. cartClip5.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  69. cartClip6.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  70. cartClip7.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  71. cartClip8.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
  72.  
  73. function onAdd(e:MouseEvent):void{
  74.  
  75.         switch(e.target.name){
  76.                 case "add_btn":
  77.                 addToCart(0)
  78.                 trace("button")
  79.                 break
  80.  
  81.                 case "add_btn2":
  82.                 addToCart(1)
  83.                 break
  84.  
  85.                 case "add_btn3":
  86.                 addToCart(2)
  87.                 break
  88.  
  89.                 case "add_btn4":
  90.                 addToCart(3)
  91.                 break
  92.  
  93.                 case "add_btn5":
  94.                 addToCart(4)
  95.                 break
  96.         }
  97.  
  98.         showTotal()
  99.         showCart()
  100. }
  101.  
  102. function onRemove(e:MouseEvent):void{
  103.  
  104.         switch(e.target.parent.name){
  105.                 case "cartClip0":
  106.                 trace("remove fired")
  107.                 removeFromCart(0)
  108.                 break
  109.  
  110.                 case "cartClip1":
  111.                 removeFromCart(1)
  112.                 break
  113.  
  114.                 case "cartClip2":
  115.                 removeFromCart(2)
  116.                 break
  117.  
  118.                 case "cartClip3":
  119.                 removeFromCart(3)
  120.                 break
  121.  
  122.                 case "cartClip4":
  123.                 removeFromCart(4)
  124.                 break
  125.         }
  126.  
  127.         showTotal()
  128. }

Actionscript 3.0 Brush Up in the Books

Sunday, March 29th, 2009

Thanks for a great brush up guys and gals!

I’m very pleased at how well all of you did and I hope it was helpful and can lead you toward your next actionscripting goals. We covered a lot of material and did almost everything exclusively with code!

If you’re still hungry for more actionscript Pittsburgh has a great Flash Users Group that meets the 3rd Thursday of every month.

Keep flashing and drop me a link of your work every now and again.

Dragging Made Easy

Thursday, March 26th, 2009

Drag and Drop has popped its head into my work week about a half dozen times this week which inspired me to post some code of a basic object oriented drag system.  It’s not perfect and hitTestPoint would give me a more accurate collision detecation against the circle, but I thought it would be nice to see a simple example.

Here’s a quick rundown

  • Array and Loop for setting up the listeners
  • Always set the dragging item on top
  • Check for the correct detection (Here’s a hint it’s the third box)
  • React to the detection

Using the MouseEvent reference defined as e, you can avoid calling the movie clip or button by their original instance name.  This gives you, the developer, a more flexible framework to add and delete drag-able items at will.

Source Code
dragdemoquestion
dragdemoquestion

  1.  
  2. // D R A G A B L E  C L I P S
  3. var dragArray = new Array()
  4. dragArray[0] = box1
  5. dragArray[1] = box2
  6. dragArray[2] = box3
  7. dragArray[3] = box4
  8.  
  9. // L I S T E N E R S
  10. for(var i:int=0; i<dragArray.length; i++){
  11.         dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, onDragClip)
  12.         dragArray[i].addEventListener(MouseEvent.MOUSE_UP, onStopDragClip)
  13.         dragArray[i].addEventListener(MouseEvent.MOUSE_OVER, onOverDragClip)
  14.         dragArray[i].addEventListener(MouseEvent.MOUSE_OUT, onOutDragClip)
  15.         dragArray[i].buttonMode=true
  16. }
  17.  
  18. // H A N D L E R S
  19. function onDragClip(e:MouseEvent):void{
  20.         e.target.startDrag()
  21.         // Make sure what I’m dragging is always on top
  22.         setChildIndex(MovieClip(e.target), numChildren-1)
  23. }
  24. function onStopDragClip(e:MouseEvent):void{
  25.         // stop the drag please
  26.         e.target.stopDrag()
  27.         // hey do I know you…
  28.         if(e.target.hitTestObject(circle1)){
  29.                 if(e.target.name =="box3"){
  30.                         // … if so do some stuff
  31.                         txt_box.text="Drag Me - Winner"
  32.                         centerClip(MovieClip(e.target),circle1)
  33.                 }else{
  34.                         // … nevermind I thought you were someone else
  35.                         txt_box.text="Drag Me - Loser fail"
  36.                         removeChild(MovieClip(e.target))
  37.                 }
  38.         }
  39. }
  40. function onOverDragClip(e:MouseEvent):void{
  41.         // Rollovers… Aren’t they fun :)
  42.         e.target.filters = [new GlowFilter(0xFFFF00,.5, 20.0,20.0,2,1,false, false)]
  43. }
  44. function onOutDragClip(e:MouseEvent):void{
  45.         // Peace out on the Roll out
  46.         e.target.filters = null
  47. }
  48. // C U S T O M  M E T H O D S
  49. function centerClip(mc1:MovieClip, mc2:MovieClip):void{
  50.         mc1.x =mc2.x+((mc2.width/2) - (mc1.width/2))
  51.         mc1.y =mc2.y+ ((mc2.height/2) - (mc1.height/2))
  52. }
  53.  
  54.  

Swap Depths for AS 2.0

Saturday, March 21st, 2009

Every now and again I get asked questions about ActionScript 2.0.  Although I would much rather give that person a hug and tell them that AS 3.0 will set them free I know that not everyone can make the jump instantly.

Today I was asked how to swap depths in AS 2.0.  No problem right? Wrong! It seemed like everything I tried was jut not working. After I stepped away for a bit I was able to solve the problem but for the life of me I don’t know what was initially wrong. Nevertheless here is the answer.

  1.  
  2.    // sets swapDepths
  3.    this.swapDepths(this.getNextHighestDepth())
  4.  

This will place a movieClip at the top of the display order.
Demo Files
ActionScript 2.0 Swap Depths Demo - Project File
ActionScript 2.0 Swap Depths Demo

Flash + SWC + Flex at Flash Users Group

Tuesday, March 17th, 2009

This Thursday is our Monthly Adobe Flash Users Group meeting.  This month I’ll be presenting on the use of SWC files in rapid prototyping strategy.  It’s a great way to build out functionalty quickly while still allowing for the flexibily of last minute changes.

Ben Pritchard will demo the FLARToolkit as seen on the GE site. Personally I’m really looking forward to this presentation.  Ben’s a great speaker and he’s the MacGyver of actionscript.

The meeting starts at 6:30 be there or be square.

Great Font Ideas I Wish I Had First

Sunday, March 15th, 2009

Every now and again I come across some ideas I wish I had first.  Some of them are really clever, some are just really simple. Mostly my appreciation comes from thinking DAMNIT I should have thought of that.  Nevertheless I wanted to pass along a few things that are fun, clever, and definitly good ideas.

There is a designer disclaimer here by the way.  If you don’t get it I apologize cause you’re not in the club.  If you do get it then congrats you should be receiving your cool designer live-strong bracelet and horn-rimmed glasses in the mail within the next few weeks.

My Reading List

Tuesday, March 10th, 2009

The other day it occurred to me that I haven’t been reading very much this year. I was geeking out about ActionScript and other Web Development this past weekend after an alumni brush up course in which we exchanged titles we read, things that have influenced us, and titles that are on our respective “to read” lists.

It was then I realized most of my recently read items were from the tail end of last year. One of the alums asked me to send him a list in which I went really overboard, but it got me thinking about all of the good material in these books and that if I was a budding Web Designer or Developer what titles would help me take my game to the next level.

So here was my list off the top of my head in no particular order. This is a compilation of things I’ve read, I am reading,  things I know I should have read, and things I can’t wait to read.

Feel free to add to the list

Things I’ve read and or currently reading

Pittsburgh Web Design Day

Monday, March 9th, 2009

Where: Left Field
When: Saturday April 4th 8:30am - 4:30pm
Cost: $40
Limited Registration

There’s a great one day Web Conference going on in just a few weeks called Pittsburgh Web Design Day. The name says it all.  One day of really cool web stuff in Pittsburgh, kind of like a geek holiday. It’s brought to you by the great people at Refresh Pittsburgh, so you knows it’s going to awesome.

I’m especially psyched because I’ve been invited to talk about my processes.  My session is called ”The Story of a Web Project: Process and Best Practices” and basically it’s best practices of identifing good clients/projects, how to cost out a project, avoiding some commong pittfalls, and how to finish a project.

The speaker line up is jammed packed with fantastic people like Val Head, Samantha Warren, Geoff Barnes, and Chris Cashdollar. Spots are going fast so make sure to register early.

ActionScript 3.0 Brush Up - April 09

Saturday, March 7th, 2009

It’s that time of year again. Spring is in the air, the clocks are going to change again, and the ActionScript 3.0 Brush Up course is back again.

To make things a little easier this year I’ll be posting the slides to what we will be covering. Just download the zip file and you can reference the slides. I’m giving you permission to use this for educational purposes and not for distribution.

Here are the slides and other Resources

Week 1

ActinScript 3.0 Diagram
Week 1 - Slides

Week 2

Week 2 - Slides
Week 2 Examples

Week 3

Week 3 - Slides
Week 3 - Examples

Week 4

Week 4
Week 4 - Sample Files

##### Thanks Guys and Gals I had a great time I hope to hear from you in the future. I always learn a lot from the grads like little tips and tricks that you’ve discovered along the way.  Keep in touch!

Grant Skinners Tweetcode Content

Tuesday, March 3rd, 2009

Grant Skinner, for those of you that don’t know, is a god in Flash community and has once again blown my mind with an idea that I feel like I should have come up with.  He’s got a tweet code contest running with prizes such as Flash CS4, and Friends of Ed books.

The rules are simple.

  1. 140 characters or less
  2. Use only the gimmie code
  3. No use of outside code
  4. Be super creative
  5. Post to your twitter account

Here’s my contribution in al of it’s 67 character glory.  I call it the color snake.

xp=mouseX;yp=mouseY;ls(i,i*1e5,1);if(i==0)mt(xp,yp);lt(xp,yp);i++;

There are lots of great submissions. You could be one of them.  Check it out!