PDA

View Full Version : Trouble dynamically adding movieclips to the stage


ChiWhiteSox56
10-20-2009, 03:58 PM
Hello all,

I am trying to write some code for someone show wants to be able to control their timeline animation by using the slider component. Everything was going fine until I tried to add the Play and Pause buttons to the stage. I instantiated both movieclips and added "stage.addChild(<mcName>);" but neither clip showsup when I test the movie. I tried removing "stage." but that didn't help either.

I tried to find a solution to my problem online, and while I haven't figured it out yet, it seems that these movieclips might need to be part of a function. This could be totally incorrect, but it's the best guess I have right now.

Any help would be greatly appreciated. My AS code is below. (I couldn't get the .FLA file to zip small enough, but if anyone wants it, I'll find a way.) See comment "// PLAY/PAUSE CODE" for Play/Pause buttons.

Thanks,
Cindy


//SLIDER CODE

// Import slider and movieclip classes
import fl.controls.Slider;
import fl.events.SliderEvent;
import flash.display.MovieClip;

// Instantiate slider
var mcSlider:Slider = new Slider();

// Add slider to stage
addChild(mcSlider);

// Position slider
mcSlider.move(20,400);

// Set slider size
mcSlider.setSize(280,1);

// Determine slider incrememtal behavior
mcSlider.maximum = mcBall.totalFrames;
mcSlider.liveDragging = true;

// Make animation move in relation to slider
mcSlider.addEventListener(Event.CHANGE, updatemc)
function updatemc(event:Event)
{
mcBall.gotoAndStop(mcSlider.value)
} // end function updatemc

// Make slider nub match movieclip position
mcSlider.addEventListener(Event.ENTER_FRAME, matchFrames);
function matchFrames(event:Event):void
{
mcSlider.value = mcBall.currentFrame;
if(mcSlider.value == mcBall.currentFrame)
{
mcBall.play();
}
if(mcSlider.value == mcSlider.maximum)
{
mcBall.stop();
}
}

// PLAY/PAUSE CODE

// Instantiate buttons
var mcPlay:MovieClip = new MovieClip();
var mcPause:MovieClip = new MovieClip();

// Add Play and Pause buttons to stage
stage.addChild(mcPlay);
stage.addChild(mcPause);

// Position Play and Pause buttons to stage
mcPlay.x = 160;
mcPlay.y = 334;
mcPause.x = 160;
mcPause.y = 334;

// Make "Play" button invisible whie movieclip plays
mcPlay.visible = false;
mcPause.visible = true;

// Play/pause button functionality - Two functions
mcPause.addEventListener(MouseEvent.CLICK, pauseBall)
function pauseBall(event:MouseEvent):void
{
mcPlay.visible = true;
} // end function pauseBall

mcPlay.addEventListener(MouseEvent.CLICK, playBall)
function playBall(event:MouseEvent):void
{
mcPlay.visible = false;
} // end function playBall

raydowe
10-20-2009, 04:01 PM
Not sure what your expecting or maybe I'm just missing something. I had a quick look at your code and you are creating two empty movie clips and adding them to the stage, so your not going to see anything whether they are added or not.

Are you getting an error?

ChiWhiteSox56
10-20-2009, 04:08 PM
No, I'm not getting an error.

What's happening is that the ball movieclip is showing up, and the slider used to control it shows up and works properly (this was added in the //SLIDER CODE part), but the Play and Pause icons do not.

I'm curious about the movieclips being empty. That sounds like it could very well be the problem, but I'm afraid I don't know how to fix that. I thought that what I did would put the Play and Pause on the stage, but clearly that's not the case. :)

raydowe
10-20-2009, 04:14 PM
Nope,

// Instantiate buttons
var mcPlay:MovieClip = new MovieClip();
var mcPause:MovieClip = new MovieClip();

// Add Play and Pause buttons to stage
stage.addChild(mcPlay);
stage.addChild(mcPause);


This is just making two empty movie clips and putting them on the stage. Where do the play and pause buttons exist right now?

ChiWhiteSox56
10-20-2009, 04:32 PM
Right now, the Play and Pause movieclips are in the Library as mcPlay and mcPause. I have Export for Actionscript and Export in Frame 1 checked for both, and the Class for each is mcPlay or mcPause. Both have a Base Class of flash.display.MovieClip.

raydowe
10-20-2009, 04:50 PM
Ok, sounds like your setup right, just had some wrong code.

You want:

var mcPlay_mc = new mcPlay();
var mcPause_mc = new mcPause();


Because you are exporting those symbols for AS and giving them a class, you want to make a new instance of each of those classes.

ChiWhiteSox56
10-20-2009, 06:28 PM
Beautiful!! Worked like a charm.

Thanks so much for your help - I learned something today! :)

raydowe
10-20-2009, 06:58 PM
np, glad I could help :)