11-10-2009, 06:53 PM
|
#1
|
|
Member
Join Date: Oct 2009
Location: New York, NY
Posts: 64
|
Get an Objects name....
Good day,
I'm not sure how to trace the elements in my code since my variables are populated dynamically. I have tried to use trace(cartOrder[0]); but when I do I just get the output [object movieclip]. I want it to give me the name of the handbag. My code is
var cartOrder:Array = [];
Code:
var cartOrder:Array = [];
handbag1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
handbag1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
handbag2_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
handbag2_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
handbag3_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
handbag3_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
handbag4_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
handbag4_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
function pickUp(evt:MouseEvent):void {
var object = evt.currentTarget;
object.startDrag();
}
function dropIt(evt:MouseEvent):void {
var obj = evt.currentTarget;
obj.stopDrag();
if (obj.hitTestObject(cart))
{
reply_txt.text = "Made it !!";
cartOrder.push(obj);
trace(cartOrder);
}
else
{
reply_txt.text = "Missed :(";
}
}
|
|
|
11-10-2009, 07:06 PM
|
#2
|
|
Member
Join Date: Oct 2009
Location: New York, NY
Posts: 64
|
Should DisplayObject be used instead?
|
|
|
11-10-2009, 07:18 PM
|
#3
|
|
six eyes
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
|
trace(cartOrder[0].name);
|
|
|
11-10-2009, 07:38 PM
|
#5
|
|
Member
Join Date: Oct 2009
Location: New York, NY
Posts: 64
|
How do I change it so that it returns whatever value I put into the cart? Right now I get whatever is in position 0.
I tried trace cartOrder.name & cartOrder[].name but these don't give me the right results.
Thanks
|
|
|
11-10-2009, 08:12 PM
|
#6
|
|
six eyes
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
|
Your cart is an array - it can contain multiple objects. You have to decide which objcet you want and use the correct index. 0 will give you the first. cartOrder.length - 1 will give you the last
|
|
|
11-10-2009, 08:19 PM
|
#7
|
|
Member
Join Date: Oct 2009
Location: New York, NY
Posts: 64
|
There is no way to get a listing from the array of whats in the cart?
What I ultimately want to do is send the 4 variables from the array to javascript. The trick is I need to know what order each item is going into the array. Which one is first, second, and so on. This way I can get a user's preference.
I am trying to use trace to see if I am on the right path track of getting this done. I figure if I can get the trace to display the contents of my array then I can dump the array values into variables, then send the individual variables into javascript.
|
|
|
11-11-2009, 08:50 PM
|
#8
|
|
Member
Join Date: Oct 2009
Location: New York, NY
Posts: 64
|
Viewing objects in a dynamic array
I finally figured out how to trace my objects in an array.
WARNING - Its not pretty but it works.
For anyone looking at this, I did change the format of the code.
Code:
var cartOrder:Array = [];
var counter:Number=0;
createObjListeners(); //begin
function createObjListeners() : void {
var loop:Number = 1;
do{
this["handbag" + loop + "_mc"].addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
this["handbag" + loop + "_mc"].addEventListener(MouseEvent.MOUSE_UP, dropIt);
}while(++loop <= basketCnt);
}
function pickUp(evt:MouseEvent):void {
var object = evt.currentTarget;
object.startDrag();
}
function dropIt(evt:MouseEvent):void {
var obj = evt.currentTarget;
obj.stopDrag();
if (obj.hitTestObject(cart))
{
reply_txt.text = "Made it !!";
cartOrder.push(obj);
counter++;
trace(counter);
}
else
{
reply_txt.text = "Missed :(";
}
// Here starts my error checking
if (counter == 1)
{
trace (cartOrder[0].name);
}
if (counter == 2)
{
trace (cartOrder[1].name);
}
if (counter == 3)
{
trace (cartOrder[2].name);
}
if (counter == 4)
{
trace (cartOrder[3].name);
}
}
|
|
|
11-11-2009, 08:56 PM
|
#9
|
|
Member
Join Date: Oct 2009
Location: New York, NY
Posts: 64
|
Pulling variables from an array
Question...
My dynamic array has objects in it that are "undefined". However when I do a trace on the array (cartOrder[1].name, cartOrder[2].name, etc.) I can see the name of the object. Can I take an object out of the array and assign it as a new variable? Is it possible?
|
|
|
11-11-2009, 09:07 PM
|
#10
|
|
Member
Join Date: Oct 2009
Location: New York, NY
Posts: 64
|
Got it.... another un-elegant solution.. I can take the name of the obj in the cart and assign to to a new variable. Seems weird to me, especially since the object is not defined. Anyway here it goes.
var newvar1 = cartOrder[0].name;
If you want to check it use:
trace("newvar1 = " + newvar1);
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 09:18 AM.
///
|
|