Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

printing array

Posted on 2009-07-15
8
193 Views
Last Modified: 2013-11-11
I am working on a game project. I am adding items to a shopping cart when the player goes to frame 6 i want it to print the items in the cart.
Currently i am using a switch in the main of frame 6. This is long tiresome code.
What i want it to do is work through a loop, if the item code matches that array i want it to print the description, item code, and price on the screen

Here is the code that i have so far......any suggestions

stop();
//displays total cost
total_txt.text = "$" + totalCost;
//creates a text field for the items in the shopping cart
_root.createTextField("cartText", _root.getNextHighestDepth(), 100, 100, 500, 500);
//defines variables for the cart text format and an item in the shopping cart
var cartTextFormat = new TextFormat();
var item:String = "";
//specifies the format of the cartText field
cartTextFormat.multiline = true;
cartTextFormat.color = 0xffffff;
cartTextFormat.font = "Segoe Script";
//displays the header in the cartText field
cartText.text = "Items In Cart\t\t\t\t\t\t\tPrices\n";
 
//loops through the cart to display the items and costs of the items in the cartText field
for(var i = 0; i < cart.length; i++){
	item = cart[i];
	switch(item){
		case "blueshirt":
			cartText.text += "Long Sleeved Blue Blouse\t\t\t\t$" + itemPrices[i] + "\n";
			break;
		case "redblackshirt":
			cartText.text += "Red & Black Ripped Shirt\t\t\t\t$" + itemPrices[i] + "\n";
			break;
		case "shoe2":
			cartText.text += "Pink Sandals\t\t\t\t\t\t\t$" + itemPrices[i] + "\n";
			break;
	}
}
//sets the format to the cartText field
cartText.setTextFormat(cartTextFormat);

Open in new window

0
Comment
Question by:jrb47
  • 3
  • 3
8 Comments
 
LVL 20

Expert Comment

by:a_b
ID: 24860938
If you create a class called item and store those in an cart array you can override the toString method in the class to print the details of Item.


for(var i = 0; i < cart.length; i++)
{
     item = cart[i];
     sysout(item.toString());
}

I am not sure why you need to compare using the switch case.
0
 

Author Comment

by:jrb47
ID: 24861248
I am new to actionscript.....BUT I did not want to compare....how would i create the class you are mentioning.
I tried something similar but it would do nothing.


Thank you so much for the help
0
 
LVL 14

Expert Comment

by:TanLiHao
ID: 24878251
Like a_b said, you shouldn't be even using a switch statement to do this.

I personally think you should make cart an object, with properties itemNames and itemPrices in it.

However, your code seems to be working, so I don't get your problem.

You will need to give more details.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:jrb47
ID: 24878280
I would like to be able to do this without the switch...
Ideally what i would like to do is be able to read through the cart, print the array, give option to remove from cart, (have it leave the screen) and adjust the price of the cart.
I have it working with putting a cart.text command on each item.

You would be interested in looking at the fla file. I think you would see more of what i mean.

0
 
LVL 14

Expert Comment

by:TanLiHao
ID: 24878651
I don't see the .fla file.

Actually this is what I think you should do.

Let cart be the array of items.
cart.ItemName be an array of items'  names, such asLong Sleeved Blue Blouse
cart.ItemPrice be an array of items' prices.

Then your code can get rid of the swtich statement. See code snippet.

Then of course for the other two parts you should be more clear on what you want.

If you would attach a .fla file with comments specifically on what you want to do, then it would be easy.

for(var i = 0; i < cart.length; i++) {
        item = cart[i];
        cartText.text += cart.itemName + "\t\t\t\t$" + cart.ItemPrice[i] + "\n";
}

Open in new window

0
 

Author Comment

by:jrb47
ID: 24878729
you cant attach an fla file on this forum
I would have to send it directly to you
I did try that piece of code you listed but it did not work......not sure why

0
 
LVL 14

Accepted Solution

by:
TanLiHao earned 500 total points
ID: 24878771
It would work but there is a slight mistake with my code anyway.

It's cart.ItemName[i] not cart.ItemName.

Anyway to attach the .fla file, rename it to .txt extension and upload it here. Otherwise you can use upload elsewhere.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I convert a CS4 FLA file to CS6? 1 877
Action Script commands 3 220
How to emebed .XML into  flash to make a self contained .SWF 1 186
Firefox fullscreen video lags 4 170
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to how to record live broadcast.

809 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