How do I build a movie clip in Actionscript 3 - actionscript-3

How do I create the AS3 equivalent of a moviescript with multiple frames?
If I were using the Flash IDE, I would put whatever stuff I wanted on frame 1, other stuff on frame 2, etc. and step through the frames as the user clicks the "Next" button. Or perhaps put in keyframes and tweens and let the system play through the frames at a fixed rate.
I don't see a way to do this in AS3, even though all the descriptions I've seen say that Flash CS3 turns your timeline and frames into ActionScript, and I would like to know how to do the same without having the Flash IDE (e.g., working in Flex).
Let's take a simple example: I have 3 frames. Frame 1 contains the splash page (a lot of text and a button). Frame 2 contains one image, one label, and one button that says "Next". Frame 3 contains two images and one label.
How would you build that in AS3?

Flash CS
You will put your logic code in key frame, and do something with the text,button etc. It's hard for multi uses to edit and work on it.
AS3 use IDE like Flash Builder
Flash CS will is just used to make animation swf
Assume we have a swf named A.swf
A.swf
mySymbol (have a link name like com.mySymbol)
subSymbol1(named subSymbol1)
nameLabel(named label1)
addressLabel(named label2)
subSymbol2(named subSymbol2)
nameLabel(named label1)
addressLabel(named label2)
Here is how you use A.swf in flash builder
Class MyView {
public function MyView() {
var loader:Loader = new Loader();
var url:String = "A.swf";
var urlReq:URLRequest = new URLRequest(url);
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(urlReq, loaderContext);
}
private function onLoadComplete(e:Event):void {
//now you can get defined symbols in A.swf
var c:Class = getDefinitionByName(" com.mySymbol");
//get a mySymbol instance
var mc:MovieClip = new c();
//add to parent
some.addChild(mc);
/*what you said you got three frames,
Just like set like subSymbol1 and subSymbol2 in this A.swf
add some text in subSymbol1 and other in subSymbol2 */
mc.subSymbol1.visible = false;
mc.subSymbol2.visible = true;
}
}
It could make the program and view indenpendent in a way.

You would most likely just create each of those frames as an individual Sprite.
When you want to switch between them, you'd use removeChild() to hide the old one and addChild() to show the new one.
If you want to get fancy you can add Tweens (either built-in or from a tweening engine). This would allow you to fade between the frames, or scale them in, or slide from left to right, or whatever.
If you need to do more complex sequenced animations you can look into something like TimelineLite to help with that.

Related

Replacing movieclip

I m currently working on converting one of my as2 game project into as3 (flash pro cs4). But there is a problem on the background movieclip.
I have a function called disposemc:
function disposemc(mcname:MovieClip):void{
mcname.parent.removeChild(mcname);
mcname = null
}
Another function called changelayer:
function changelayer(mcname:MovieClip,layer:MovieClip):void{
layer.addChild(mcname)
}
So in main timeline frame 2 I have a movieclip on stage (placed in IDE) with instance name "bg_mc", then on main timeline frame 3 I have a DIFFERENT mc on stage with the SAME instance name "bg_mc". The purpose of this is to replace the old bg_mc by the new one.
The problem is,
In frame 2, I applied changelayer function to bg_mc and moved it into a child mc of root by addChild, and then applied a function of my CPU image post processing framework and added the bg_mc into an array.
After some stuffs happened, I went nextFrame to frame 3, and found that the new bg_mc didnot replace the old one, instead it overlaps.
So I tried disposemc function when leaving frame 2 to get rid of the old bg_mc, and at frame 3 I applied changelayer to bg_mc and added bg_mc to my render array.
And I found that the old bg_mc is off the stage but it is still rendering onto my bitmap data, means it is not nullified like it suppose to be in disposemc function. And it is also hard to access it because the name overlaps. When I call bg_mc in frame 3 it is the new one, but the old one still exists, and from the rendered bitmap data it can see it is still playing.
Will making the old mc stop in disposemc function help?
Can anyone give any help? If my structure of making bg is bad, is there any other way to override an instance with a different mc in library?
The purpose of this is to replace the old bg_mc by the new one
You work with MovieClip and frames. So use keyframes by design. If you need to place different background in third frame, don't program it, place it. If you want program, don't use MovieClip with keyframes as main navigation instrument.
Main timeline could be easily swapped with very simple logic and several movie clips with only one frame
//Some MovieClips from the library, with only one frame, still designed in Flash IDE
var currentScene:DisplayObject;
var home: MyHove = new MyHove()
var intro:MyIntro = new MyIntro();
navigate(home);
//after some interaction from the user, go to another page
navigate(intro);
//It will work like gotoAndStop... but better, and give you more control
function navigate(scene: DisplayObject):void{
if(currentScene != null){
removeChild(currentScene);
}
currentScene = scene;
if(currentScene != null){
addChild(currentScene);
//apply logic for swapping background
}
}

Duplication in AS3

