Flash CS5 Actionscript 3.0 Go to Next Frame and Play Code - actionscript-3

I am coding a simple movie timeline in Flash CS5 actionscript 3.0. I have all my buttons working except for the Play button which I would like to code to say "go to next frame and play". Here is my current actionscript can anyone help me to find the correct code?
fastforwardBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame);
function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
gotoAndStop(4025);
}
stop();
gotoStopBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_2);
function fl_ClickToGoToAndStopAtFrame_2(event:MouseEvent):void
{
gotoAndStop(1);
}
gotoPlayBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame);
function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
gotoAndPlay(1);
}
stopBtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_2);
function fl_ClickToGoToNextFrame_2(event:MouseEvent):void
{
nextFrame();
}
playBtn.on (release) ;{
_root.gotoAndPlay(_currentframe+1);
}

playBtn.addEventListener(MouseEvent.CLICK, fl_Click_Play);
function fl_Click_Play(event:MouseEvent):void
{
MovieClip(root).play();
}
should work, if you have a simple fla. And if that doesn't seem to work, you may need something like this to target the right path:
MovieClip(this.parent).play();
Without knowing your fla structure, its just a guess ;)
also = _root is AS2, and always targets _level0. This is changed in AS3 to just "root", and targets the timeline of the topmost displayObjectContainer.

Related

Rewind animations in Flash CS3

I have made an animation in Flash CS3 and i need to reverse this in the timeline.
I tried this, but it did not work:
if(!this.flag)
{
this.flag=1;
this.stop();
this.fwd_btn.addEventListener("click", fl_MouseClickHandler.bind(this));
function fl_MouseClickHandler() {
this.on("tick", ffwd.bind(this));
}
this.rew_btn.addEventListener("click", fl_MouseClickHandler_2.bind(this));
function fl_MouseClickHandler_2() {
this.on("tick", rewind.bind(this));
}
function ffwd(e) {
this.gotoAndStop(this.currentFrame + 1);
}
function rewind(e) {
this.gotoAndStop((this.currentFrame + this.getDuration() - 1) % this.getDuration());
}
}
How can i do this by using Action Script code or by using the timeline functions?
You should try Greensock TweenMax with frame plugin, it allows you to go to a particular frame (even in reverse) very simply. You can also target a label.
TweenMax.to(this, 2, {frame:1});
//In 2 seconds, stage timeline will go to first frame.
Have a look here : http://www.snorkl.tv/2010/10/overview-of-tweenmax-framelabel-and-frame-plugins-nifty-way-to-play-a-flash-timeline-backwards/

FLASH AS3 Sound Overlap

Alright, I'm a total noob in flash as3 so this must be very easy to solve I guess. I'm making a soundboard with recorded voices in flash cs6, very simple: 1 frame, ten buttons, each button makes a different sound. The problem is the overlapping of these sounds, so what I need is that when I press one button the other sounds stop playing. anyone please?
See the play() method documentation in the Sound class, it returns a SoundChannel object, which has a stop() method.
So you could do it like that (schematically) :
var currentChannel:SoundChannel;
button1.addEventListener(MouseEvent.CLICK, onButtonClick);
button2.addEventListener(MouseEvent.CLICK, onButtonClick);
button3.addEventListener(MouseEvent.CLICK, onButtonClick);
function onButtonClick(event:MouseEvent):void
{
/* provided you have implemented selectSoundByButton func somewhere */
const sound:Sound = selectSoundByButton(event.currentTarget);
if (currentChannel) {
currentChannel.stop();
}
currentChannel = sound.play();
}
More detailed description:
Let's say you want to create yet another fart button application in flash. That's what you have to do:
Create a button symbol, add it to stage and give it an instance name in properties tab. Let's call it myButton
Add the sound to library with file->import
Export this sound to actionscript. Right click on a sound in library, check "Export for actionscript", "export in frame 1" on "actionscript tab". Fill "Class" input with a desired class name for a sound (e.g. MySound)
Then you have to trigger the sound playback on your button click. So you should put the following code to the first frame of your flash clip:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var currentChannel:SoundChannel;
const mySound:Sound = new MySound();
function onClick(e:MouseEvent):void {
if (currentChannel) {
currentChannel.stop();
}
currentChannel = mySound.play();
}
myButton.addEventListener(MouseEvent.CLICK, onClick);
Add this to your code on each button before playing a sound:
SoundMixer.stopAll();
If you're adding actions directly from the timeline in Adobe Flash, there's no need to import the class. If you're working from an IDE like FlashDevelop or FlashBuilder, add this code to the beginning (after Package {):
import flash.media.SoundMixer;
Happy coding!
Edit: More info on the SoundMixer class

Action Script 3.0: Move Movieclip file to another project;

