How to make the bitmap generate the SWF file - actionscript-3

The bitmap resource, It's usually make the bitmap generate the SWF file ,and use Loader class to load into the application.
I search some answers from google, find two ways to generate SWF File, one. use mxmlc tool. and another, use jsfl.
I know we can embed the bitmap or swf file into As code. and use mxmlc command like this:
the as file is Vip.as , and the code :
package
{
public class Vip
{
[Embed(source="vip.gif"]
public static var vip:Class;
}
}
and now, I use mxmlc Vip.as
...
It's has Vip.swf file, upload the Vip.swf file to Server.
Then, In flashBuilder, create a new ActionScript project, the application code is :
public class LoadUI extends Sprite
{
public function LoadUI()
{
init();
}
private function init():void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
loader.load( new URLRequest('http://localhost/swfResouce/Vip.swf'));
}
private function completeHandler(e:Event):void {
var loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
}
and debug the application, Error is:
VerifyError: Error #1014: Class Not Found mx.core::BitmapAsset.
I don't know how to use mxmlc generate swf file . And no error when debug the code.
Another way is that use JSFL to generate SWF in flash cs5, But I don't know how to use this. Ah, very pain.

I am guessing you want to display an embedded image (gif).
Here is how its done:
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
public class Main extends Sprite
{
[Embed(source="vip.gif")]
private var embeddedVip : Class;
public function Main()
{
var vip : Bitmap = new embeddedVip();
addChild(vip);
}
}
}

Why don't you just load the images directly (from Loader) and wrap them in a Sprite? A GIF doesn't exactly need a timeline. :)

To avoid
VerifyError: Error #1014
try to compile with static linking:
mxmlc -static-link-runtime-shared-libraries=true -debug=true Main.swf --Main.as
[EDIT]
mxmlc -static-link-runtime-shared-libraries=true -debug=true Main.as

Related

Decompiling a Bin File Inside the SWF file

