|
-
Is this even possible? Playing mc instances sequentially in an array.
I have a movieclip tweened of a lightbulb bright then fading. 50 or so instances are placed within another movieclip and given instance names of bulb0 to bulb50. I have a stop action on the first frame of this movieclip.
Other animation within the main movieclip is activated on rollover and I have used actionscript to place this main movieclip on the stage and to control the other animation within it.
Within my external as. I have an array as follows:
var bulbclips:Array = new Array();
for (var i:uint; i < 58; i++) {
bulbclips.push("winNav.bulb"+i);
var index:int = 0;
trace("winNav.bulb"+i);
}
which successfully traces the loop of the instances of the bulbs.
What i'm trying to get is to mouse over the main movieclip and for the instances of the movieclips within the main movieclip to light up and fade in sequence from 0 to 50 (each one starting a little later). I've tried adding:
bulbclips[index].play();
to my function among many other things but no joy so far....
any ideas appreciated!
G
p.s. do i even need the array?
Last edited by G-man; 02-26-2009 at 11:28 AM.
-
You are putting Strings in your bulbclips array. You probably wanted to put the actual clips in the array. The code below assumes that winNav is in scope.
Code:
var bulbclips:Array = new Array();
var bulb:Movieclip;
for (var i:uint = 0; i < 58; i++) {
bulb = winNav.getChildByName("bulb"+i) as MovieClip;
bulbclips.push(bulb);
trace("winNav.bulb"+i);
bulb.addEventListener(Event.ENTER_FRAME, cascade);
}
function cascade(e:Event):void{
var b:MovieClip = e.currentTarget as MovieClip;
if (b.currentFrame == b.totalFrames){ //could also use totalFrames/2 or another frame index to adjust overlap.
var index:int = int(b.name.substring("bulb".length)) + 1;
if (index == bulbclips.length){
index = 0;
}
var bnext:MovieClip = bulbclips[index];
bnext.gotoAndPlay(1);
}
}
-
Hi there, thanks for the help on this though i'm confused by your comment 'the code below assumes that winNav is in scope. I've had a quick read up on scope but still unfamiliar. To help you understand... code pasted.
Code:
----------
package com.site.microsite{
import flash.display.*;
import flash.events.*;
public class WinathonMC extends MovieClip {
public function WinathonMC() {
var winNav:WinNav = new WinNav();
addChild(winNav);
winNav.x = stage.stageWidth/2;
winNav.y = 575;
winNav.nav3spotlight0.rotation = 50;
winNav.nav3spotlight1.rotation = 120;
winNav.nav3spotlight2.rotation = 60;
winNav.nav3spotlight3.rotation = 140;
//spotlight variables
var spotlightCW0:Boolean = true;
var spotlightCW1:Boolean = true;
var spotlightCW2:Boolean = true;
var spotlightCW3:Boolean = true;
var spotlight00rot:Number = winNav.nav3spotlight0.rotation;
var spotlight01rot:Number = winNav.nav3spotlight1.rotation;
var spotlight02rot:Number = winNav.nav3spotlight2.rotation;
var spotlight03rot:Number = winNav.nav3spotlight3.rotation;
var bulbclips:Array = new Array();
for (var i:uint; i < 58; i++) {
bulbclips.push("winNav.bulb"+i);
var index:int = 0;
trace("winNav.bulb"+i);
}
winNav.addEventListener(MouseEvent.MOUSE_OVER,onNa vOver);
winNav.addEventListener(MouseEvent.MOUSE_OUT,onNav Out);
function onNavOver(evt:MouseEvent) {
addEventListener(Event.ENTER_FRAME,onEnterFrame);
function onEnterFrame() {
// rotation of spotlight 1 on mouse event
if (spotlight00rot > 70) {
spotlightCW0 = false;
}
if (spotlight00rot < 30) {
spotlightCW0 = true;
}
if (spotlightCW0 == true) {
winNav.nav3spotlight0.rotation = (spotlight00rot += .4);
} else {
winNav.nav3spotlight0.rotation = (spotlight00rot -= .4);
}
// rotation of spotlight 2 on mouse event
if (spotlight01rot > 120) {
spotlightCW1 = false;
}
if (spotlight01rot < 70) {
spotlightCW1 = true;
}
if (spotlightCW1 == true) {
winNav.nav3spotlight1.rotation = (spotlight01rot += .6);
} else {
winNav.nav3spotlight1.rotation = (spotlight01rot -= .6);
}
// rotation of spotlight 3 on mouse event
if (spotlight02rot > 120) {
spotlightCW2 = false;
}
if (spotlight02rot < 50) {
spotlightCW2 = true;
}
if (spotlightCW2 == true) {
winNav.nav3spotlight2.rotation = (spotlight02rot += .6);
} else {
winNav.nav3spotlight2.rotation = (spotlight02rot -= .6);
}
// rotation of spotlight 4 on mouse event
if (spotlight03rot > 160) {
spotlightCW3 = false;
}
if (spotlight03rot < 90) {
spotlightCW3 = true;
}
if (spotlightCW3 == true) {
winNav.nav3spotlight3.rotation = (spotlight03rot += .4);
} else {
winNav.nav3spotlight3.rotation = (spotlight03rot -= .4);
}
//winNavigation.bulb20.play()
bulbclips[index].play();
}
}
function onNavOut(evt:MouseEvent) {
addEventListener(Event.ENTER_FRAME,onEnterFrame);
function onEnterFrame() {
//rotation stopped for spotlight 1 on mouse out
if (spotlightCW0 == true) {
winNav.nav3spotlight0.rotation = (spotlight00rot -= .4);
} else {
winNav.nav3spotlight0.rotation = (spotlight00rot += .4);
}
//rotation stopped for spotlight 2 on mouse out
if (spotlightCW1 == true) {
winNav.nav3spotlight1.rotation = (spotlight01rot -= .6);
} else {
winNav.nav3spotlight1.rotation = (spotlight01rot += .6);
}
//rotation stopped for spotlight 3 on mouse out
if (spotlightCW2 == true) {
winNav.nav3spotlight2.rotation = (spotlight02rot -= .6);
} else {
winNav.nav3spotlight2.rotation = (spotlight02rot += .6);
}
//rotation stopped for spotlight 4 on mouse out
if (spotlightCW3 == true) {
winNav.nav3spotlight3.rotation = (spotlight03rot -= .4);
} else {
winNav.nav3spotlight3.rotation = (spotlight03rot += .4);
}
}
}
}
}
}
-----------
Sorry! Lots of it! As you can see nothing has been physically lifted onto the stage, it's all placed with actionscript from my library items. winNav (my main container movieclip) contains other movieclips that i need to target.
Does this help and does your solution still stand?
-
Please use the [ code ] tags to format code to preserve indentation and readability. Here, I've done it for you this time.
Original code, re-formatted for quasi-readability:
Code:
package com.site.microsite{
import flash.display.*;
import flash.events.*;
public class WinathonMC extends MovieClip {
public function WinathonMC() {
var winNav:WinNav = new WinNav();
addChild(winNav);
winNav.x = stage.stageWidth/2;
winNav.y = 575;
winNav.nav3spotlight0.rotation = 50;
winNav.nav3spotlight1.rotation = 120;
winNav.nav3spotlight2.rotation = 60;
winNav.nav3spotlight3.rotation = 140;
//spotlight variables
var spotlightCW0:Boolean = true;
var spotlightCW1:Boolean = true;
var spotlightCW2:Boolean = true;
var spotlightCW3:Boolean = true;
var spotlight00rot:Number = winNav.nav3spotlight0.rotation;
var spotlight01rot:Number = winNav.nav3spotlight1.rotation;
var spotlight02rot:Number = winNav.nav3spotlight2.rotation;
var spotlight03rot:Number = winNav.nav3spotlight3.rotation;
//array that holds bulb instances and sets the index at 0
//var bulbclips:Array = [winNavigation.bulb0, winNavigation.bulb1, winNavigation.bulb2, winNavigation.bulb3];
//var index:int = 0;
var bulbclips:Array = new Array();
for (var i:uint; i < 58; i++) {
bulbclips.push("winNav.bulb"+i);
var index:int = 0;
trace("winNav.bulb"+i);
}
winNav.addEventListener(MouseEvent.MOUSE_OVER,onNavOver);
winNav.addEventListener(MouseEvent.MOUSE_OUT,onNavOut);
function onNavOver(evt:MouseEvent) {
addEventListener(Event.ENTER_FRAME,onEnterFrame);
function onEnterFrame() {
// rotation of spotlight 1 on mouse event
if (spotlight00rot > 70) {
spotlightCW0 = false;
}
if (spotlight00rot < 30) {
spotlightCW0 = true;
}
if (spotlightCW0 == true) {
winNav.nav3spotlight0.rotation = (spotlight00rot += .4);
} else {
winNav.nav3spotlight0.rotation = (spotlight00rot -= .4);
}
// rotation of spotlight 2 on mouse event
if (spotlight01rot > 120) {
spotlightCW1 = false;
}
if (spotlight01rot < 70) {
spotlightCW1 = true;
}
if (spotlightCW1 == true) {
winNav.nav3spotlight1.rotation = (spotlight01rot += .6);
} else {
winNav.nav3spotlight1.rotation = (spotlight01rot -= .6);
}
// rotation of spotlight 3 on mouse event
if (spotlight02rot > 120) {
spotlightCW2 = false;
}
if (spotlight02rot < 50) {
spotlightCW2 = true;
}
if (spotlightCW2 == true) {
winNav.nav3spotlight2.rotation = (spotlight02rot += .6);
} else {
winNav.nav3spotlight2.rotation = (spotlight02rot -= .6);
}
// rotation of spotlight 4 on mouse event
if (spotlight03rot > 160) {
spotlightCW3 = false;
}
if (spotlight03rot < 90) {
spotlightCW3 = true;
}
if (spotlightCW3 == true) {
winNav.nav3spotlight3.rotation = (spotlight03rot += .4);
} else {
winNav.nav3spotlight3.rotation = (spotlight03rot -= .4);
}
//winNavigation.bulb20.play()
bulbclips[index].play();
//advance bulb clip order
//function advance():void {
//(index < bulbclips.length-1) ? index++ : index=0; //advance the index to play next bulb
//bulbclips[index].play(); //play the clip
//}
//
////start the clips playing
//bulbclips[index].play();
}
}
function onNavOut(evt:MouseEvent) {
addEventListener(Event.ENTER_FRAME,onEnterFrame);
function onEnterFrame() {
//rotation stopped for spotlight 1 on mouse out
if (spotlightCW0 == true) {
winNav.nav3spotlight0.rotation = (spotlight00rot -= .4);
} else {
winNav.nav3spotlight0.rotation = (spotlight00rot += .4);
}
//rotation stopped for spotlight 2 on mouse out
if (spotlightCW1 == true) {
winNav.nav3spotlight1.rotation = (spotlight01rot -= .6);
} else {
winNav.nav3spotlight1.rotation = (spotlight01rot += .6);
}
//rotation stopped for spotlight 3 on mouse out
if (spotlightCW2 == true) {
winNav.nav3spotlight2.rotation = (spotlight02rot -= .6);
} else {
winNav.nav3spotlight2.rotation = (spotlight02rot += .6);
}
//rotation stopped for spotlight 4 on mouse out
if (spotlightCW3 == true) {
winNav.nav3spotlight3.rotation = (spotlight03rot -= .4);
} else {
winNav.nav3spotlight3.rotation = (spotlight03rot += .4);
}
}
}
}
}
}
No offense intended, but that code hurts my brain. You have functions nested 3 deep for no particular reason. It looks like you're not familiar with classes and scoping.
Yes, the basic technique I outlined at first should still work.
Instead of declaring ever more nested functions so that things will have access to local variables declared in other functions, use instance level variables.
Somewhat improved code:
Code:
package com.site.microsite{
import flash.display.*;
import flash.events.*;
import flash.utils.Dictionary;
public class WinathonMC extends MovieClip {
private var winNav:WinNav;
private var bulbClips:Array;
//ideally, these would go away in favor of an actual Spotlight class.
private var spotPos:Dictionary;
private var spotInc:Dictionary;
private var spots:Array;
public function WinathonMC() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void{
winNav = new WinNav();
addChild(winNav);
winNav.x = stage.stageWidth/2;
winNav.y = 575;
winNav.nav3spotlight0.rotation = 50;
winNav.nav3spotlight1.rotation = 120;
winNav.nav3spotlight2.rotation = 60;
winNav.nav3spotlight3.rotation = 140;
//spotlight variables
spots = [winNav.nav3spotlight0, winNav.nav3spotlight1, winNav.nav3spotlight2, winNav.nav3spotlight3];
spotPos = new Dictionary();
for (var i:int = 0; i < spots.length; i++){
spotPos[spots[i]] = true;
}
spotInc = new Dictionary();
spotPos[winNav.nav3spotlight0] = 0.4;
spotPos[winNav.nav3spotlight1] = 0.6;
spotPos[winNav.nav3spotlight2] = 0.6;
spotPos[winNav.nav3spotlight3] = 0.4;
bulbclips = new Array();
for (var i:uint; i < 58; i++) {
bulbclips.push(winNav.getChildByName("bulb"+i) as MovieClip);
trace("winNav.bulb"+i);
}
winNav.addEventListener(MouseEvent.MOUSE_OVER,onNavOver);
winNav.addEventListener(MouseEvent.MOUSE_OUT,onNavOut);
}
private function onNavOver(evt:MouseEvent):void {
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(e:Event):void{
// rotation of spotlight 1 on mouse event
adjustRot(30, 70, spots[0]);
// rotation of spotlight 2 on mouse event
adjustRot(70, 120, spots[1]);
// rotation of spotlight 3 on mouse event
adjustRot(50, 120, spots[2]);
// rotation of spotlight 4 on mouse event
adjustRot(90, 160, spots[3]);
}
private function adjustRot(min:int, max:int, target:MovieClip):void{
// rotation of spotlight on mouse event
if (target.rotation > max) {
spotPos[target] = false;
}
if (target.rotation < min) {
spotPos[target] = true;
}
if (spotPos[target]) {
target.rotation += spotInc[target];
} else {
target.rotation -= spotInc[target];
}
}
private function onNavOut(evt:MouseEvent):void {
addEventListener(Event.ENTER_FRAME,onEnterFrameNavOut);
}
private function onEnterFrameNavOut(e:Event):void {
for (var i:int = 0; i < spots.length; i++){
if (spotPos[spots[i]]){
spots[i].rotation -= spotInc[spots[i]];
}else{
spots[i].rotation += spotInc[spots[i]];
}
}
}
}
This so far, is just a rewrite of your existing code. I'm afraid I don't see how you want the bulbs to tie in to the spotlights. I also don't quite get what it's supposed to be doing, so my refactoring was at a surface level.
Also, you will probably want navOver and navOut to remove the EnterFrame listeners that the other one adds. Otherwise you'll get both at once.
-
Hi there
Hey 5 tons. No offence taken. I'm a total newb to AS3 and so you're right, I'm still getting to grips with classes and i only really read about scope after you mentioned it. The learning curve for me here is steeeeeeep. However, thank you so much for your input.
To explain. The spotlights and bulbs are different parts of the same movieclip. The 4 spotlights create 4 searchlights in the main area of the movieclip and the bulbs are flashing clockwise around the header panel for the movie clip as this movieclip will ultimately be a button for a prize section of the site.
Apologies for the code thing... first time i've properly used this forum! As i said total newb here.
Yes, the code hurt my head too trying to write it. I was hoping there may have been a shorter way to write this but in my inexperience, that is what i came up with.
I really totally appreciate you looking at this and doing the re-write. All i need to do now is read it through and understand it before i implement it.
Again, many thanks and I will keep you posted of my progress.
G
-
Additional update
Hey man,
Have done a straight copy paste to start from the re-written code you wrote and I instantly get script errors. I can't seem to get it to work. Once pasted, I get a '1084: Syntax error: expecting rightbrace before end of program.
If I then add the right brace at the end of the code and re-publish I then get 'Line 48: 1151: A conflict exists with definition i in namespace internal.'
Because of my unfamiliarity with your code, I'm struggling slightly! I think I need to invest in more books and more time!!!
Really appreciate your help with this.
G-man
Last edited by G-man; 02-26-2009 at 07:59 PM.
-
Not entirely surprising, I didn't actually attempt to compile. The first error means that I forgot a closing } somewhere. Yeah, add another at the end of the file. The one to close the class is missing above.
The second error means that the variable i was already defined when I tried to use it again. You can change it to anything else (j is traditional), or you can move the var declaration outside of the loop initializer.
-
Many thanks once more for the very tidy re-write.
Yeah have solved those initial errors, and also amended the upper case C on bulbClips to a lowercase. Now I've done that I just need to address the other amends like the remove event listeners on nav out and over.
Once that's done.... I'm going to go swot up on classes so I can understand it a bit more and will then look at it again.
Thanks once again for all your help on this.
G
Tags for this Thread
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
|