?
Solved

Help with checkboxes and arrays in actionscript

Posted on 2010-01-08
12
Medium Priority
?
789 Views
Last Modified: 2013-11-11
Hello experts,

Im stumped.... I am fairly new to action script and need to create a flash form that has 3 columns with a few check boxes in each. Each checkbox contains a prod name and a price.

I need to create a submit function that will add up the selected checboxes and give me a total. As well as a little if then conditional validation to make sure they select the allowed combo of products.

I could probably do this in 10 mins in coldfusion but this has to be completly done in flash with no external file links etc....

I have no problem going through tutorials but i am yet to find one that covers the data array to contain products, the checkbox form and submit function to validate and sum the total.

Thanks for your time

 
0
Comment
Question by:joedfuse
  • 6
  • 6
12 Comments
 
LVL 37

Expert Comment

by:CyanBlue
ID: 26213274
I don't think you will find the specific tutorial that does all you need, but this one covers general idea on how to handle the checkbox on the form where you can add in your if logic with multiple checkboxes...
   http://www.flashscript.biz/php/checkbox/checkbox.html

CyanBlue
0
 

Author Comment

by:joedfuse
ID: 26213352
Thanks for the response. The least of my probs was the check box but more along the lines of dynamically create the check box from product array then add those values up on a submit button without leaving the flash file.

Thanks
0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 26213403
I don't know what version of Flash/ActionScript you are using, but the Flash manual has sample syntax you can borrow from... ;)

I don't quite understand what you meant by 'product array' and 'without leaving the flash file' part...

CyanBlue
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:joedfuse
ID: 26213764
ok heres some javascript to try to explain the babbling nonsense i have going through my head when trying to explain what im looking for. Im using Flash cs4.

In the code snip i have some products and prices loaded. What im looking for is the actionscript version of this array, dynamically create checkboxes based on this viewing only products not prices. Then after a submission button clicked it adds the values up and if the cost under 20 dollars it yells CORRECT other wise it lets people know they fail.

And when i refer to not leave the flash i mean i cant use database, php page, xml page etc... everything needs to be done within flash. If i could use other tech believe me this would all get done in CF.

Im gonna increase the point value of this question to take into account the time actually getting to the question lol
var prodNameCol1 = new Array(); //product name col 1
prodNameCol1[0] = "Prod 1";
prodNameCol1[1] = "Prod 2";
prodNamecol1[2] = "Prod 3";

var priceCol1 = new Array(); //prices of products col 1
priceCol1[0] = 2.00;
priceCol1[1] = 2.00;
priceCol1[2] = 2.95;

var prodNameCol2 = new Array(); //product name col 2
prodNameCol2[0] = "Prod 4";
prodNameCol2[1] = "Prod 5";
prodNamecol2[2] = "Prod 6";

var priceCol1 = new Array(); //prices of products col 2
priceCol2[0] = 2.00;
priceCol2[1] = 2.00;
priceCol2[2] = 2.95;

Open in new window

0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 26214033
I do understand the question except the submit part of it...  So, there is no actual call to an external script to post it to database or something, but rather you simply want to display the total of it, I guess...  Am I correct on that???

Why don't you take a stab at it and see where get???  It seems fairly straight forward to me...  Honestly, I don't plan to do that for you, but I can help you get through it if you get stuck while you try...  

CyanBlue
0
 

Author Comment

by:joedfuse
ID: 26272886
Thats the problem i dont know where to start looking Im new to action script an am looking for tutorials that cover what im trying to do. Im not asking for anyone to write the code for me just point me in a direction to write the array that im looking for and a tutorial to get the SUM as well as apply some conditional statements.

There can be multiple tutorials that tie this together. And believe me im looking as well as i post this.

I wrote an example again below in CF this works fine, just dont know how to translate to actionscript.
<!--- Build a new query with name and cost. --->
<cfset qProducts = QueryNew( "name, cost" ) />
 
<!--- Add some rows to the query. --->
<cfset QueryAddRow( qProducts ) />
<cfset qProducts[ "name" ][ 1 ] = "Prod1" />
<cfset qProducts[ "cost" ][ 1 ] = "2.00" />
 
<cfset QueryAddRow( qProducts ) />
<cfset qProducts[ "name" ][ 2 ] = "Prod2" />
<cfset qProducts[ "cost" ][ 2 ] = "2.00" />
 
<cfset QueryAddRow( qProducts ) />
<cfset qProducts[ "name" ][ 3 ] = "Prod3" />
<cfset qProducts[ "cost" ][ 3 ] = "2.00" />

<!--- Get the sum of costs. --->
<cfset flcostSum = ArraySum( qProducts["cost"] ) />

<cfoutput>#flcostSum#</cfoutput>

Open in new window

0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 26285574
This is the easiest code sample I can think of which should work well with AS3...

Create a new FLA file...  Drag a Checkbox component from the Components panel onto the stage...  Select that instance from the stage and delete it...
Paste this code into the frame 1 of your movie and test it...

CyanBlue
import fl.controls.CheckBox;

var products:Array = new Array();
products.push({name:"Prod 1", cost:"2.00"});
products.push({name:"Prod 2", cost:"2.00"});
products.push({name:"Prod 3", cost:"2.95"});
products.push({name:"Prod 4", cost:"2.00"});
products.push({name:"Prod 5", cost:"2.00"});
products.push({name:"Prod 6", cost:"2.95"});

