I am very new in AS3. I use FlashDevelop 4. I have no Adobe Flash, just FD (pure AS3) I have problems with classpath. I searched the database but I found nothing that could help me. So the problem is as follow.
I have the project "Testing" in C:\AS App\Testing with the file Main.as. I have, also, the folder C:\AS App\W_T. In this folder is the file write_text.as. The file Main.as looks like that:
package
{
import flash.display.*;
import flash.text.TextField;
import flash.events.*;
import W_T.write_text;
public class Main extends Sprite
{
var t:TextField=new TextField();
public function Main():void
{
stage.addChild(t);
stage.addEventListener(MouseEvent.CLICK, write);
}
public function write(me:MouseEvent):void
{
var wt:W_T.write_text = new W_T.write_text();
t.x = 100;
t.y = 100;
t.text = wt.output();
}
}
}
The file write_text.as looks like that:
package W_T
{
import flash.display.*;
import flash.text.TextField;
import flash.events.*;
import W_T.*;
public class write_text
{
public function write_text()
{
}
public function output():String
{
var txt:String;
txt = "From function !";
return(txt);
}
}
}
I make the appropriate specification in the global classpath. But I get the following errors:
Error: Type was not found or was not a compile-time constant: write_text.
var wt:W_T.write_text = new W_T.write_text();
Error: Call to a possibly undefined method write_text.
var wt:W_T.write_text = new W_T.write_text();
Definition W_T:write_text could not be found.
import W_T.write_text;
Could you help me please to find where I have done wrong ?
Thank you.
EB
The problem is the compiler isn't finding your write_text.as file. By default, it will be looking here:
C:\AS App\Testing\W_T\write_text.as
Try adjusting accordingly and see if it works. When you declare package W_T { } flash will look in a subfolder called W_T for a file with the same name as the public class.
When you declare just simply package { } with no name, it will look in the root of your class library path, which seems to be a folder called Testing if that's where your main.as file is living.
If you wanted to use a common folder for your class files (that isn't nested in an individual projects folder), you can tell flash develop where to find them by going to: project -> properties, then the Classpaths tab. By default flash develop will only look in folders relative to the project.
The problem is that you haven't told the compiler (flex?) where to find the file. When you add it as classpath you only tell flash develop where to find the file, not the compiler.
First of all you have to check how you compile and secondly how you can change and add classpaths for your solution.u
I usually do this through ant script and a build.xml file and I can't unfortunately remember how the "pure as3"-projects are working in regards to compiling
Related
I'm trying to use away3d to load a texture. Everything works fine except when embedding a texture inside the project, it crashes and gives that
error: VerifyError: Error #1014: Class mx.core::BitmapAsset could not be found.
The code for embedding the texture:
[Embed(source="../embedded/texture.jpg")]
public var reelTexture:Class;
I'm using Adobe flex builder 3, Away3d 4.1.
The problem is that you don't have framework.swc/core.swc/flex.swc (depending of your Flex SDK version) in your project library path, however linking this swc can lead in increasing the size of your application by ~200kb when compiled with mxmlc, even if you don't use flex directly.
I recommend you do the following (we do this in our pure as3 projects):
1.Create flex library project named flex4embedapi (or flex3embedapi depends on the version of Flex SDK you use in your main project)
2.Create file classes.as:
package
{
import mx.core.BitmapAsset;
import mx.core.ByteArrayAsset;
import mx.core.FontAsset;
import mx.core.SoundAsset;
import mx.core.SpriteAsset;
public class classes
{
public function classes()
{
ByteArrayAsset;
SpriteAsset;
BitmapAsset;
FontAsset;
SoundAsset;
}
}
}
3.Be sure this class is included in the library including list (Project->Properties->Flex library build path->Classes)
4.Copy flex4embedapi.swc to the lib folder (folder with linked swc libraries) of your project.
All should works now.
UPD:
For quick fix try this config file for Flex SDK 4.6 (-load-config=config.xml) it's 100% works for as3 project without any other linked libraries:
<flex-config>
<target-player>11.1.0</target-player>
<default-frame-rate>40</default-frame-rate>
<compiler>
<locale>
<locale-element>en_US</locale-element>
</locale>
<external-library-path>
<path-element>${flexlib}/libs/player/{targetPlayerMajorVersion}.{targetPlayerMinorVersion}/playerglobal.swc</path-element>
</external-library-path>
<library-path>
<path-element>${flexlib}/libs/core.swc</path-element>
<path-element>${flexlib}/libs/player/{targetPlayerMajorVersion}.{targetPlayerMinorVersion}</path-element>
</library-path>
<optimize>true</optimize>
<warn-no-constructor>false</warn-no-constructor>
</compiler>
</flex-config>
Test project contains the only main class embedtest.as:
package
{
import flash.display.Sprite;
import flash.utils.ByteArray;
public class embedtest extends Sprite
{
[Embed(source="somefile.txt", mimeType="application/octet-stream")]
private static const some_file:Class;
public function embedtest()
{
var b:ByteArray = new some_file();
trace(b.length);
}
}
}
Greatings!
I have yet another question concerning the loading of a .swf inside a existing one.
In my game. I have a introduction screen in which I load another .swf (which is a movie).
This all works fine and the URLRequest is this:
request = new URLRequest("Movie.swf");
As I said, this works fine. However when I copy my game.swf and movie.swf to a USB stick.
(I put them in the same directory to prevent other issues).
It doesn't seem to find the movie.swf.
Now I know that it has to do with the path given in the URLRequest and/or the publish settings. But I do not know how to make this so that it searches in the same directory
as the game.swf is in.
I hope you guys have an answer for this issue.
Thanks in advance,
Matti.
Matti, I believe Lukasz's comment is correct about it being a security error.
You can avoid this security error by embedding Movie.swf instead of using a Loader. If you do this, then at compile-time the Movie.swf needs to sit next to the Game.as file, and it will be included in the Game.swf (no need to deliver both files, just Game.swf).
The syntax is:
package
{
import flash.display.Sprite;
public class Game extends Sprite
{
[Embed(source="MyMovie.swf")]
private var myMovieClass:Class;
private var myMovie:DisplayObject;
public function Game():void
{
myMovie = new myMovieClass();
// Technically myMovie is now a Loader, and once
// it's loaded, it'll have .content that's a
// MovieClipLoaderAsset, and .content.getChildAt(0)
// will be your MyMovie.swf main timeline.
}
}
}
Alternately, if you embed it as mimeType="application/octet-stream", you can get the bytes of the SWF and use it in your existing Loader's .loadBytes() method:
package
{
import flash.display.Sprite;
import flash.utils.ByteArray;
public class Game extends Sprite
{
[Embed(source="MyMovie.swf", mimeType="application/octet-stream")]
private var movieBytes:Class;
private var myMovie:DisplayObject;
public function Game():void
{
// Treat this loader the same as your current loader,
// but don't call .load(url), call loadbytes():
var l:Loader = new Loader();
l.loadBytes(new movieBytes() as ByteArray);
}
}
}
I am trying to follow this guide: http://tv.adobe.com/watch/learn-flash-professional-cs5/using-flash-pro-and-flash-builder-together/
While using CS6 Flash Pro and Flash Builder 4.5.
The issue I am having is my Flash Builder code doesn't seem to have any effect. I did everything the guide with the only variation being the names I used and button position.
The code I ended up with:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
public class UntitledMessage extends MovieClip
{
public function UntitledMessage()
{
super();
var icon:btnDontKnow = new btnDontKnow();
icon.x = 0;
icon.y = 0;
addChild(icon);
var btnLabel:TextField = new TextField();
btnLabel.text = "Play Intro";
btnLabel.x = 250;
btnLabel.y = 275;
addChild(btnLabel);
}
}
}
If the source code seems to be ignored, try deliberately inserting a syntax error. Remove a curly brace or something similar.
If there are still no errors, are compiling from a .fla and this is supposed to be the root element, I would try checking the Document Class property in the .fla file. It's under Publish in the Properties tab in Flash CS6. It should state that UntitledMessage is the Document Class and the file has been found.
Also make sure you are editing the file that is actually being read and not a copy. Stupid but common mistake.
I have a game that I've made that runs in two files, one the preloader and one the swf. When I use these two files together they work fine, however some flash portals require only one SWF, so I'm trying to find the shortest-path way to convert it to use only one.
The game swf uses a Document Class that includes the package, class constructor, and tons of functions, and runs fine. I have lots of movieclips on the stage that I refer to in my code. The code is something like this (abridged):
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
...
public class Game extends MovieClip {
var EnemyArray:Array;
var HeroArray:Array;
...
public function Game() { // class constructor
EnemyArray = new Array();
addEventListener(Event.ENTER_FRAME,onEnterFrame);
mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
...
}
public function KeyPressed(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
attack();
...
}
} // End of class
} // End of package
I also have another swf, my preloader, which has the following code in frame 1:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.Event;
var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
var myURL:URLRequest = new URLRequest("Game.swf");
myLoader.load(myURL);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onLoading(event:ProgressEvent):void {
var loaded:Number = event.bytesLoaded / event.bytesTotal;
percent_txt.text = "Loading: " + (loaded*100).toFixed(0) + "%";
}
function onComplete(event:Event):void {
myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
this.addChild(myLoader);
}
I've been through the gamut of solutions online and none of them seem to work with the way that my code is set up, such as: How to create Preloader in AS3
I tried his option 1 by taking all the code in my Document Class other than the package and constructor and pasted it into frame 2. However it returns "Error #1009: Cannot access a property or method of a null object reference." for any references to items on the stage, such as:
mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
As he mentions, "This works well if you organized your project in a way that the most of the assets (images, etc) are in the Flash IDE Library and are not loaded on the first frame (you can check that in each library item's properties)." My project is NOT organized in such a way.
What is the easiest way for me to reorganize my FLA/code so they can have a preloader in a single swf?
Thanks so much for your time and help!
I use FlashDevelop (no FLAs) so I'm not sure if it'll work for you but I use the solution described here.
I hope I can get some help with my actionscript problem.
(inb4: 'What exactly are you trying to accomplish (other than opening an arbitrary file without user permission)?' link)
I am trying to assist with student exercises - so I thought I'd come up with an example of a random gallery. The idea with this would be that students would be able to generate a SWF file, and in the same directory, they would have a folder with images. Then the SWF movie would read the contents of the directory; choose a random image, and display it.
I would have hoped that this could be done both locally (while students develop) - and, should they choose so, they should be able to upload the entire directory online -- and hopefully, the example would work either way. That turns out not to be so easy - so these are my questions:
They say, that in order to get local directory listing, I should use AIR - what does that mean? Is there some executable - other than flashplayerdebugger - that I should call in order to reproduce the SWF if it uses AIR?
Is it possible to get a local directory listing without using this AIR (in the similar vein as the Flex code below?)
It seems I can load a local text file; but I cannot load a local bitmap file. Would it be possible to load a bitmap file as well?
How can you give local access permission to files/folders, if it so happens that you don't have access to the Internet, so you cannot access settings_manager04.html - but you'd like to use a Flash application both with local files and with network sockets on the local network?
Now, one thing is that most students work with Flash CSx versions - I on the other hand am on Linux, so I have tried to make a Flex example; so I've provided the example as Flex code, but I'd like to know possible answers for the Flash CS environment too. The directory structure, compile lines, and code is given at end of this post - but here is how the "application" directory looks like:
Ok, so here is the problem in a bit more detail. First, I thought I'd try to read the directory contents - so I tried the code here: AS3 Basics: List all files in a folder including subdirectories (AIR ... on students' Flash CS - but that seemed to fail miserably.
So I thought - OK, then let's put in a text file in the same directory as the .swf, which will contain a list of the images; e.g.
cat > fileslist.txt <<EOF
img/01.jpg
img/02.jpg
EOF
Then, the Flash app would read this file first - and then proceed to choose a random image from the contents. But at the very first instant, I get:
An ActionScript error has occurred:
SecurityError: Error #2148: SWF file file:///home/rgaltest/rgaltest.swf cannot access local resource file:///home/rgaltest/fileslist.txt. Only local-with-filesystem and trusted local SWF files may access local resources.
at flash.net::URLStream/load()
at flash.net::URLLoader/load()
at rgaltest()[/home/rgaltest/rgaltest.as:23]
So, the answer to this seems to be:
"If you are getting that error when using flash.net.URLLoader and the free flex compiler, add the following option when you compile to get your swf to work: -use-network=false" (juicy tidbits - Only local-with-filesystem and trusted local SWF files may access local resources.)
"Turns out the solution is to configure your Flash Player to trust the swfs in the project’s /bin-debug directory ... 1) Goto http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html ... " (Solution – Error #2010: Local-with-filesystem SWF files are not permitted to use sockets. » Mark Lapasa)
Now, I would not like to have -use-network=false - because students may need to integrate other network based stuff in that example. Then again, something similar to that "settings_manager04.html" ("Global Security Settings panel") page shows up if you open the SWF in a browser, then right-click, then choose "Global Settings" - but does it work for SWF files being shown as a part of Ctrl-Enter build in Flash CS (or correspondingly, those that are open with flashplayerdebugger?) So I try it - choose "Edit Locations", "Browse for folder", choose /home/rgaltest under "Always Allow":
... and now the fileslist.txt gets loaded alright; also when the .swf is ran with flashplayerdebugger.
Right, so now that the files list is loaded, I'd proceed on loading the bitmaps.. so I add the respective code (with onImgLoaded etc), and this is what I get in the log:
At frame #1
file onTextLoaded: img/01.jpg,img/02.jpg,
At frame #2
file onImgLoaded: [object URLLoader]
TypeError: Error #1034: Type Coercion failed: cannot convert "ÿØÿà
...
¿ÿÙ" to flash.display.Bitmap.
at rgaltest/onImgLoaded()[/home/rgaltest/rgaltest.as:64]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
At frame #3
At frame #4
...
Interestingly - the image did get read - as all of its binary contents are dumped to the log! Yet, it cannot be interpreted as bitmap data???!
First of all, this is my directory layout:
mkdir /home/rgaltest
cd /home/rgaltest
touch rgaltest.as
touch fileslist.txt
mkdir img
cd img
convert -size 320x240 gradient:\#4b4-\#bfb 01.jpg
convert -size 320x240 gradient:\#b44-\#bff 02.jpg
chmod -R 777 /home/rgaltest # to ensure no problem with file permissions
The rgaltest.as is given below; I compile that with:
/path/to/flex_sdk_4.6.0.23201_mpl/bin/mxmlc -static-link-runtime-shared-libraries -use-network=true -verbose-stacktraces -debug=true rgaltest.as
... and I check that locally with (make sure mm.cfg is properly set up, see also Using Flash Player debugger):
# in one terminal:
tail -f ~/.macromedia/Flash_Player/Logs/flashlog.txt
# in another terminal:
/path/to/flashplayer_11/flashplayerdebugger rgaltest.swf
And finally, here is rgaltest.as:
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.net.*; //URLLoader;
import flash.display.Bitmap;
import flash.display.BitmapData;
[SWF(width="550", height="400")]
public class rgaltest extends MovieClip {
public var _pictureArray:Array;
public var frameCnt:Number = 0;
public var myTextLoader:URLLoader;
public var isFileslistLoaded:Boolean = false;
public var myImgLoader:URLLoader;
public var isImgLoading:Boolean = false;
// constructor - create/initialize objects here
public function rgaltest() {
this.stage.frameRate = 10; // frames per second
// load the text file with image list
myTextLoader = new URLLoader();
myTextLoader.addEventListener(Event.COMPLETE, onTextLoaded);
myTextLoader.load(new URLRequest("fileslist.txt"));
// add event listener for onEnterFrame
configureListeners(this);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.ENTER_FRAME, my_OnEnterFrame);
}
public function onTextLoaded(e:Event):void {
_pictureArray = e.target.data.split(/\n/);
trace("file onTextLoaded: " + _pictureArray);
isFileslistLoaded = true;
}
public function my_OnEnterFrame(event:Event):void {
frameCnt++;
trace("At frame #" + frameCnt);
if (isFileslistLoaded) {
if (!isImgLoading) {
//load a random image
var rndChoice:Number = Math.floor(Math.random()*2) + 1;
myImgLoader = new URLLoader();
myImgLoader.addEventListener(Event.COMPLETE, onImgLoaded);
myImgLoader.load(new URLRequest(_pictureArray[rndChoice]));
isImgLoading = true;
}
}
} // end onEnterFrame
public function onImgLoaded(e:Event):void {
trace("file onImgLoaded: " + e.target);
var _image:Bitmap = Bitmap(e.target.data);
var _bitmap:BitmapData = _image.bitmapData;
addChild(_image);
}
} //end class
} //end package
Some other relevant links:
flex - Adding Local Filenames/References To Array In Actionscript - Stack Overflow
flash - Is there a way to read local files in ActionScript 3 "silently"'? - Stack Overflow
adobe flash cs4 as3: get current directory of a running project? - Stack Overflow
flex - open local files in AS3 - Stack Overflow
Bump - it seems I solved the bitmap loading (putting it here in separate post, so that I don't clog the OP too much); I just used Loader class instead of URLLoader; see code below (also note the mistake in random calculation in OP, which is corrected below).
Anyways - this is the thing: when I tried the Loader class on the students' Flash CSx, it tended to fail miserably! But, maybe that was because in those cases, we didn't give any permissions to the folder from "settings_manager04.html"? Any special things that should be taken care of, when a code similar to this is generated in Flash?
Here is the reworked rgaltest.as:
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.net.*; //URLLoader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader; //*
[SWF(width="550", height="400")]
public class rgaltest extends MovieClip {
public var _pictureArray:Array;
public var frameCnt:Number = 0;
public var myTextLoader:URLLoader;
public var isFileslistLoaded:Boolean = false;
public var myImgLoader:Loader;
public var isImgLoading:Boolean = false;
// constructor - create/initialize objects here
public function rgaltest() {
this.stage.frameRate = 10; // frames per second
// load the text file with image list
myTextLoader = new URLLoader();
myTextLoader.addEventListener(Event.COMPLETE, onTextLoaded);
myTextLoader.load(new URLRequest("fileslist.txt"));
// add event listener for onEnterFrame
configureListeners(this);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.ENTER_FRAME, my_OnEnterFrame);
}
public function onTextLoaded(e:Event):void {
_pictureArray = e.target.data.split(/\n/);
trace("file onTextLoaded: " + _pictureArray);
isFileslistLoaded = true;
}
public function my_OnEnterFrame(event:Event):void {
frameCnt++;
trace("At frame #" + frameCnt);
if (isFileslistLoaded) {
if (!isImgLoading) {
//load a random image
var rndChoice:Number = Math.floor(Math.random()*2) + 0;
trace("Loading: " + rndChoice + " / " + _pictureArray[rndChoice]);
myImgLoader = new Loader();
myImgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded);
myImgLoader.load(new URLRequest(_pictureArray[rndChoice]));
isImgLoading = true;
}
}
} // end onEnterFrame
public function onImgLoaded(e:Event):void {
trace("file onImgLoaded: " + e.target);
var _image:Bitmap = Bitmap(myImgLoader.content);
var _bitmap:BitmapData = _image.bitmapData;
addChild(_image);
}
} //end class
} //end package