Been beginning to learn AS3 (no prior programming experience). I'm in the process of trying to make a simple array and I'm having trouble.
I have four objects, and I just want to do something simple, like scale an object on a mouse over (and shrink on a mouse out) to start.
Here's my starting code:
Code:
var myArray:Array = new Array([box1_mc, box2_mc, box3_mc, box4_mc]);
for each(var box:MovieClip in myArray) {
box.addEventListener(MouseEvent.MOUSE_OVER, boxOver):
box.addEventListener(MouseEvent.MOUSE_OUT, boxOut);
function boxOver(event) {
//scaling code
}
}
I'm getting this error on this third line of the array: 1078: Label must be a simple identifier.
function setupVars():void
{
var myArray:Array = new Array([box1_mc, box2_mc, box3_mc, box4_mc]);
for(var i:Number = 0; i < myArray.length; i++)
{
myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
}
}
function boxOver(ev:Event):void
{
// something
}
function boxOut(ev:Event):void
{
// something else
}
for each is valid way of working with arrays but since you have no experience, i'd say it would be better if you learned how to work with arrays this way. I think it gives you far more control.
ok things to consider
myArray.length == 4
BUT actually targeting array values are going to be:
myArray[0] == box1_mc
myArray[1] == box2_mc
myArray[2] == box3_mc
myArray[3] == box4_mc
myArray[4] == null/undefined
so that for loop starts 'i' at 0. as long as 'i' is less than myArray.length then add one to the 'i'
myArray[i] pulls out one instance of what is in there. So you end up cycling through the entire array and when i == 4 the loop stops.
whats nice about knowing this method is that if there is a reason you need to call one particular item from your array then you can use myArray[3] and single it out.
Finally when working with event listeners you can do them one of 2 ways. have a function out side the bounds of the function that is adding the listener:
Code:
function firstfunct()
{
something.addEventListener(someevent, eventFunct);
}
function eventFunct(e:Event)
{
// what you want to happen
}
or you can create the function in the event listener:
Code:
function firstfunct()
{
something.addEventListener(someevent, function(){ trace("cant comment here ^_^") });
}
Hmm. That looks easy enough. I must have read the chapter on arrays in Wiley's Bible like 4 times, and your post was infinitely simpler to understand than the book. lol.
Thanks a lot. I'll test it out and see what happens!
Well, I think I'm understanding your code, but it doesn't seem to be working, and I'm not sure why. Maybe I included something incorrectly. As you'll see, I'm trying to just fade the alpha of each, to do something easy to begin with:
Code:
function setupVars():void {
var myArray:Array = new Array([box1_mc, box2_mc, box3_mc, box4_mc]);
for(var i:Number = 0; i < myArray.length; i++) {
myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
myArray[i].alpha = 0
myArray[i].buttonMode = true;
}
}
var boxTween:Tween;
var box2Tween:Tween;
function boxOver(ev:Event):void {
boxTween = new Tween(myArray, "alpha", None.easeNone, 0, 1, .5, true);
}
function boxOut(ev:Event):void {
box2Tween = new Tween(myArray, "alpha", None.easeNone, 1, 0, .5, true);
}
I'm getting Access of Undefined Property errors for each value in the array, Access of Undefined Property i, and Access of Undefined Property myArray.
Ahh one other thing if forgot to mention is the difference between public and private variables.
so lets say you have something like this:
Code:
// usually nice to define all of these at the top = ]
var boxTween:Tween;
var box2Tween:Tween;
function setupVars():void {
var myArray:Array = new Array([box1_mc, box2_mc, box3_mc, box4_mc]);
for(var i:Number = 0; i < myArray.length; i++) {
myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
myArray[i].alpha = 0
myArray[i].buttonMode = true;
}
}
function boxOver(ev:Event):void {
// ev is a target of what got an event listener one property is currentTarget which is one specific instance of myArray
boxTween = new Tween(ev.currentTarget, "alpha", None.easeNone, 0, 1, .5, true);
}
function boxOut(ev:Event):void {
box2Tween = new Tween(ev.currentTarget, "alpha", None.easeNone, 1, 0, .5, true);
}
Okay, got it. So I should define the variables first thing. But if they're outside the function, they're global, right? So why does it matter if they're at the top? Just easy to store them first I suppose.
I'm still getting access of undefined property errors for the movie clips.
This is getting frustrating.
Thanks for all your help here deadlock. It's much appreciated.
Okay, nevermind that. I got it. Careless mistake on my part. I must have undid something or something, because the code wasn't right. But I fixed it. No compiler errors. However, now the functions don't do anything on the MOUSE_OVER. Ah!!
I need to take a break from this for a little while.
// usually nice to define all of these at the top = ]
var boxTween:Tween;
var box2Tween:Tween;
function setupVars():void {
////// try this
var myArray:Array = new Array();
myArray.push(box1_mc, box2_mc, box3_mc, box4_mc);
/////////////////
for(var i:Number = 0; i < myArray.length; i++) {
myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
myArray[i].addEventListener(MouseEvent.MOUSE_OUT, boxOut);
myArray[i].alpha = 0
myArray[i].buttonMode = true;
}
}
function boxOver(ev:Event):void {
// ev is a target of what got an event listener one property is currentTarget which is one specific instance of myArray
boxTween = new Tween(ev.currentTarget, "alpha", None.easeNone, 0, 1, .5, true);
}
function boxOut(ev:Event):void {
box2Tween = new Tween(ev.currentTarget, "alpha", None.easeNone, 1, 0, .5, true);
}
The problem with your code is that your never calling the function setupVars.
Add "setupVars();" to the end of your code on a new line, without the quotes.
The functions are there, but you just have not started the one function which starts the rest of your code.
But the reason for that error and anytime that you see Expected 0 got 1 means that you have setupVars( ZERO items here ) but the compiler is expecting: setupVars( ONE thing here)
in your case the compiler is expecting setupVars(evn:Event) from your enterFrame event. any function that is triggered from an event needs to have an Event var defined in the function.
but mouse event and mouse over will happen any time you mouse hovers over the boxes, so if you make setup vars work on an enterframe, you in tern create a problem of adding event listeners to the objects that already have them.
something you may even want to consider doing (after you get it to work ; P ) is working with the event listeners in this fashion:
Code:
function setupVars():void {
var myArray:Array = [box1_mc, box2_mc];
for(var i:Number = 0; i < myArray.length; i++) {
myArray[i].addEventListener(MouseEvent.MOUSE_OVER, boxOver);
myArray[i].buttonMode = true;
}
}
var box:Tween;
var box2: Tween;
function boxOver(ev:Event):void {
ev.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, boxOver);
ev.currentTarget.addEventListener(MouseEvent.MOUSE_OUT, boxOut);
box = new Tween(ev, "width", Elastic.easeIn, 100, 130, .5, true);
box = new Tween(ev, "height", Elastic.easeIn, 100, 130, .5, true);
trace("yay, it works");
}
function boxOut(ev:Event):void {
ev.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, boxOut);
ev.currentTarget.addEventListener(MouseEvent.MOUSE_OVER, boxOver);
box2 = new Tween(ev, "width", Elastic.easeOut, 130, 100, .5, true);
box2 = new Tween(ev, "height", Elastic.easeOut, 130, 100, .5, true);
trace("yay, this one works, too");
}
the reason for doing it like that is just a small performance/resource consideration, something you might want to keep in mind when these as3 projects get larger.