Copy link to clipboard
Copied
Hi,
I'm trying to use the static variables and methods for the first time.
So I took the code, I had previously on an upper level component, down to the related class file.
Its all working fine, but the sound does not want to play.
Gives the error: Invalid sound. The row is marked bold.
What is causing that error?
Many thanks in advance!
private static var numberForFlowerSound: uint = 0;
private static var flowerSound01: BellLoop01Multitrack = new BellLoop01Multitrack();
private static var flowerSound02: BellLoop02Multitrack = new BellLoop02Multitrack();
private static var flowerSound03: BellLoop03Multitrack = new BellLoop03Multitrack();
private static var flowerSound04: BellLoop04Multitrack = new BellLoop04Multitrack();
private static function countForFlowersSound(): void
{
numberForFlowerSound++;
if (numberForFlowerSound == 1)
{
flowerSound01.play();
}
else if (numberForFlowerSound == 2)
{
flowerSound02.play();
}
else if (numberForFlowerSound == 3)
{
flowerSound03.play();
}
else if (numberForFlowerSound == 4)
{
flowerSound04.play();
numberForFlowerSound = 0;
}
}
Copy link to clipboard
Copied
what's BellLoop01MultiTrack?
Copy link to clipboard
Copied
all the BellLoops are mp3 files.
Copy link to clipboard
Copied
attach a screenshot of your library that shows the mp3 linkages.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
that looks ok.
are you calling that 'private' static function from a public static function?
Copy link to clipboard
Copied
its called from a private function in the same class file.
I could copy the entire code, but its quite long.
When I comment out all four sound related rows, then the code is running as expected.
And it was running as expected, including sound, when it was in other class file and without the static.
But I want to learn coding with static var and methods.
Any ideas why the sound does not want to play?
Copy link to clipboard
Copied
well, is there a public static function the indirectly calls that private static function?
if not, how do you get that to execute?
Copy link to clipboard
Copied
I made simplified version of the class file, left out all animation part.
When the sound related rows (marked bold) are commented out and I click on different instances of that class on stage, I can see the expected trace statement. And it is counting up until four and then starting again from 0. As expected.
Whit the sound rows uncommented I still get the same error message.
package BoyWithScooter
{
import flash.display.*;
import flash.events.*;
import flash.media.Sound;
public class Flowers extends MovieClip
{
private static var numberForFlowerSound: uint = 0;
private static var flowerSound01: BellLoop01Multitrack = new BellLoop01Multitrack();
private static var flowerSound02: BellLoop02Multitrack = new BellLoop02Multitrack();
private static var flowerSound03: BellLoop03Multitrack = new BellLoop03Multitrack();
private static var flowerSound04: BellLoop04Multitrack = new BellLoop04Multitrack();
public function Flowers()
{
addedToStage_Flowers();
}
private function addedToStage_Flowers(): void
{
this.addEventListener(MouseEvent.CLICK, flower);
}
private function flower(event: MouseEvent): void
{
Flowers.countForFlowersSound();
}
private static function countForFlowersSound(): void
{
numberForFlowerSound++;
if (numberForFlowerSound == 1)
{
trace("numberForFlowerSound is " + numberForFlowerSound);
flowerSound01.play();
}
else if (numberForFlowerSound == 2)
{
trace("numberForFlowerSound is " + numberForFlowerSound);
flowerSound02.play();
}
else if (numberForFlowerSound == 3)
{
trace("numberForFlowerSound is " + numberForFlowerSound);
flowerSound03.play();
}
else if (numberForFlowerSound == 4)
{
trace("numberForFlowerSound is " + numberForFlowerSound);
flowerSound04.play();
numberForFlowerSound = 0;
}
}
}
}
Copy link to clipboard
Copied
i don't know why you're seeing that error message, but your code makes no sense. that's not the way you would use a static mehtod/variable.
just make them private and your code will work.
typically you would create a public static method. then you can execute it from any class in your package without needing to create a class instance.
Copy link to clipboard
Copied
Thanks kglad.
I reviewed the code and found out that I was overdoing with the static.
It is enough when only the var numberForFlowerSound is static - keeps track for all instances of that class on the stage, not one by one.
And the function and the sounds do not need to be static.
Got it working as I needed it.
Copy link to clipboard
Copied
kglad, could you please mark your last answer as helpful.
Cant consider it as completely correct answer to my question, but it definitely helped me to solve my issue.
Copy link to clipboard
Copied
sure.
but your solution obviates the only reason to use a static variables: so you can use a public static method.
here's an excerpt from a singleton class (only one instance can be created), for example:
package com.kglad {
public class Data {
private static var data:Data;
private static var allowInstantiation:Boolean;
public static function getInstance():Data {
if (data == null) {
allowInstantiation=true;
data = new Data();
allowInstantiation=false;
}
return data;
}
public function Data():void {
if (! allowInstantiation) {
throw new Error("Error: Instantiation failed: Use data.getInstance() instead of new.");
}
}
public static function getWordAF(level:int):Array{
return data["wordsG"+level]; // these variables are defined in the Data class and are available everywhere in com.kglad by using, var d:Data=Data.getInstance(); d.getWordAF(3);
}