View Full Version : can't even get Adobe provided class to work
Lazer57
11-19-2007, 08:20 PM
I copied and pasted code straight from the adobe help material and I can't get it to work. Below is the class I copied and pasted. I just changed the package to lesson because I have it in the lesson folder and I changed the mp3 name to the one I wanted it to use.
SoundExample.as
package lesson
{
import flash.display.Sprite;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class SoundExample extends Sprite
{
private var url:String = "micRec1.mp3";
private var song:SoundChannel;
public function SoundExample()
{
var request:URLRequest = new URLRequest(url);
var soundFactory:Sound = new Sound();
soundFactory.addEventListener(Event.COMPLETE, completeHandler);
soundFactory.addEventListener(Event.ID3, id3Handler);
soundFactory.addEventListener(IOErrorEvent.IO_ERRO R, ioErrorHandler);
soundFactory.addEventListener(ProgressEvent.PROGRE SS, progressHandler);
soundFactory.load(request);
song = soundFactory.play();
}
private function completeHandler(event:Event):void
{
trace("completeHandler: " + event);
}
private function id3Handler(event:Event):void
{
trace("id3Handler: " + event);
}
private function ioErrorHandler(event:Event):void
{
trace("ioErrorHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void
{
trace("progressHandler: " + event);
}
}
}
I tried putting SoundExample(); into the timeline but that gave me an 1180 error. Then I tried lesson.SoundExample(); in the timeline and got a 1120 error. Make lesson.SoundExample either the class or base class of the mp3 gives me a 5000 error saying that I have to subclass flash.media.Sound. I am very frustrated with classes. It seems so easy to get things to work when I put the code in the timeline, but I need to use classes because the stuff I am going to make will be very repetitive. Please help.
Vikana
11-19-2007, 08:37 PM
Use Flex 2.0 as your compiler.
Hope this answers your question.
Vikana :cool:
Lazer57
11-19-2007, 09:26 PM
Use Flex 2.0 as your compiler.
Hope this answers your question.
Vikana :cool:
I don't understand. Why would Flex 2.0 make it work differently with the same code? Is there a bug or problems with writing code in .as files from Flash CS3?
theRemix
11-19-2007, 11:18 PM
(sadly) they are different compilers... i also thought that they can each compile swfs from the same source equally, and the only difference is the libraries that they came with... maybe that's still true, but you can't compile them interchangeably.
Mazoonist
11-19-2007, 11:27 PM
Many of the examples in the help files are meant to be designated as your Document Class in the fla fle.
Lazer57
11-19-2007, 11:40 PM
(sadly) they are different compilers... i also thought that they can each compile swfs from the same source equally, and the only difference is the libraries that they came with... maybe that's still true, but you can't compile them interchangeably.
So Flex 2.0 compiles the code better or something? I have only published swfs via Flash, never Flex 2.0 so I haven't tried to switch back and forth. I am just really confused. I have trying hundreds of different changes to non-working classes, I have been looking through books and watching vid tutorials, reading the adobe help stuff, looking on forums, and I still can't grasp what the fundamental things that are needed to make classes work. If I keep it to a few lines I can sometimes get it to work. Putting code on the timeline seems almost failsafe, I can get things to work on it all the time.
theRemix
11-19-2007, 11:47 PM
yes "lesson" may not be in your classpath
to make it easy, you can leave it on the project root directory, make a class file named SoundExample.as and change the package line:
package lesson
{
...
to
package
{
...
then compile SoundExample.as
this should work, otherwise, post compiler errors or any output.
Lazer57
11-19-2007, 11:56 PM
Many of the examples in the help files are meant to be designated as your Document Class in the fla fle.
What needs to be changed to make it just a class and have it work? I have tried importing it in the document class and I still get issues.
Lazer57
11-20-2007, 12:06 AM
yes "lesson" may not be in your classpath
to make it easy, you can leave it on the project root directory, make a class file named SoundExample.as and change the package line:
package lesson
{
...
to
package
{
...
then compile SoundExample.as
this should work, otherwise, post compiler errors or any output.
Ok, I did that. First I got an error that said I must subclass the Sound class, so I did extends Sound, took care of that. I changed url to damnURL because there was a problem with that variable. After I did that, no errors occurred but it didn't return any of the trace statements and there is no sound. So I added SoundExample(); to the frame that I wanted the sound to start and the trace statements to go through, but I am getting the following error on the frame that I added it to:
1136: Incorrect number of arguments. Expected 1.
Vikana
11-20-2007, 06:59 AM
Once you have compiled the SoundExample class with no errors, you can create an instance of the class on the stage.
var soundExample:SoundExample = new SoundExample();
Vikana :cool:
Mazoonist
11-20-2007, 10:53 AM
It's not necessary to compile with Flex2, Flash will do just fine. When you press ctrl-enter to test a movie, flash compiles the code into a swf. Flash just needs to be able to find your class file, and it will find it if you use any of the following ways:
1. Save the fla and the as file in the same folder. In this case, the package line in the class should just say "package" with nothing after it. This is the easiest method if you want to just test something really quick. Don't forget to save the fla, though, or Flash can't find the class. Using an import statement won't be necessary.
2. The class is located in a folder that's in your class path. Again, the package line should have nothing else after it, and again, using an import statement won't be necessary. Even saving the fla file is optional now.
3. The class is located in a subfolder somewhere underneath a folder that's in your classpath. In this case, your package line should reflect the location of the file, but you don't include the name of the classpath folder itself. And the external code will need to use an import statement. Example:
If a folder called "Classes" is in your classpath,
and you have subfolders named com, mysite, and lesson underneath Classes
the package line in your SoundExample class would read
package com.mysite.lesson
You would import the class from external code using the line:
import com.mysite.lesson.SoundExample;
Notice that since Classes is in your classpath, you don't use it's name in your coding anywhere, it's just simply a portal or window to whatever lies beneath it. Simple concept, but very important to realize it.
Here's how to set the classpath (The following paragraph is an excerpt from a tutorial I've been preparing on this very subject, and am going to be posting to the tutorials section soon):
There are two ways in Flash to designate a classpath. The first way is in the publish settings for the document, but this only works for one document at a time. I prefer the second way, which sets the classpath for all new documents. So, inside Flash, go to Edit, Preferences. In the Preferences dialog box, click “Actionscript” in the left pane. In the right pane, click the button that says “Actionscript 3.0 settings.” In the Actionscript 3.0 Settings dialog box, click the button with the plus sign (+). Next, click the browse button (it looks like a target) so that you can browse your system to find the folder you just created. When you’ve located the folder, click OK on the browse dialog box. Finally, click OK on the Actionscript 3.0 dialog box, and the Preferences dialog box. Done!
Lazer57
11-20-2007, 04:04 PM
Vikana, Mazoonist, the Remix
It worked. I put it back into the lesson folder, made the adjustments and it worked there too. Wow, thank you very much for taking the time. I was moping around last night and this morning, had decided I would quit trying to learn and put my time into something else. Now I feel reinvigorated. I know I will come up to more problems, but I was stuck at the starting gates for nearly a month and it got me down. Thank you very much!
Mazoonist I hope I remember to keep an eye out for your tutorial. If you get a chance private message me when you post it. The differences in how to deal with classes depending on where they are is essential to know, yet I haven't found much on the topic. I think I understand better now from your post and maybe I will understand it perfectly from your tutorial.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.