user2220653
user2220653

Reputation: 1

Receiving Error 1026 in AS3 and do not know why

I am getting error 1026 which is "Constructor functions must be instance methods" and I don't know why. I am creating a media player type program and am having trouble with my music portion. I made a function music player before this and copied the code over to my new project. The only thing that is different now is that the code is in a state and not the main.mxml file. This is my code below and I am receiving the error where it says "public var music:Sound;" I don't see why I am getting this error and any help would be appreciated!

<fx:Declarations>
    <fx:XML id="musicXML" source="data/musak.xml" />
    <s:XMLListCollection id="musicCollection" source="{musicXML.song}" />

</fx:Declarations>
<s:HGroup>
    <s:DataGrid id="musicGrid" dataProvider="{musicCollection}" click="onClick()" />
    <local:MusicPosition id="mProgress" width="319" height="83"/>

    <s:VGroup height="55">
        <s:Label text="Now playing: {musicGrid.selectedItem.@name}" />
        <s:Label id="txtPosition" width="91"/>
    </s:VGroup>

</s:HGroup>
<s:Button x="146" y="93" label="play" click="playMusic()"/>
<s:Button x="270" y="93" label="pause" click="pauseMusic()"/>
<fx:Script>

    <![CDATA[

        //set your variables
        public var music:Sound;
        [Bindable]public var musicChannel:SoundChannel;
        public var pausedTime:Number = 0;

        public function onClick():void {
            if(musicChannel) {
                musicChannel.stop();

                //clean up the variables
                music = null;
                musicChannel = null;

            }
            music = new Sound();
            music.load(new URLRequest(musicGrid.selectedItem.file.toString()));
            music.addEventListener(Event.COMPLETE, onMusicLoad);

        }

        public function onMusicLoad(e:Event):void {

            mProgress.update(0);
            //new channel
            musicChannel = music.play();


        }

        protected function onE(e:Event):void {
            if(musicChannel) {
                txtPosition.text = millisecondsToSeconds(musicChannel.position).toString() + "/" +
                    millisecondsToSeconds(music.length).toString();
                mProgress.update(musicChannel.position/music.length);

                mProgress.alpha = 1;
            } else 
                mProgress.alpha = 0;

        }

        protected function millisecondsToSeconds(milliseconds:Number):Number {
            return Math.round(milliseconds/1000);
        }

        public function pauseMusic():void {
            if(musicChannel) {
                pausedTime = musicChannel.position;
                musicChannel.stop();
            }
        }

        public function playMusic():void {
            if(musicChannel) {
                musicChannel = music.play(pausedTime);
            }
        }
    ]]>
</fx:Script>

Upvotes: 0

Views: 671

Answers (1)

JeffryHouser
JeffryHouser

Reputation: 39408

Be very careful when naming thing. You cannot create a variable, or method, inside your class that is the same name as the class itself.

A common--but not required--naming convention is this:

  • Use CamelCase for classes. In this case, your class would be named either Music.mxml or music.as. Based on your code, it seems you already follow this convention with your MusicPosition class.
  • Use camelcase for method, but make the first letter lowercase. Your variable could be named music in this case. Or, you may create a variable named musicPosition. Remember in Flex, the id attribute of an MXML tag is the equivalent of a variable name.
  • For constants, use all uppercase. Constants are commonly used as event types.

Once, again, these are common conventions [in programming languages beyond Flex/ActionSCript] but are not required or enforced by the Flex SDK.

Upvotes: 2

Related Questions