I decompiled a swf file using JPEXS Free Flash Decompiler v.10.0.0 , When i decompile the swf file I only see 2 scripts:
AppLoader
$gamename-web_sec$aba37a......
In the binaryData folder, there's a thing named:
DefineBinaryData(1:gamename-web_sec$aba37a........
Here's the tree of the decompiled swf:
gamename.swf
Headers
BinaryData
DefineBinaryData(1:gamename-websec$aba37a...
Frames
Scripts
AppLoader
$gamename-web_sec$aba37a...
inside the appLoader, here's the script:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
public class AppLoader extends Sprite
{
private var content:Class;
public function AppLoader()
{
content = §gamename-web_sec$aba37a95a98fb59f905d8915dbd77871-2007405621§;
super();
var loader:Loader = new Loader();
addChild(loader);
var ba:ByteArray = new content();
var bamod:ByteArray = new ByteArray();
bamod.writeBytes(ba,1,ba.length - 1);
loader.loadBytes(bamod,new LoaderContext(false,new ApplicationDomain()));
}
}
}
$gamename-web_sec$aba37a... actionscript:
package
{
import flash.utils.ByteArray;
public class §gamename-web_sec$aba37a95a98fb59f905d8915dbd77871-2007405621§ extends ByteArray
{
public function §chitchatcity-web_sec$aba37a95a98fb59f905d8915dbd77871-2007405621§()
{
super();
}
}
}
I see that It's loading the swf game with that BinaryData. When i try to export the bin file and try to load it again using JPEXS Flash Decompiler again. This what it shows,
[2744232]
header
shapes
sprites
binaryData
DefineBinaryData (51763)
DefineBinaryData (11730)
frames
others
ErrorTag
Unknown (ID=377)
Unknown (ID=909)
Unknown (ID=544)
...
Unknown (ID=879)
ErrorTag
Unknown (ID=777)
....
scripts
frame1
DoAction
It show's so many unknown objects in others folder.
Do P-code source: (because the actionscript source is blank)
Jump loc0005
loc0005:
Do you guys have an idea how will i able to decompile the swf file? I have a permission of the owner, and he wants me to decompile it, so we can better secure the game more.

AS3: How to access and control embedded sounds in an external swf?

I rarely use sounds in AS3/Flash. I am using Flash Pro CS6, but I can't seem to figure out how to access, control (play, stop, etc) sounds embedded in an external SWF loaded into the main SWF.
It's easy to control them when embedded on the main swf. However, on an externally loaded SWR, I get all kinds of errors. For this app, I really need to embed them in the external SWF.
I read several solutions, but none seem to work.
I embed the sound an mp3 file called soundSegment1.mp3 using Flash CS6 import tool and then open the actionscript properties panel on flash to select the class name: SoundSegment1. Then I edit the class code and create a file called SoundSegment1.as and it's saved right next to my document class main.as in the same directory. The code of the SoundSegment1 class looks like this:
package {
import flash.media.*;
public class SoundSegment1 extends Sound
{
public function SoundSegment1 ()
{
// no code in here
}
public function playSound()
{
var soundSegment1:Sound = new SoundSegment1();
var channel:SoundChannel = soundSegment1.play();
}
}
}
Then, in my main.as, I have done several attempts to play this sound such as:
var fileLocation:URLRequest = new URLRequest(SWFToLoad);
loader.load(fileLocation);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
loader.contentLoaderInfo.addEventListener(Event.INIT, initListener);
function initListener(e:Event):void // I also placed this code on the completeListener and it didn't work
{
loader.content.soundSegment1.playSound(); // doesn't work.
}
I get:
Line XXX 1061: Call to a possibly undefined method playSound through a reference with static type flash.display:DisplayObject.
or, I also read that I should be able to do something like this anywhere in the Main.as file:
var theClass:Class = Class(loader.content.getDefinitionByName("SoundSegment1"));
var theSound:theClass = new theClass();
theSound.play() //doesn't work either.
I also tried on the completeListener:
var TheClass:Class = e.target.applicationDomain.getDefinition("SoundSegment1") as Class;
var theSound:TheClass = new TheClass();
theSound.play() //doesn't work either.
I get:
ReferenceError: Error #1065: Variable SoundSegment1 is not defined.
at flash.system::ApplicationDomain/getDefinition()
I am stuck and I really need to get this to work. I would be genuinely grateful for your help.
Thank you in advance for any help provided. I really need to get it to work, because I can't simply embed them in the main SWF or load them individually externally one by one.
Thanks again!
user SnickelFritz from Kirupa gave me a fabulous answer with much easier code, which can be very powerful when using multiple sounds. This is just great, because you can use only one preloader per SWF, instead of having multiple loader for each file:
code for the main.as file
http://www.kirupa.com/forum/showthread.php?305098-Playing-a-embedded-sound-in-an-external-swf
package
{
import flash.display.*;
import flash.media.*;
import flash.events.*;
import flash.net.*;
public class Main extends MovieClip
{
var swf:MovieClip;
public function Main()
{
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
l.load( new URLRequest("child.swf") );
}
private function swfLoaded(e:Event):void
{
swf = e.target.content as MovieClip;
addChild(swf);
swf.playSound();
}
}
}
Code for the loaded swf "child.as"
package
{
import flash.display.*;
import flash.media.*;
import flash.events.*;
import flash.net.*;
public class Child extends MovieClip
{
private var s:Sound1;
private var sc:SoundChannel;
public function Child()
{
s = new Sound1();
sc = new SoundChannel();
// All of the following lines are actually optional, if all your graphic elements are already inside the swf prepared in flash pro
var g:Graphics = this.graphics;
g.beginFill(0x666666);
g.drawRect(0,0,550,400);
g.endFill();
}
public function playSound():void
{
sc = s.play();
}
}
}
User "kglad.com" on the Adobe Forums also gave me a good answer:
http://forums.adobe.com/message/5681137#5681137

How AS3 class files.as work together with FLA file?

I have two questions: Can i write app using just *.as files, and then compile them somehow in SWF? (i am making myself a webpage now)
Secondly, please look at the code, my problem is the same - I can't render text field onto stage, to be visible.
Flash say's 'undefined method addChild.' and 'Access of undefined property tekstuKaste through a reference with static type Class.'
This is a constructor type class inic which kinda serves for initialization, cos all i do is, I make an instance OF THIS class in FLA file by ActionScript, and expect, all application to work.
package {
import pacinas.visuals.*;
import pacinas.visuals.AE_kanva;
public class inic {
public function inic() {
trace("===========");
trace("inicializēt un izsaukt visu no Kanvas klases");
trace("===========");
trace(" ");
var kanvas:AE_kanva = new AE_kanva();
trace(" ");
kanvas.varis();
trace(" ");
trace("===========");
trace("inicializēt un izsaukt visu no Lauki klases");
trace("===========");
trace(" ");
var laukiTxt:BE_tekstaLaukiPrimitive = new BE_tekstaLaukiPrimitive();
trace("");
laukiTxt.simpleText();
addChild(BE_tekstaLaukiPrimitive.tekstuKaste);
}
}
}
There is another EXTERNAL CLASS By whom i hoped to place a rectangles - that does not work too. Example:
package pacinas.visuals
{
import flash.display.Sprite;
public class AE_kanva extends Sprite
{
public function AE_kanva()
{
var kvad:Shape = new Shape();
kvad.graphics.beginFill(0xFF0000);
kvad.graphics.drawRect(0, 0, 100,100);
kvad.graphics.endFill();
addChild(kvad);
trace("konstruktors - zīmē kanvu");
}
public function varis()
{
trace("te glabaas variaabljus");
var ff:int = 4;
var dd:int = 8;
}
}
}
And here is class i hoped will make text box for me (to fill it with XML later)
package pacinas.visuals
{
import flash.text.*;
import flash.display.Sprite;
public class BE_tekstaLaukiPrimitive extends Sprite
{
public var tekstuKaste:TextField = new TextField();
private var kontinents:String = new String ("XML SATURU CMON! a123");
public function BE_tekstaLaukiPrimitive():void
{
trace("teksta rāmis = konstruktora klase");
addChild(tekstuKaste); <--CAN'T GET THIS TO WORK!!!
tekstuKaste.text = kontinents;
}
public function simpleText()
{
trace("nekonstruktora f-cija no Teksta lauki");
}
}}
p.s. I do not use document Class. Ok I will if it's needed. But how?
Can I write app using just *.as files, and then compile them somehow into a SWF?
Yes - using the Flex SDK you can write pure ActionScript and compile it down into a working SWF. FlashDevelop is a good IDE that takes advantage of this.
You will however need to understand how the document class works.
Flash says undefined method addChild. and Access of undefined property tekstuKaste through a reference with static type Class.
In your code, this line is causing your issue:
addChild(BE_tekstaLaukiPrimitive.tekstuKaste);
The first error undefined method addChild is because your class inic does not extend a class that implements the method addChild(). DisplayObjectContainer defines this method, so you'll want to extend that as a minimum, like this:
public class inic extends DisplayObjectContainer
The second error is because you're attempting to access a property of the class BE_tekstaLaukiPrimitive as if it were static. I suspect what you actually wanted to do was this:
addChild(laukiTxt); // laukiTxt is the instance you created.

How could I convert an existing SWF file to an SWC for using as a library?

I am new to the ActionScript world and I bumped into a problem that is hard for me. I have an SWF file that contains some classes that I cannot recompile from source but want to use again in a different project. I see that SWC files are containing compiled classes that can be easily reused. My question is how could I convert the existing SWF file to an SWC file so that I can use it as a regular library? Google did not help me.
SWC files use ZIP compression, and actually contain a SWF. If you change the file extension from .swc to .zip you can browse the contents and directory structure. It looks like this:
foobar.swc
-> catalog.xml
-> library.swf
With some reverse engineering, you might be able to create a SWC from a SWF by building the catalog.xml file and packing them together in ZIP file.
But that seems rather complex! You could also simply load the external SWF into your own SWF using Loader. All of the classes, symbols, and timelines of the external SWF will then be available. Of course, this creates a run-time dependency.
Since your are not able to recompile your code to an SWC, I suggest you to use a Loader if you don't need to access the classes as compile-time.
[Event(name="complete", type="flash.events.Event")]
public class SWFLibrary extends EventDispatcher
{
private var loader:Loader;
private var loaded:Boolean;
public function SWFLibrary(urlOrBytes:*)
{
super();
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
if (urlOrBytes is String) {
loader.load(new URLRequest(urlOrBytes));
} else if (urlOrBytes is URLRequest) {
loader.load(urlOrBytes);
} else if (urlOrBytes is ByteArray) {
loader.loadBytes(urlOrBytes);
} else {
throw new ArgumentError("Invalid urlOrBytes argument");
}
}
public function getAssetClass(className:String):Class
{
if (!loaded) {
throw new IllegalOperationError("The SWF library isn't loaded");
}
return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
}
private function completeHandler(event:Event):void
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
loaded = true;
dispatchEvent(new Event(Event.COMPLETE));
}
}

