I am trying to use http://code.google.com/p/as3svgrendererlib/ in my flash cs 5.5 project to import and load svg files.
I downloaded the swc file from http://code.google.com/p/as3svgrendererlib/downloads/list and linked it in actionscript settings.
I tried compiling the project using the following code:
package {
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
public class Main extends Sprite {
import flash.net.URLLoader;
import flash.net.URLRequest;
//ProcessExecutor.instance.initialize(stage);
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = "text";
myLoader.addEventListener(Event.COMPLETE, xmlComplete, false, 0, true);
myLoader.load( new URLRequest("assets/spring_tree_final.svg"));
}
public function xmlComplete(e:Event):void {
trace("it's finished loading");
var svg:SVGDocument = new SVGDocument();
svg.parse(e.target.data);
addChild(svg);
stage.addChild(svg);
}
}
}
But I kept getting the following error:
C:\Users\xx\Desktop\load_svg\Main.as, Line 33 1046: Type was not
found or was not a compile-time constant: SVGDocument.
C:\Users\xx\Desktop\load_svg\Main.as, Line 33 1180: Call to a
possibly undefined method SVGDocument.
So i checked out the source from http://code.google.com/p/as3svgrendererlib/source/checkout and tried importing it using the following line:
import com.lorentz.SVG.*;
right below the 'import flash.events.Event' line.
But I continue to receive the same errors as before.
What am I missing?
You need this import statement:
import com.lorentz.SVG.display.SVGDocument;
Related
I'm trying to create a simple Menu in AS3. There is a sprite called startButton, which when pressed will call the function startGame, and that's it! But, not so easy. I'm using flashdevelop IDE so I'm trying to call a loader to get a .png image file for the spite startButton. But, it doesn't work. There are no error messages, the debugger just does not respond. Any help? Here is the code for both files
Main code:
package {
//Other Files
import Menu;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends Sprite {
//Game values
public static var gameWidth:int = 750;
public static var gameHeight:int = 750;
public function Main() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
addChild(Menu.startButton);
Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
stage.addEventListener(Event.ENTER_FRAME, update);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
//Function starts game
public function startGame(evt:MouseEvent):void {
removeChild(Menu.startButton);
}
//Updates every 60 seconds
public function update():void {
trace("Updated");
}
}
}
And Menu Image code:
package {
//Other files
import Main;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class Menu extends Sprite {
public static function imageLoaded():void {
startButton.addChild(loader);
//initizlize values for startButton Bitmap
startButton.x = (Main.gameWidth / 2) - (startButton.width / 2);
startButton.y = (Main.gameHeight / 2) - (startButton.height / 2);
}
//create startButton Bitmap
public static var startButton:Sprite = new Sprite();
public static var loader:Loader = new Loader();
loader.load(new URLRequest("lib/menustartbutton.png"));
loader.addEventListener(Event.COMPLETE, imageLoaded);
}
}
By the way, I wait for the loader to successfully load the image before working with it, just in case the image takes more time and it draws errors.
The problem is that you misuse static. all static methods/properties are initialized before the classes themselves. As a result static can receive values but they cannot run any code. Running code has to happen after all classes are ready to go which is not the case when static is initialized. In your case startButton and loader are created correctly but the next line never runs 'loader.load'.
Don't misuse static, you are obviously trying to use static to make you code writing and life easier but at the end because you are misusing it you will always end up with more problems.
Here's the code for the sample program, It's a Hello World program.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
/**
* …
* #author Your Name
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry
var greeting:TextField = new TextField();
}
greeting.text = (“Hello, World”);
greeting.x = 100;
greeting.y = 100;
addChild(greeting);
}
}
When I run it, I get 2 syntax errors, both on line 43 \src\Main.as(43): col: 18 Error: Syntax error. What I'm doing wrong? I googled the problem and no luck, Please Help!!! Any Help would be extremely appreciated!! The link to the tutorial is here "https://precisioncode.wordpress.com/2012/09/16/a-tutorial-in-as3-with-flashdevelop/".
The quotes around “Hello, World” are copy pasted from the web and are not the character you get when you type shift and the quote key, delete the copied ones and type them yourself.
I'm trying to link my .as file to my Flash program. Below is the code from my .as file:
package com.project {
import flash.display.Sprite;
import flash.events.*;
public class Program extends Sprite{
public var value:Number;
private var max:Number;
private var min:Number;
function draggable()
{
min = bar_mc.y;
max = bar_mc.height - Erhu_H3_btn.height;
Erhu_H3_btn.addEventListener(MouseEvent.MOUSE_DOWN, dragHandle);
}
function dragHandle(event:MouseEvent):void
{
Erhu_H3_btn.startDrag(false, new Rectangle(0,min,0,max));
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}
function stopDragging(event:MouseEvent):void
{
Erhu_H3_btn.stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
}
}
}
Below is the code in my Flash file:
import com.script.Script;
stop();
var Program:Program = new Program();
Can someone please tell me what I'm doing wrong? I keep getting error 1046! Thanks! :)
You probably missed importing statement
import com.project.Program
I have number of SWF files which should be accessed from external server. To be able to do this I need to have Security.allowDomain in each SWF file. The problem here is that I don't have FLA for those files and there are thousands of SWFs.
If there a better way to configure those files to be accessible from other domain?
Like having some kind of config file or so.
Yes, there is one workaround, but I think it's a security hole, so it can be fixed in any release of flash player. Meanwhile it works now so here is solution - use URLLoader with BINARY dataFormat as preloader for swf bytes:
swf without Security permissions for it scripting:
package
{
import flash.display.MovieClip;
public class astest extends MovieClip
{
public function astest()
{
}
public function externalCheck():void
{
graphics.beginFill(0xFF0000);
graphics.drawCircle(100, 100, 100);
}
}
}
Loader swf that wants to load previous swf and call the externalCheck method:
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class astest2 extends MovieClip
{
private var loader:Loader;
private var urlLoader:URLLoader;
public function astest2()
{
init();
}
//this method works fine
protected function init():void
{
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest("http://domain_with_your_swfs/astest.swf"));
urlLoader.addEventListener(Event.COMPLETE, function(event:Event):void
{
addChild(loader = new Loader());
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);
loader.loadBytes(urlLoader.data as ByteArray);
});
}
//this method will fire SecurityError when calling the 'externalCheck' method
protected function init2(event:Event = null):void
{
addChild(loader = new Loader());
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);
loader.load(new URLRequest("http://domain_with_your_swfs/astest.swf"));
}
protected function onLoader(event:Event = null):void
{
var swf:Object = loader.content;
swf.externalCheck();
}
}
}
Don't forget to place the crossdomain.xml file to the root of your server with swf files to load, without it URLLoader will not able to load the bytes, it's the only security requirement.
I am getting a null object error when I add the mouse event listener for the log in button. (Look at the comments in the constructor)
I am using Flash CS6, and objects such as logInbutton and screen_log_in are instance names from the .fla file. This here is the .as file.
Error I get is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at actions::indexPage()
My AS3 code:
package actions
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IEventDispatcher;
import flash.net.URLRequest;
import flash.display.Loader;
import fl.motion.MotionEvent;
import flash.events.MouseEvent;
public class indexPage extends MovieClip
{
public function indexPage():void
{
loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
//THIS IS THE LINE WHICH IS CAUSING THE ERROR
//WHEN I COMMENT IT OUT THE ERROR IS GONE
logInButton.addEventListener(MouseEvent.CLICK, goToLogIn);
}
var _swfLoader:Loader;
var _swfContent:MovieClip;
public function loadSWF(path:String):void
{
var _req:URLRequest = new URLRequest();
_req.url = path;
_swfLoader = new Loader();
setupListeners(_swfLoader.contentLoaderInfo);
_swfLoader.load(_req);
}
function setupListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE, addSWF);
}
function addSWF(event:Event):void
{
event.target.removeEventListener(Event.COMPLETE, addSWF);
event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
_swfContent = event.target.content;
screen_log_in.addChild(_swfContent);
}
function unloadSwf():void
{
_swfLoader.unloadAndStop();
screen_log_in.removeChild(_swfContent);
_swfContent = null;
}
function goToLogIn(e:MouseEvent):void
{
unloadSwf();
screen_log_in.loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
}
function goToRegister(e:MouseEvent):void
{
unloadSwf();
screen_log_in.loadSWF("http://mathlympics.cu.cc/register.swf");
}
}
}
You can not access stage until stage is available.
public function indexPage():void
{
addEventListener(Event.ADDED_TO_STAGE,init)
}
public function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init)
loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
//THIS IS THE LINE WHICH IS CAUSING THE ERROR
//WHEN I COMMENT IT OUT THE ERROR IS GONE
logInButton.addEventListener(MouseEvent.CLICK, goToLogIn);
}
I am just going to answer my question for the future visitors. The cause of problem I have note been able to figure out but what I have figured out is how to get around it. I simply copy pasted my code from document class to the frames and it works absolutely fine there.