Totally confused by actionscript - actionscript-3

I have this code in ActionScript 2, it animates to audio input in my VJ software.
Question:
How do I re-write this as AS3 code?
var audiofft1 :Number;
circle1.onEnterFrame = function(){
this._xscale = audiofft1
this._yscale = audiofft1
}
stop()

The migration es this:
var audiofft1 :Number;
circle1.addEventListener(Event.ENTER_FRAME, onEnterFramaCircle1);
function onEnterFramaCircle1(e:Event):void{
this._xscale = audiofft1;
this._yscale = audiofft1;
};
stop();
But what do you try to do?

Related

I am looking for a simple solution to loading a new/different mp3 sound file upon clicking a thumbnail using my existing code below

This seems simple enough, I just can't crack it though.
currently, in the below code. its a UILoader with a series of thumbnail mc's horizontally across the bottom of the FLA. when the thumbs are clicked and new SWF loads in the UI.
I am simply looking for the error of my ways, hopefully by simply added to this code and a possible explanation. I need to figure out how to play/stop on exit and new mp3 sound file.
UILoader
With movieClips used for thumbnail loads
var imagesXML:XML;
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("lessons/images/Images5.xml"));
/////////////////////////////////////////////////////////////
//////////// thumbnail loader /////////////////////////
function xmlLoaded(evt:Event):void
{
imagesXML = new XML(xmlloader.data);
var thumbLoader:UILoader;
for(var i:uint = 0; i < imagesXML.image.length(); i++)
{
thumbLoader UILoader(getChildByName("thumb" + 1));
thumbLoader.load(new URLRequest("lessons/images/thumbs/" + imagesXML.image[i].#file));
thumbLoader.buttonmode = true;
thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
var fullPath:String = "lessons/images/file-1.swf";
mainLoader.load(new URLRequest(fullpath));
}
}
/////////////////////////////////////////////////////////////
//////////////// load Images /////////////////////////////
function thumbClicked(evt:MouseEvent):void
{
var thumbName:String = evt.currentTarget.name;
var thumbIndex:uint = uint(thumbName.substr(5));
var fullPath:String = "lessons/images/" + imagesXML. image[thumbIndex].#file;
mainLoader.load(new URLRequest(fullPath));
}
you can try to use this library https://github.com/treefortress/SoundAS
or try to google "Sound manager for as3" and I sure you will find library you like
Declare sound object outside of functions so it is available to all functions
var mySound:Sound;
var myChannel:SoundChannel;
function thumbClicked(evt:MouseEvent):void
{
var thumbName:String = evt.currentTarget.name;
var thumbIndex:uint = uint(thumbName.substr(5));
var fullPath:String = "lessons/images/" + imagesXML. image[thumbIndex].#file;
mainLoader.load(new URLRequest(fullPath));
//stop any already playing sound
if ( myChannel != null) { myChannel.stop(); }
//load an MP3 file
mySound = new Sound(); mychannel = new SoundChannel();
mySound.load(new URLRequest("myAudio.mp3"));
mySound.play();
}

AS3 removing local enterFrame listener

(I don't know why the question doesn't show the "hello")
Anyway, hello all,
I have this code:
myVideo1.addEventListener(MetadataEvent.METADATA_RECEIVED, timeListener);
function timeListener(eventObject:MetadataEvent):void
{
var totalSeconds = String(eventObject.info.duration);
durationTime = String(Math.floor(totalSeconds));
addEventListener(Event.ENTER_FRAME, updateTime2);//<---LISTENER
var timeFull = durationTime;
function updateTime2(event:Event):void
{
var elapsedSeconds = String(Math.floor(myVideo1.playheadTime));
var runTime:String = (elapsedSeconds);
var timeGone = Math.floor((eventObject.info.duration) - (myVideo1.playheadTime));
var timeRem = Math.floor(timeGone / 60);
var secGone = String(timeGone / 60 - timeRem);
// etc...
}
}
This piece of code gets the remaining "seconds" of the video.
How can I remove the enterFrame listener? Where in the code?
It's printing the Error #1009 when jumping to the next frame of the main timeline.
Anyway, the movie is running "normally", so the question is for learning purposes.
Thanks in advance,
Cheers
removeEventListener(Event.ENTER_FRAME, updateTime2);
Is this what you wanted?

actionscript 3.0 function calling using button click

im creating a simple flash playlist using buttons, in my stage i have 4 buttons which is button for song1,song2, stop and play. i have a working code for this one, but i decided to revise it because my previous code is like, per song they have each stop and play button,so i created this one to have a dynamic stop and play, i created a function for each song, the function will change the filename of the song to be loaded,
heres the catch, so i first pick a song, (either song1 or song2) then i click stop, then when i select a new song this error appears
Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at playlist_fla::MainTimeline/songSelect1()
i think its not calling the second function because i cant see the trace i put inside it,anyway heres my code, sorry for the long post,
THANKS IN ADVANCE
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
var lastPosition:Number = 0;
var song;
song1.addEventListener(MouseEvent.CLICK,songSelect1);
function songSelect1(e:MouseEvent):void{
song = "<filenameofthe1stsong>";
mySound.load(new URLRequest(song));
myTransform.volume = 0.5;
myChannel.soundTransform = myTransform;
lastPosition=0;
trace(1);
}
song2.addEventListener(MouseEvent.CLICK,songSelect2);
function songSelect2(e:MouseEvent):void{
song = "<filenameofthe2ndsong>";
mySound.load(new URLRequest(song));
myTransform.volume = 0.5;
myChannel.soundTransform = myTransform;
lastPosition=0;
trace(2);
}
btnStop.addEventListener(MouseEvent.CLICK,onClickStop);
function onClickStop(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
}
btnPlay.addEventListener(MouseEvent.CLICK,onClickPlay);
function onClickPlay(e:MouseEvent):void{
myChannel = mySound.play(lastPosition);
myChannel.soundTransform = myTransform();
}
Correction:
According to Adobe:
Once load() is called on a Sound object, you can't later load a
different sound file into that Sound object. To load a different sound
file, create a new Sound object.
So, creating a new sound object would be the only way to fix it.
- - - Original Post - - -
If you enable debugging in your flash settings, it'd be easier to determine exactly what line is causing issues. That said, your code doesn't look incorrect apart from defining your sound transform twice. Try this:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
myChannel.soundTransform = myTransform;
var lastPosition:Number = 0;
var song;
song1.addEventListener(MouseEvent.CLICK,songHandler);
song2.addEventListener(MouseEvent.CLICK,songHandler);
btnStop.addEventListener(MouseEvent.CLICK,onClickStop);
btnPlay.addEventListener(MouseEvent.CLICK,onClickPlay);
function songHandler(e:MouseEvent):void {
switch (e.currentTarget.name) {
case "song1":
songSelect("<filenameofthe1stsong>")
break;
case "song2":
songSelect("<filenameofthe2ndsong>")
break;
}
}
function songSelect(songPath:String):void {
mySound.load(new URLRequest(songPath));
myChannel.soundTransform.volume = 0.5;
lastPosition = 0;
trace("Loading " + songPath);
}
function onClickStop(e:MouseEvent):void {
lastPosition = myChannel.position;
myChannel.stop();
}
function onClickPlay(e:MouseEvent):void {
myChannel = mySound.play(lastPosition);
myChannel.soundTransform = myTransform();
}

AS3 can't find the correct way to gotoAndStop a movieclip instance in as3

I have a movieclip "achtergrond" from my library which I put on stage with a function like this:
function set_game ()
{
oefNr = 4;
var bg:achtergrond = new achtergrond();
bg.x = 0;
bg.y = 0;
bg.name = "bg";
bg.gotoAndStop ("uit");
addChild (bg);
set_next ();
}
The movieclip contains 2 frames "aan" and "uit" and it starts on the frame "uit".
Further in my game I want to set the frame to "aan" while a sound is playing, like this:
function playSnd ():void
{
getChildByName("bg").gotoAndStop("aan");
snd = new Sound(new URLRequest("phonetic_" + curArr[curSnd] + ".mp3"));
cnl = snd.play();
cnl.addEventListener (Event.SOUND_COMPLETE, completeSnd);
}
But for the life of me I can't find the correct way to do this. Flash keeps going on about displayObjects and other things, and I have no clue why I can't address my movieclip. Actually, I have a clue, but no more than that. I don't understand this part of Flash very well yet.
The answer is that if I try to access my clip like this: this["bg"] or getChildByName("bg") I am referring to the DisplayObject.
This does not have all the methods of a MovieClip, like the gotoAndStop I need in this case.
I declared a new variable:
var movie:MovieClip;
Then I cast my DisplayObject as a MovieClip and put it into the var movie:
function set_game ()
{
oefNr = 4;
var bg:achtergrond = new achtergrond();
bg.x = 0;
bg.y = 0;
bg.name = "bg";
bg.gotoAndStop ("uit");
addChild (bg);
movie = this.getChildByName("bg") as MovieClip;
set_next ();
}
Now I can use MovieClip-specific methods like gotoAndStop:
function playSnd ():void
{
movie.gotoAndStop("aan");
snd = new Sound(new URLRequest("phonetic_" + curArr[curSnd] + ".mp3"));
cnl = snd.play();
cnl.addEventListener (Event.SOUND_COMPLETE, completeSnd);
}
Answer inspired by this answer
bg.name = "bg";
actually many times this code doesn't work properly. So, instead of setting name, you should get real instance name of your "achtergrond" object and than you have to use this instance name.
Another Solution:
I think achtergrond is single object so why do you use it with "getChildByName"? Use it like bg.gotoAndStop();.

Something like cookies in Flash/ActionScript

i need to implement something like a cookie in a flash file....and i dont have a clue about ActionScript.
Basically it is a video with a mute/unmute button. If i mute the video and refresh the browser it is not muted again.So i need to persist the mute status somehow.
Here is my complete ActionScript File:
import flash.net.SharedObject;
var a:Boolean = false;
var cookie:SharedObject = sharedobject.getLocal("muted");
if (cookie.data.muted == true) {
SoundMixer.soundTransform = new SoundTransform(0);
Object(root).ton_btn.gotoAndStop(2);
}
ton_btn.addEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler);
function fl_MouseOverHandler(event:MouseEvent):void
{
Object(root).ton_btn.buttonMode = true;
Object(root).ton_btn.useHandCursor = true;
}
ton_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
if (! a)
{
var muteStatus:Boolean = true;
cookie.data.muted = muteStatus;
SoundMixer.soundTransform = new SoundTransform(0);
Object(root).ton_btn.gotoAndStop(2);
trace(a);
}
else
{
var muteStatus:Boolean = false;
cookie.data.muted = muteStatus;
SoundMixer.soundTransform = new SoundTransform(1);
Object(root).ton_btn.gotoAndStop(1);
trace(a);
}
a = ! a;
}
This is not working, now my mute button is flickering....it seems, that the if clause is constantly executing.
Thanks for any tip, hint or link in advance. ;)
Regards
Nils
Edit:
So stupid...it was just a typo.
var cookie:SharedObject = sharedobject.getLocal("muted");
must be:
var cookie:SharedObject = SharedObject.getLocal("muted");
Now it is working.
I recommend following opensource that is popular ActionScript3.0 Cookie Frameworks. In general, two kinds of cookies in with the framework is known. Please see under open source. Solve your problem more quickly, you may give. Number 1 is a only Sourcode and Tutorial, Number 2 is a full of Frameworks.
Read and Write and Edit using a SharedObject
Actionscript3 Cookie Util