me and friend are working together to create and rpg game. And i ask my friend to make an opening animation which the file format that he gave me is .gif . then, when i put that file to my project, it adds bunch of bitmap(1000+) in the library that make the size of our project a lot bigger. so, i asked him again to remake it but on another project(.fla) in a movieclip. So, my problem is how i'm going to tranfers that movieclip to our main project?
p.s. we're using AdobeFlashProfessional CS6. Sorry for my bad english.
Thanks.
Your friend makes animation in fla and complies it into swf movie.
You import the swf movie into your project's library (File -> Import to Library)
In the library, you assign this movie a class name like OpeningMovie
You can use it in your code as
var opening_movie:OpeningMovie = new OpeningMovie();
addChild(opening_movie);
You will probably want to know when the movie has ended so you can continue:
opening_movie.addEventListener(Event.ENTER_FRAME, checkOpeningEnd, false, 0, true);
stop();
function checkOpeningEnd(evt:Event) {
var mc:MovieClip = evt.target as MovieClip;
if (mc.currentFrame == mc.totalFrames) {
mc.removeEventListener(Event.ENTER_FRAME, checkOpeningEnd, false);
mc.parent.removeChild(mc);
mc.stop();
play();
}
}

External ActionScript Only Executed When Testing Single Scene

New to AS 3.0 and I seem to have an issue where an external AS file is run when I test an individual scene in Flash Pro but not when I test the entire movie or when I test from Flash Builder. Anyone know what the problem might be?
Here's the code from the AS file:
package {
import flash.display.MovieClip;
public class Level1 extends MovieClip {
public var myplayer:MyPlayer;
public function Level1() {
super();
myplayer.x = 516;
myplayer.y = 371;
if (myplayer.x == 516)
{
trace("player x is 516");
}
else if (myplayer.y == 371)
{
trace("player y is 371");
}
}
}
}
Any ideas?
EDIT
I think I figured out the problem. The swf contained two scenes, and the external AS file started running at the start of Scene 1, but the myPlayer movie clip was not instantiated until Scene2, which, I think was causing the problem I was having, in addition to giving a 1009 null object error.
So I simply deleted the first scene, and now everything works fine. Maybe I will put that first scene in a separate SWF? Or, is there some way to delay a script's execution until a certain scene?
Your problem:
When the constructor of your doucment class runs, myPlayer doesn't yet exist so it throws a 1009 runtime error and exits the constructor at the first reference to myPlayer.
Solutions:
Put all the myPlayer code on the first frame of the MyPlayer timeline. OR use your current document class as the class file for MyPlayer (instead of documentClass). Change all references to myPlayer to this.
Listen for frame changes and check until myPlayer is populated, then run your code.
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
function enterFrameHandler(e):void {
if(myPlayer){
//run the myPlayer code
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
}
If your frame rate is 24fps, this code though will run 24 times every second (until it finds myPlayer), so it's not the most performant way to go.
Use events. Add an event to the first frame of myPlayer (or to a class file for MyPLayer) that tells the document class that it exists now.
stage.dispatchEvent(new Event("myPlayerReady"));
Then listen for that event on the document class:
stage.addEventListener("myPlayerReady",playerReadyHandler);
playerReadyHandler(e:Event):void {
//your player code
var myPlayer = MyPlayer(e.target); //you can even get the reference from the event object
}
Thanks for your constructive, helpful responses, LDMS. I thought I had found a solution when I hadn't. Your advice worked. What I did was add the following code to the timeline of MyPlayer
this.x = 516;
this.y = 371;
if (this.x == 516)
{
trace("player x is 516");
}
if (this.y == 371)
{
trace("player y is 371");
}
and I removed the code from the document class. Everything seems to be working fine now. Thanks again for your help!

Changing individual movieclip's frame rate with TweenMax

My aim is to change the frame rates of my individual (looping) movie clips through clickable controls (slow/med/fast). I've heard it isnt possible to achieve this through as3/flash alone, so I've tried greensock's TweenMax... However I can't seem to figure out how to do this. Is there anyone that could help?
box1.addEventListener(MouseEvent.MOUSE_DOWN, box1down);
function box1down(event:MouseEvent):void {
//FRAMERATE CODE HERE
}
Many thanks!
Here is the API doc for TweenMax: http://www.greensock.com/as/docs/tween/com/greensock/TweenMax.html
If you have multiple movieclips that you are trying to control, you can just create an abstract class with the functionality you want and extend that class. So something like:
public class ControlledMovieClip extends MovieClip {
public function ControlledMovieClip() {
stop();
}
public function animate(frameRateInSeconds:Number):void {
TweenMax.to(this, frameRateInSeconds, { frame: this.totalFrames - 1, repeat: -1, ease: Linear.easeNone });
}
}
Have all your movieclips that are looping extend that class, and then you could call the animate function on the objects in your box1down event handler.
I haven't tested that code so you might need a gotoAndStop(1) at the end of each iteration.
It's possible through Actionscript alone it just requires you to handle the frame progression yourself (instead of using mc.play() you stop the movieclip and call nextFrame() yourself).
Lets say a Movieclip (myMC) has 20 frames of animation. To manually run the animation at normal speed you simply call myMC.nextFrame(); on every frame of your project (using an ENTER_FRAME listener for example).
To have the animation run at half speed you can use a frame count and a frame trigger:
var frameTick = 0;
var frameAnimTrigger = 2;
function Update(e:Event):void
{
frameTick++;
if(frameTick == frameAnimTrigger)
{
myMC.nextFrame();
frameTick = 0;
}
}
Because nextFrame is only called every other frame the animation appears to run at half speed.