Flash Cart: Macgyver Style
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
-
// array creation
-
// total
-
var total:Number = 0
-
// products
-
productsArray[0] = {price:3, name:"burger", qty:0}
-
productsArray[1] = {price:5, name:"drink", qty:0}
-
productsArray[2] = {price:2, name:"fries", qty:0}
-
productsArray[3] = {price:10, name:"shake", qty:0}
-
productsArray[4] = {price:1, name:"cookie", qty:0}
-
productsArray[5] = {price:2, name:"coffee", qty:0}
-
// cartClips — These are the movieclips on the stage that reprecent my items added
-
// to the cart
-
cartClipArray[0] = cartClip0
-
cartClipArray[1] = cartClip1
-
cartClipArray[2] = cartClip2
-
cartClipArray[3] = cartClip3
-
cartClipArray[4] = cartClip4
-
cartClipArray[5] = cartClip5
-
cartClipArray[6] = cartClip6
-
cartClipArray[7] = cartClip7
-
cartClipArray[8] = cartClip8
-
// clearcart
-
// a for loop wouldn’t work here — go figure
-
cartClipArray[0].visible=false
-
cartClipArray[1].visible=false
-
cartClipArray[2].visible=false
-
cartClipArray[3].visible=false
-
cartClipArray[4].visible=false
-
cartClipArray[5].visible=false
-
cartClipArray[6].visible=false
-
cartClipArray[7].visible=false
-
cartClipArray[8].visible=false
-
-
// A D D T O T H E C A R T
-
function addToCart(id:Number){
-
// checks if nothing is in the array
-
if(cartArray.length<1){
-
cartArray.push(productsArray[id])
-
cartArray[i].qty++
-
}else{
-
// check for similar products
-
for(var i:int=0;i0){
-
cartArray[id].qty–
-
if(cartArray[id].qty==0){
-
showTotal()
-
cartArray.splice(id,1)
-
showCart()
-
}
-
showCart()
-
}
-
}
-
-
// B U T T O N S T U F F
-
add_btn.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
-
add_btn2.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
-
add_btn3.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
-
add_btn4.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
-
add_btn5.addEventListener(MouseEvent.MOUSE_DOWN, onAdd)
-
-
cartClip0.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
cartClip1.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
cartClip2.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
cartClip3.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
cartClip4.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
cartClip5.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
cartClip6.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
cartClip7.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
cartClip8.addEventListener(MouseEvent.MOUSE_DOWN, onRemove)
-
-
function onAdd(e:MouseEvent):void{
-
-
switch(e.target.name){
-
case "add_btn":
-
addToCart(0)
-
trace("button")
-
break
-
-
case "add_btn2":
-
addToCart(1)
-
break
-
-
case "add_btn3":
-
addToCart(2)
-
break
-
-
case "add_btn4":
-
addToCart(3)
-
break
-
-
case "add_btn5":
-
addToCart(4)
-
break
-
}
-
-
showTotal()
-
showCart()
-
}
-
-
function onRemove(e:MouseEvent):void{
-
-
switch(e.target.parent.name){
-
case "cartClip0":
-
trace("remove fired")
-
removeFromCart(0)
-
break
-
-
case "cartClip1":
-
removeFromCart(1)
-
break
-
-
case "cartClip2":
-
removeFromCart(2)
-
break
-
-
case "cartClip3":
-
removeFromCart(3)
-
break
-
-
case "cartClip4":
-
removeFromCart(4)
-
break
-
}
-
-
showTotal()
-
}
Tags: ActionScript 3.0, Code Mission, Flash Cart