setupCheckBoxes();

function setupCheckBoxes()
{
	for (var i:Number = 0; i < products.length; i++)
	{
		this["cb" + i] = new CheckBox();
		this["cb" + i].label = products[i].name + " : " + products[i].cost;
		this["cb" + i].name = "prod" + i;
		this["cb" + i].addEventListener(MouseEvent.CLICK, updateCost);
		addChild(this["cb" + i]);
		this["cb" + i].y = i * 20;
	}
}

function updateCost(e:MouseEvent):void
{
	var cb:CheckBox;
	var idx:Number = 0;
	var sum:Number = 0;
	for (var i:Number = 0; i < products.length; i++)
	{
		cb = getChildByName("prod" + i) as CheckBox;
		if (cb.selected == true)
		{
			sum += parseFloat(products[i].cost);
		}
	}
	trace(sum);
}

Open in new window

0
 

Author Comment

by:joedfuse
ID: 26286008
thanks for the response... there where about 8 errors in the code just put in so i made some changes to the code and now only getting one nagging error

1086: Syntax error: expecting semicolon before dot.

Its happening in Line 1.

I'm assuming it has something to do with cost but not sure.
var products.push(name:"Prod 6", cost:"2.95");

setupCheckBoxes();

function setupCheckBoxes()
{
        for (var i:Number = 0; i < products.length; i++)
        {
                this["cb" + i] = new CheckBox();
                this["cb" + i].label = products[i].name + " : " + products[i].cost;
                this["cb" + i].name = "prod" + i;
                this["cb" + i].addEventListener(MouseEvent.CLICK, updateCost);
                addChild(this["cb" + i]);
                this["cb" + i].y = i * 20;
        }
}

function updateCost(e:MouseEvent):void
{
        var cb:CheckBox;
        var idx:Number = 0;
        var sum:Number = 0;
        for (var i:Number = 0; i < products.length; i++)
        {
                cb = getChildByName("prod" + i) as CheckBox;
                if (cb.selected == true)
                {
                        sum += parseFloat(products[i].cost);
                }
        }
        trace(sum);
}

Open in new window

0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 26286515
You cannot push anything unless you declare an array first...  In other words, you are missing this line...

   var products:Array = new Array();

CyanBlue
0
 

Author Comment

by:joedfuse
ID: 26286939
you rock!

now i just want to add comments to your code to see if i am properly understanding what you did as im trying to learn this and not just throw some code in an app and call it a day.

as you can see from my code i added comments seeing if i can explain what you did. Now lets say i wanted a button to do the sum in another frame and dynamic txt box.. i would just take listener out of the setupcheckbox function correct? and that buttons function would be where i would do validation?

thanks for all you help i will be accepting this solution
//get the checkbox control from lib
import fl.controls.CheckBox;

// create the array and populate itwith a prod name and cost
var products:Array = new Array();
products.push({name:"Prod 1", cost:"2.00"});
products.push({name:"Prod 2", cost:"2.00"});
products.push({name:"Prod 3", cost:"2.95"});
products.push({name:"Prod 4", cost:"2.00"});
products.push({name:"Prod 5", cost:"2.00"});
products.push({name:"Prod 6", cost:"2.95"});

//build checkboxes onscreen at y coord from top of screen
setupCheckBoxes();

function setupCheckBoxes()
{
        //LOOP through array to build checkboxes
	for (var i:Number = 0; i < products.length; i++)
        {
                this["cb" + i] = new CheckBox();// call a new instance of checkbox
                this["cb" + i].label = products[i].name;//write the labels to screen
                this["cb" + i].name = "prod" + i;//assign product name to label
                this["cb" + i].addEventListener(MouseEvent.CLICK, updateCost);
                addChild(this["cb" + i]); //tell flash to update everytime mouse clicks checkbox
                this["cb" + i].y = i * 20; //checkbox position
}

//function that runs in the loop when mouse event happens
function updateCost(e:MouseEvent):void
{
        var cb:CheckBox;//set cb as variable of checkbox component
        var idx:Number = 0;//clear the index for array
        var sum:Number = 0;//clear variable sum
        for (var i:Number = 0; i < products.length; i++) //loop through array
        {
                cb = getChildByName("prod" + i) as CheckBox;//get the products from the looped array
                if (cb.selected == true)
                {
                        sum += parseFloat(products[i].cost);//if the checkbox is true add the cost to the sum
                }
        }
        trace(sum);//test app
}

Open in new window

0
 
LVL 37

Accepted Solution

by:
CyanBlue earned 1000 total points
ID: 26287537
Yeah, your comment is good...  ;)  You might want to remove idx variable in updateCost method though...  I don't use that...  I just forgot to remove that line...

One thing is that you should be in the same frame when/if you want to access those checkbox components...  When you go to the next frame, those checkboxes goes away, so you don't have values...  You could save the value of the sum variable into a variable that persists through the movie and use it that way...

CyanBlue
0
 

Author Comment

by:joedfuse
ID: 26287562
I have some more positioning questions but i will be posting another thread as i need to gather my thoughts
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question