|
-
Basic array help
Hello, I'm just getting to grips with AS3 and have never studied arrays before.
I'm trying to get duplicate movieclips to appear underneath each other every time a button is pressed. The movieclip is called "cartslot". Here's what I have so far:
Code:
var checkoutSlotsArray:Array = new Array();
var slot:cartslot = new cartslot();
tShirtBuy.addEventListener(MouseEvent.CLICK, sendShirtToCheckout);
function sendShirtToCheckout (evt:MouseEvent):void {
// If this is the first movieclip being generated
if (checkoutSlotsArray.length == 0) {
// Create a cartslot movieclip as a child
shoppingCart.addChild(slot);
checkoutSlotsArray.push(slot);
// Set the first movieclips position
slot.x = 0;
slot.y = 0;
} else {
// Create another cartslot movieclip as a child
shoppingCart.addChild(slot);
checkoutSlotsArray.push(slot);
// Calculate the bottom of the duplicated clips
var slotPos:Number = ((checkoutSlotsArray.length + 1) * shoppingCart.slot.height);
// Set the duplicate movieclips position
slot.x = 0;
slot.y = slotPos;
}
}
The first movieclip appears fine, however the second click gives me the error:
TypeError: Error #1010: A term is undefined and has no properties.
at indoorshredwebsite_fla::shopmc_17/sendShirtToCheckout()
Can anyone give me any pointers? As mentioned, I'm really new to arrays so I'll need very specific instructions if possible. Thanks!
-
You have a couple of problems. First, you are not duplicating anything. You only created one slot, you are just re-using it many times. Since a single DisplayObject can only be on the displayList once, you will not see more than one slot. But the same object can be in an array many times, so you are adding it to the checkoutSlotsArray more than once. The SAME object would be in there more than once.
The immediate cause of your error has nothing to do with duplication or arrays. You are referring to shoppingCart.slot, but there is no such property. Adding a child does not create a property.
You also do not need to have different cases for the first and subsequent calls. A single case will handle it.
Code:
var checkoutSlotsArray:Array = new Array();
tShirtBuy.addEventListener(MouseEvent.CLICK, sendShirtToCheckout);
function sendShirtToCheckout (evt:MouseEvent):void {
//Create new cartslot.
var slot:cartslot = new cartslot();
//add it to the cart
shoppingCart.addChild(slot);
var slotPos:Number = ((checkoutSlotsArray.length) * slot.height);
checkoutSlotsArray.push(slot);
// Set the slot's position
slot.x = 0;
slot.y = slotPos;
}
The first time through, the array length is 0, so the y is set to 0. Subsequent times through, it'll be set to the number of slots (before adding the new slot) * the slot height. You could also just set the y pos to the shoppingCart height if it is expanding with the increased slots. Or you could set it to the previous slot's y + height.
-
Brilliant, thank you, it works great.
There's another hurdle I've come across. Inside this duplicated clip is a "close" button that makes the respective clip disappear. I've named the button "closeSlot".
Code:
checkoutSlotsArray[0].closeSlot.addEventListener(MouseEvent.CLICK, clearSlot);
function clearSlot (evt:MouseEvent):void {
trace("worked");
}
However of course, Flash doesn't like what I've done and has given me:
TypeError: Error #1010: A term is undefined and has no properties.
at indoorshredwebsite_fla::shopmc_17/indoorshredwebsite_fla::frame1()
I'm guessing it's because I'm trying to add a listener to a clip that doesn't exist yet. How should I really be doing this instead?
-
I've found this code which should work but for some reason the "close" buttons don't even act as a button, they do nothing when clicked. Can anyone see why?
Code:
for(var i:int = 0; i < checkoutSlotsArray.length; i++){
checkoutSlotsArray[i].closeSlot.buttonMode = true;
checkoutSlotsArray[i].closeSlot.addEventListener(MouseEvent.CLICK, clearSlot);
}
function clearSlot (evt:MouseEvent):void {
trace("worked");
}
I ran a trace on checkoutSlotsArray.slotClose and it exists, so I can't see why it won't function as a button.
-
slotClose is not closeSlot. Nor is a property on the array a property of the elements of the array.
The things in checkoutSlotsArray are cartslots. Does a cartslot have a closeSlot property?
-
I figured it out. The "for" statement needed to be in the same function that created the child (sendShirtToCheckout). I had it elsewhere. Problem solved!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|