In my Actionscript program, I draw a polygon using the methods:
this.graphics.moveTo()
and
this.graphics.lineTo()
In the update function of the polygon model I change it a bit, and then draw it all over again. Eventually, every call to the update() function draws the updated polygon and I can see it changes.
On some point of the program, I want to be able to use this polygon as a movieclip, so I
can attach a mask to it - so as the polygon drawn over and over again, I could see a nice
background in the form of that polygon, fills it inside.
Problem is - I do not know how to take this array of points I have, which is my polygon representation, and turn it into a movieclip ( if possible at all... )
If you have any other recommendations how to implement the above, It would be great.
You can just create a new MovieClip and use it's graphics for drawing. So instead of using this.graphics.moveTo / lineTo, try this:
var mc:MovieClip = new MovieClip();
mc.graphics.moveTo(...);
mc.graphics.lineTo(...);
this.addChild(mc);
A convenient way of drawing, if you want to type less, is to do something like this:
var mc:MovieClip = new MovieClip();
with(mc.graphics) {
clear();
lineStyle(...);
moveTo(...);
lineTo(...);
...
ect.
}
this.addChild(mc);
I suggest you take a look at Point class. For example,
var p1:Point = new Point(100,150);
Then you can have an array of points
var arrPoints:Array = new Array(p1,p2,p3);
With a for loop, you can decide if i==0 you use moveTo and for the rest use lineTo. You can have a special condition at the end to close your polygon, i==arrPoints.length-1.
So, basically create a movieclip object, use its graphic property to fill it with the points you defined in your array. As long as your points are in a movieclip, you can mask it. Finally, that "this" notation you used is probably referring to the main movieclip which is the stage.
var mc:MovieClip = new MovieClip();
mc.graphics.moveTo(p1.x,p1.y);
will draw your graphics into your mc.
Related
I am trying to give my "buttons" a bounce in effect using the tween class. I am also trying to make my code more efficent by using a function to handle this effect for all my buttons.
var MusicClip:MovieClip = new music_mc();
var MoviesClip:MovieClip = new movie_mc();
var GameClip:MovieClip = new game_mc();
MusicClip.y = 63;
MusicClip.x = 577;
MoviesClip.y = 87;
MoviesClip.x = 401;
GameClip.y = 75;
GameClip.x = 151;
addChild(MusicClip);
addChild(MoviesClip);
addChild(GameClip);
This is where I am having a hard time. I thought I had to tween both the
scaleX and scaleY for all three MovieClips, but the button just appears on the
stage and doesn't animate in. Also if I can put this code in a function so I
don't have to write it for every button (and future buttons) that would be great.
var scaleTween:Tween=new Tween(MusicClip,"scaleX",Elastic.easeOut,0,1,1,true);
var scale2Tween:Tween=new Tween(MusicClip,"scaleY",Elastic.easeOut,0,1,1,true);
I am not getting any error but there is no bouncing in effect.
You need to assign listeners to your MovieClips that will perform an action when you do something. You can use a single event handler for all of your MovieClips:
MusicClip.addEventListener(MouseEvent.MOUSE_OVER,bounceButton);
MusicClip.addEventListener(MouseEvent.MOUSE_OVER,bounceButton);
MoviesClip.addEventListener(MouseEvent.MOUSE_OVER,bounceButton);
function bounceButton(event:MouseEvent):void
{
var scaleTween:Tween=new Tween(event.target,"scaleX",Elastic.easeOut,0,1,1,true);
var scale2Tween:Tween=new Tween(event.target,"scaleY",Elastic.easeOut,0,1,1,true);
}
Note that rather than specifying any one MovieClip as the tween target, you are using event.target, which will refer to the MovieClip you have rolled over.
On an unrelated note, it is good practise to start instance names with a lower case letter. This helps to distinguish them from class names which by convention have uppercase letters to start each word:
var myVariable:MyClass;
How do I convert a point that is inside my MovieClip to coordinates on the stage (main timeline)?
Usually localToGlobal is your friend in this battle.
var point:Point = myMovie.localToGlobal(new Point(10, 10));
If your movie is a child of another one:
var point:Point = myMovieParent.myMovie.localToGlobal(new Point(10, 10));
etc.
for more info see the reference.
The command you want is localToGlobal. That command is used by creating a Point, passing the point to that method of the MovieClip, and then getting the converted point that's returned. Here's the page in the documentation:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#localToGlobal%28%29
You want to use localToGlobal :
var globalPoint:Point = clipParent.MyClip.localToGlobal(new Point(0,0));
here's the livedocs reference :localToGlobal livedocs reference
Not sure why anemgyenge used 10,10 as that would return Point(20,20) if your movieclip was at 10,10 locally.
i have a
dsgnArea----> a movieclip
dsgnArea is masked by dsgnAreaMask, which inturn is a movieclip
dsgnArea.mask=dsgnAreaMask;
the width,height and position of dsgnAreaMask and dsgnArea are same.
i dynamically added multiple movieclips and labels to dsgnArea;
like..
dsgnArea.addChild(movieClip1);
dsgnArea.addChild(movieClip2);
dsgnArea.addChild(label1);
dsgnArea.addChild(label2); and so on...
these movieclips (movieClip1,movieClip2,......) and labels(label1,label2,....) positions can be altered in runtime..
but as i masked the dsgnArea with dsgnAreaMask, only a portion of the added movieClips and labels are visible...
So, my problem is to grab that visible portion in dsgnArea into a bitmap,like a screenshot of that particular dsgnArea, and save it in my server.
please help me out in this problem.
Say s is the DisplayObject object you want to capture and m is the mask applied on it.
var maskRect:Rectangle = m.getRect(s);
var matrix:Matrix = new Matrix(1, 0, 0, 1, -maskRect.x, -maskRect.y);
var w:Number = Math.min(s.width, maskRect.right) - maskRect.x;
var h:Number = Math.min(s.height, maskRect.bottom) - maskRect.y;
var bd:BitmapData = new BitmapData(w, h);
bd.draw(s, matrix);
Does that work?
The BitmapData draw method is what you are looking for. You can use it's clipRect param to define what you would like to draw (the masked parts).
Quasimondo did a handy little method to do this (take a snapshot of the whole displayObject), it's available here: http://www.quasimondo.com/archives/000670.php
I don't know if it works with masked content though.
if it doesn't, the trick would be to translate the whole content by the size of the mask
var bounds:Rectangle = dsgnAreaMask.getBounds( dsgnAreaMask );
instead of using the content of the clip
var bounds:Rectangle = clip.getBounds( clip );
as far as saving file to server is concerned, the question was asked (answered?) here AS3 Save Media File to server
var blockGraphics : Graphics = null;
blockGraphics.clear();
blockGraphics.beginFill(255);
blockGraphics.drawRect(10,10,10,10);
I am trying to simply draw a rectangle but nothing appears on the screen. What am i missing?
Afaik you can't instantiate graphics class..
you need to make a MovieClip or Sprite and work with that.. you can't draw directly to stage.
var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(0xFF0000);
mc.graphics.drawRect(10,10,10,10);
mc.graphics.endFill();
also don't forget to add it to stage
addChild(mc);
var mySprite:Sprite = new Sprite();
mySprite.graphics.beginFill(0x000000);
mySprite.graphics.drawRect(10, 10, 10, 10);
mySprite.graphics.endFill();
addChild(mySprite);
I don't really know a whole lot about the graphics class (I've used it a few times) but I don't believe you can call ANYTHING on a null object.
blockGraphics = null;
blockGraphics.clear();
is the same as calling:
null.clear();
Which is going to give you an error. Typically you'll want to take a movieclip or other such object and get it's graphics instance:
blockGraphics = mc.graphics;
blockGraphics.clear();
blockGraphics.beginFill(0xFF0000);
blockGraphics.drawRect(10,10,10,10);
would draw a red rectangle inside the "mc" movieclip.
i'd like to add all or most of my mouse events to stage, but in order to do that i need to be able to tell what is the type of the sprite being clicked.
i've added two sprites to the display list, one of which is from a class called Square, the other from a class called Circle.
var mySquare:Sprite = new Square();
var myCircle:Sprite = new Circle();
addChild(mySquare);
addChild(myCircle);
now when i click on these sprites, i'd like to know from which class they are from, or which type of sprite it is.
//mousePoint returns mouse coordinates of the stage
var myArray:Array = stage.getObjectsUnderPoint(mousePoint());
if (myArray[myArray.length - 1] is Sprite)
...
so far i know how to do is determine if it IS a sprite display object, but since i'll only be working with sprites i need something more specific. rather than checking "is Sprite", is there a way i can check "is Square" or "is Circle"?
if (myArray[myArray.length - 1] is Square)
You've answered your own question.
if (myArray[myArray.length - 1] is Square)
If this doesn't work, then it should, so something else is wrong.