So, I've been wondering about this for a while, because if it doesn't work for this new game I'm going to manually have to create hundreds of different movieclips. So, here is what I want to know:
Say if I was developing a game about "Ice Cream" Where you have to create different scoops for your customers.To get an ice cream cone you click the cone and one generates, you can drag it. How do I do this? What I would usually do is create a hundred ice cream cones, make them all invisible, and when it's clicked make the first one visible and if it's clicked again, see if the first one is visible and make the second one visible and so forth. I obviously know they are is an easier way.. but I don't know what that is.
I heard about Duplicating Movie Clips but I read somewhere that it was removed in AS3.
It's possible and quite easy in fact.
1) Click an object and get the class name.
2) Create an new instance of that class and put it in the display list.
3) Move that new instance on every frame to the mouse coordinates until you detect the MouseEvent.MOUSE_UP event.
You will have to tweak the code to fit your project, but this it how it goes. I haven't tested it, but it should work.
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
var duplicate;
var className;
original.addEventListener(MouseEvent.MOUSE_DOWN,duplicateMe);
public function duplicateMe(event):void {
className:Class = Class(getDefinitionByName(getQualifiedClassName(event.target)));
duplicate = new className;
addChild(duplicate);
duplicate.addEventListener(MouseEvent.MOUSE_UP,endDrag);
duplicate.addEventListener(MouseEvent.ENTER_FRAME,update);
}
public function update(event):void {
event.target.x = mouseX;
event.target.y = mouseY;
}
public function endDrag(event):void {
event.target.removeEventListener(MouseEvent.ENTER_FRAME,update);
}
The movieclip type is like the blue print of a movieclip. One can create as many of those movieclips as one needs. For example the movieclip type cat can have an instance with the name Simba. So:
var Simba:Cat = new Cat();
In flash you can simply select the right export to actionscript option to export the type.

Starling - load SWF image(vector) into a Image

I'm trying to convert an old project of mine to use Starling for some extra performance.
I've come to a problem when I'm loading my SWFs and trying to display them.
Here is the code I used before: (no Starling)
private function swfLoaded(e:LoaderEvent):void {
var spr:Sprite = new Sprite();
spr = e.currentTarget.content;
bitmapData.draw(spr ...);
}
The problem when I'm using Starling is that the currentTarget.content is a flash.display.displayObject.
cannot convert com.greensock.loading.display::ContentDisplay#90ffec1 to starling.display.Sprite
I want to find a way to "convert" the flash.display.displayObject into a starling Sprite.
I also want to be able to store the loaded swfs content into a array as a sprite.
Thanks in advance,
Tompa
You're overwriting spr with a different value immediately after you create it, for one thing.
After you do the bitmapData.draw() call:
var tex:Texture = Texture.fromBitmapData(bitmapData, false, false);
The new texture can then be used to create a Starling Image sprite.

How to make a frame1 in the timeline, to be my startpage, when I already have a frame on the first timeline?

I have 3 layers in the timeline, and I want to make a new layer, and then use the new layer to be my first frame (like a startpage).
How do I do that?
Flash does not support show/hide of layers. Learn about working with Movieclips. You should find a lot by just searching for it.
myStartClip.visible = true;
A "shaking screen" means you probably have compile errors.
Don't use layers or frames (aside from the first frame where you put all your code as usual of course).
It's an AS3 exercise, which means you should be working mainly in AS3 code, and not in frames and layers.
You put the "start page" in a container, which can be a Sprite or a MovieClip.
The "start page" can contain for instance a background, a textfield with some instructions and a start button.
This means that you have to create a new container for instance
var startContainer:Sprite = new Sprite();
addChild(startContainer); // and maybe position it with x and y
Then you create the things you need on your start page
var infoTf:TextField = new TextField();
infoTf.text = "some game description and how to play";
var bg:SomeBackground = new SomeBackground();
var startBtn:SomeBtn = new SomeBtn();
startBtn.addEventListener(MouseClick.CLICK, handleMClick_startGame);
And then you add these things to the container.
startContainer.addChild(bg); // add the other things too of course
Then when you press the startBtn, you use removeChild on the entire container and don't forget to remove the eventListener for the startBtn
After that you add the content for the game either directly on the stage, or to another mainContainer if you so prefer.
And in the end you use the same technique for the game over page.

parent.AddChild not working after protecting swf using SWFProtection!

I usually, use SWF Protection, to protect my swf's from decompiling.
It also adds a load bar to the swf.
The problem is that I have codes like this:
logo= new marca();
parent.addChild(logo);
logo.mouseEnabled=false;
I use parent, because I need to put the movie clip above everything, because my application allows the user to add a lot of things to the stage, so a don't want anything covering the logo.
The application works well while unprotected, but If I protect it using SWF Protection, then, I get just white screen. Nothing appears after loading.
Is there another way, to put a movieclip above everything, without being necessery to add a ON ENTER FRAME LISTENER, to update the movieclip deph, to keep it above everything?
You can use addChildAt instead of addChild to add your other clips to the stage under the logo's level.
var levelUnderLogo:uint = numChildren == 1 ? 0 : numChildren-2;
clipYouWantUnderTheLogo = new MovieClip();
addChildAt(clipYouWantUnderTheLogo, levelUnderLogo);
edit: You can also put every other clip inside another clip that is always under the logo.
var base = new MovieClip();
addChild(base);
var logo = new Logo();
addChild(logo);
// Add all your other objects into the "base" clip.
var someClipUnderTheLogo = new MovieClip();
base.addChild(someClipUnderTheLogo);
You are getting the white screen because the swf embed method is breaking in the javascript. Basically you have a javascript error.
Because you did not post code I can't help you much more. All I can say is put alerts in the javascript and see how far you get.