Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Play random sound

Community Beginner ,
Oct 22, 2018 Oct 22, 2018

I have imported 4 songs into the Adobe Animate CC library, and I want to randomly play one of these songs via AS3.

I have given them class-names: "music_1", "music_2", "music_3" and "music_4"

I am trying something like this:

var mySound:Sound = new music_[Math.floor(Math.random() * 4 + 1)]();

mySound.play();

(It gives an error: "Access of undefined property music_.")

With ActionScript 2, I could do this:

this.createEmptyMovieClip("myMC",1);

mySound = new Sound(myMC);

mySound.attachSound("music_" + Math.floor(Math.random() * 4 + 1));

mySound.start(0,1);

But can't figure out how to do it in ActionScript 3.

TOPICS
ActionScript
775
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 22, 2018 Oct 22, 2018

try:

var C:Class=Class(getDefinitionByName("music_"+(1+Math.floor(4*Math.random()))));

var mySound=new C();

mySound.play()

Translate
Community Expert ,
Oct 22, 2018 Oct 22, 2018

try:

var C:Class=Class(getDefinitionByName("music_"+(1+Math.floor(4*Math.random()))));

var mySound=new C();

mySound.play()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 23, 2018 Oct 23, 2018

Beautiful! Thank you very much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 23, 2018 Oct 23, 2018
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines