How to add an Object to the stage with nested code in AS3.0 - actionscript-3

I'm busy developing my first application using the AS3.0 language.
I built the full application in Flash Professional, it worked, but it lacked performance.
Now, I'm rebuilding it in Flash builder, so it's more optimised, and it's running great.
Although I've hit a snag.
In Flash Pro, it was very easy for me to add movieclips to the stage, with code nested inside them, so when I added the object, all the code worked like a charm.
In Flash Builder, I have NO clue on how to achieve the same effect... I'm so close to being finished but have no idea how to achieve this.
I've tried making external classes, but with no programming background, it's very hard to find a solution when I don't know where to begin...
SO in short:
HOw to you add objects to your stage using pure AS3 code,
I need to add a graphic object to my stage
And add simple mouse interactivity to it.
Any responses, links, chuckles at me, would be greatly appreciated
Shane

The following code would add a movieclip to the stage, with a line drawn from 0,0 to 100,100 . Provided you are writing this code in the Main class.
var mc:MovieClip = new MovieClip();
addChild(mc) ;
mc.moveTo(0,0)
mc.lineTo(100,100) ;

Related

Flash CS4 AS3 Event not working

I can't attach a simple event handler to a lousy movie clip. Not a single tutorial worked for me and I followed them carefully. Over an hour wasted for nothing... again! Here's what I did:
Layer1: created a symbol(movie clip). Added a rectangle. Draged it onto the layer. Added a name 'obj' in the Properties window. Exported it for ActionScript.
Layer2: Open Action Panel and wrote the following:
obj.addEventListener(MouseEvent.CLICK, move);
function move(event:MouseEvent):void {
obj.x = 200;
obj.y = 200;
}
I don't know what I might be overlooking. I tried with the import flash.events.Event; at the top. Although it wasn't present in the tutorials I've watched(on youtube).
P.S. Needless to say, I'm just starting with ActionScript 3.0 but I am reading about the basics on adobe.com
I think I got it. Adding a name for the movie clip symbol is one thing. But when we drag it onto the stage, then we have to click it. And THEN, in the properties tab of the object on the scene, we give it a (class) name. So that ActionScript can see and use it. Right-clicking on the movie clip in the Library tab and selecting Properties is NOT the properties we're looking for.
Edit: Thanks, akmozo. I just found that out. Took me long enough!
Edit2: Just wanted to point out something - when I save a project in Flash CS4 which has an event handler. The handler didn't work. The code itself, that is. I tried the same project in CS3 and it worked fine. So part of the problem was the program itself(Flash CS4).

How to put code onto objects in as3

I am trying to create a kind of simple game where you move a player around and dodge some enemies. I was wondering if it was possible to add code onto a object in as3.
I want to be able to put code onto an object and not on the main timeline, so when it is on the stage, it will carry out the codes.
Sorry if this is confusing, I don't know how to explain it.
Each MovieClip object has its own timeline. Create a layer for code in your object's timeline and add actionscript in the usual way to it -- using the Actionscript panel.
You can start with a simple 'trace' statement to see that it works.
Please do not put code on the timeline.
Read a tutorial about Object-Oriented Programming in AS3. Learn about Document Classes and how to attach a class to a library object.
Here's a link to one of the first tutorials that popped up when I googled "Flash oop tutorial": http://code.tutsplus.com/tutorials/as3-101-oop-introduction-basix--active-5789

Adobe flash can't run code in a frame

Ok, I'm fairly new to Flash, and I was trying to code some ActionScript now for the last two days. I can't a simple piece of code to work:
stop();
So... pretty simple right? ;)
I have a 10 frame animation, and I'm trying to make the animation stop at frame 5. So I select frame 5 from my timeline and then I open the action menu and I simply write stop(); in the window. When I run it I get the following error message:
In ActionScript 3.0 code cannot be placed directly on objects. Please select an object or use the Code Snippet panel to apply code to the current selection on stage.
I totally understand this is a simple question and the answer might be obvious, but I can't find it...
Thanks
EDIT:
I tried debugging it, and it works when in the debugger but not when in flash...
You're putting the code "on" the object, not allowed in AS3 (thx god).
To avoid it, create a new layer and name it "code" (or whatever you want), and put the code in it

Flash AS3 error when importing 3D-tweened movieclip