How to access parent movieclip's variable from loaded swf

I have a swf which load an external swf. The loader swf has a variable which i want to access from the loaded swf. I use MovieClip(parent.parent).myVar but it return this error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at card01_fla::MainTimeline/frame1()
The loader swf has a custom Document Class which i think is the problem.
Thanks in advance.
EDIT
This is the as of the main swf
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.text.TextField;
/**
* ...
* #author chchrist
*/
public class CardSender extends Sprite
{
private var loader:Loader;
private var viewButtonArray:Array;
private var sendButtonArray:Array;
private var cardContainer:Sprite;
public var testVar:String
public function CardSender() {
testVar = "it worked!!!";
init();
}
private function init() {
viewButtonArray = [cardsenderMC.view01, cardsenderMC.view02, cardsenderMC.view03, cardsenderMC.view04];
for each( var viewButton in viewButtonArray) {
viewButton.buttonMode = true;
viewButton.addEventListener(MouseEvent.CLICK, onViewClick);
}
}
private function onViewClick(e:MouseEvent):void {
var cardName = "card"+e.target.name.substring(4)+".swf";
loader = new Loader();
loader.load( new URLRequest(cardName));
loader.contentLoaderInfo.addEventListener(Event.INIT, onCardInit);
}
private function onCardInit(e:Event):void {
addChild(loader);
var content:* = loader.content;
content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
}
private function onCloseClick(e:MouseEvent):void {
removeChild(loader);
}
}
}
and I am trying to access the test variable from the loaded swf with this code
trace(MovieClip(parent.parent).testVar);
EDIT#2
If I addChild(loader) not in the onCardInit but in the onViewClick and in the first frame of the loaded swf i have Object(parent.parent).testVar it works...but why it doesn't want to be inside the onCardInit function?
You have just one loader variable and you're creating four Loader instances and assigning them to it - so if you are clicking all four of them in quick succession, the loader variable will effectively contain the Loader object corresponding to the last click. All your addChild calls will act on that same Loader object - the others will not be added and their parents will continue to be null
Make the following changes to the code:
private function onCardInit(e:Event):void
{
addChild(LoaderInfo(e.currentTarget).loader);
var content:DisplayObject = loader.content;
trace(content);
trace(content.closeButton);
content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
}
private function onCloseClick(e:MouseEvent):void
{
trace(e.currentTarget);//should be the closeButton
trace(e.currentTarget.parent);//should be the loaded swf
trace(e.currentTarget.parent.parent);//should be the Loader object that loaded it
//The following will work only if last three traced correctly
removeChild(e.currentTarget.parent.parent);
}
Answer to the following update in the question:
If I addChild(loader) in the onViewClick instead of onCardInit, then Object(parent.parent).testVar in the first frame of the loaded swf works. Why doesn't it work if I addChild inside the onCardInit function?
Again, that might be because you are adding a different loader object - are you clicking on more than one viewbuttons? Click on a view-button and wait till it is loaded to see it working.
parent.parent is wrong.
try using:
MovieClip(MovieClip(e.currentTarget.parent).parent).myVar ;
You have to cast the first parent to some class like MovieClip and then get this new MovieClip's parent and then getting inside variable.
try tracing parent and parent.parent, I'm sure it's because of an aditional "parent"
loader.content is NOT a movieclip, so it doesn't have the same capabilities. try
myLoadedClip:MovieClip = MovieClip(loader.content);
then your myLoadedClip.parent.myVar should show up
you might want your handler to fire on a Event.COMPLETE instead of on an Event.INIT
This only line code in loaded swf resolves it! ;)
MovieClip(MovieClip(parent.parent).root).play();