I've been working on an AS3 application and it's nearing completion. At the same time, one of the designers I work with has been building a movieclip in a separate .fla that acts as an intro animation to the application. The intro uses the 3D motion tweening capabilities of Flash CS4 / Player 10, and runs fine in the .fla in which it was built.
The problem is that when I import the movieclip into the main .fla for the application, when I dynamically instantiate the movieclip and add it to the stage, I get a barrage of the following runtime error:
ReferenceError: Error #1069: Property null not found on fl.motion.KeyframeBase and there is no default value.
at fl.motion::KeyframeBase/getValue()
at fl.motion::MotionBase/getValue()
at fl.motion::Animator3D/setTime3D()
at fl.motion::AnimatorBase/set time()
at fl.motion::AnimatorBase$/processCurrentFrame()
at fl.motion::AnimatorBase$/parentEnterFrameHandler()
I'm guessing just based on the number of errors like this that I receive that there's one per keyframe in the tweening movieclip. I've checked to ensure that the Flash publish settings are identical across the two .fla files, and although the stage sizes differ slightly, I don't think that's the issue here. I've also googled the issue and found nothing but but this lonely thread on kirupa.
Any thoughts?
Okay--turns out the problem was that we had a local version of the fl.motion package in the Actionscript source paths that was out of date. Now everything's tweening along happily!
As far as I know you once you apply a 3D Motion Tween to a clip, you can no longer modify it by actionscript.
I'd suggest either copying the clip, without the tween, so you can access and modify it via actionscript, as for the animation, maybe go for Copy Motion as Actionscript 3.0. It will spit out a nasty looking bulk of code. The alternative is to 'redo' the animation using something like TweenLite which has nicer syntax. You would select the motion tween, give it an instance then use instance.motion.keyframes to loop through the keyframes and get the position and rotation values for example.
It's not as ideal as it should be :(
Have a look at the flashthusiast.com website for more insights on the new tweens and how to work with them.
Goodluck,
George

Is it possible to create a MovieClip using ActionScript 3 code or MXML?

I'm using the Flex 3 SDK and the free FlashDevelop IDE.
As I don't have FlexBuilder or Flash CS4 Professional I cannot make MovieClips graphically.
So instead I want to create a simple MovieClip using code or MXML. For example, lets say I want to create a MovieClip with 3 frames, and load a bitmap into each frame to create a simple animation.
Is this possible? I've had a good google around and the only examples I can find are of loading existing MovieClips and adding them to a stage.
You can create a movieclip with this simple code:
var mc:MovieClip = new MovieClip();
stage.addChild(mc);
That is of cause just and empty movieclip, you can draw on it with graphics property (see here).
As far as I know there is no way to create frame with actionscript. Though there might be some undocumented methods. There are some functions that do not appear in the documentation (like the addFrameScript method).
I would say the best way (if you absolutly can not use the Flash CS4), would be to have a series of Loader objects, and the hide and show them on every in sequence.
Just put them in an array and listen for the enterFrame event.
You can load in the bitmaps in the Loader objects.
If you use the links and checkout the examples in the documentation, I think you should be able to figure it out.
As far as I've seen, there is no easy way to create a MovieClip in Flex which behaves in a way one might see as comparable to Flash's implementation MovieClip. But I don't think you really want a MovieClip to begin with. Flex does not really play well with non-flex objects. Yes, it is possible to add something to a UIComponent, but you are much better off working withing the Flex framework than doing workarounds.
I would use the mx:Image tag to load your images. It is generally the cleanest way to load an image into Flex. It will let you embed the object into the SWF at compile time, which means that you will not have to point to an outside file. I will caution about having too many embedded graphics -- that will kill your download time and possibly your performance.
If you are only interested in having an animation move or re-size, then I would use the Move and Resize objects which are native Flex Tweens.
Your best option might be to extend the UIComponent class, add a MovieClip as a child-component, and apply the settings from MXML via proxy. e.g.,
public function set movieFrames(value:Array):void {
for each(var b:Bitmap in value) {
//add bitmap to _movieclip object.
}
}
You want a Sprite not a MovieClip. And use time instead of frames. There's a Timer class and a getTimer() function. Use them.
create a class that extends/implements Sprite.
Add a Loader class.
Google it exactly how it's done. (flashtuts.com or sth like that).