How do I loaded MovieClips in the position of the mouse with a different name on each click? or I have to make several different MovieClips to laod?
please help me :)
Is this what you're trying to do?
var counter:int = 0;
stage.addEventListener(MouseEvent.CLICK, click);
function click(event:MouseEvent):void
{
var mc:MovieClip = new MovieClip(); // Or new YourExportedLibraryClip();
mc.name = 'mc_' + (counter++);
mc.x = stage.mouseX;
mc.y = stage.mouseY;
stage.addChild(mc);
}
Related
I am fairly new to actionscript 3 and I would like to know how can I make EventListener to work only once. Right now it works after every click, but I need to get it work only with first click.
After click it displays a ball on the stage where the click was made. I need to get to the point where only one ball appears and after that click should do nothing.
my code:
stage.addEventListener(MouseEvent.CLICK, onClick,false,0,true);
function onClick(evt:MouseEvent):void {
var ball:MovieClip = new Ball();
ball.x = stage.mouseX;
ball.y = stage.mouseY;
addChildAt(ball,0);
}
You need to call removeEventListener() as follows:
stage.addEventListener(MouseEvent.CLICK, onClick,false,0,true);
function onClick(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.CLICK, onClick);
var ball:MovieClip = new Ball();
ball.x = stage.mouseX;
ball.y = stage.mouseY;
addChildAt(ball,0);
}
One possible solution is to remove the EventListener.
stage.addEventListener(MouseEvent.CLICK, onClick,false,0,true);
function onClick(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.CLICK, onClick);
var ball:MovieClip = new Ball();
ball.x = stage.mouseX;
ball.y = stage.mouseY;
addChildAt(ball,0);
}
Another solution is a simple bool variable, in case you need the eventlistener for something else.
var clickedOnce:Boolean = false;
stage.addEventListener(MouseEvent.CLICK, onClick,false,0,true);
function onClick(evt:MouseEvent):void {
if(!clickedOnce){
clickedOnce = true;
var ball:MovieClip = new Ball();
ball.x = stage.mouseX;
ball.y = stage.mouseY;
addChildAt(ball,0);
}
}
Here is an example:
var table:Table = new Table();
stage.addChild(table);
//table covers the whole stage
for (var i:int = 0; i<= 10; i++){
var book:Book = new Book();
book.x = Math.random() * stage.stageWidth;
book.y = Math.random() * stage.stageHeight;
if (Math.random() < .5){
stage.addChild(book)
}
else {
table.addChild(book)
}
stage.addEventListener(MouseEvent.CLICK, clicked);
function clicked(event:MouseEvent){
trace(event.localX, event.localY);
}
what i need here is the localX or localY OF THE TABLE, not anything else.
so the general question is "how to return event.localX of a certain MovieClip regardless of any other DisplayObject over/under/inside it, without setting the mouseChildren to false (as I need them to be enabled)"
You can use DisplayObject's globalToLocal method to convert a Point from being relative to the stage to being relative to the table object.
function clicked(event:MouseEvent){
var globalPt:Point = new Point(event.stageX, event.stageY);
var tablePt:Point = table.globalToLocal(globalPt);
trace(tablePt.x, tablePt.y);
}
I have this code for a message to appear on stage when player finishes drag and drop. I would like this sprite to be removed when a button is clicked for the next frame. Can someone help me with the code?
stage.addEventListener(Event.ENTER_FRAME, EntFrame);
function EntFrame (e:Event):void
{
if (CntP1+CntP2+CntP3+CntP4+CntP5+CntP6+CntP7+CntP8 == 40)
{
var w:int = 400, h:int = 200;
var win:Sprite = new Sprite();
win.name = "Mywin";
addChild(win);
// draw rounded rect with subtle vertical linear gradient fill and blue stroke
win.graphics.lineStyle(4,0x0077ff);
var mat:Matrix = new Matrix();
mat.createGradientBox(w, h, 90 * (Math.PI / 180));
win.graphics.beginGradientFill(GradientType.LINEAR,[0xffffff,0xeeeeee],[1.00,1.00],[0,255],mat);
win.graphics.drawRoundRect(0,0,w,h,15,15);
// show center "YOU WIN!" text
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.antiAliasType = AntiAliasType.ADVANCED;
tf.defaultTextFormat = new TextFormat("Arial, Verdana",36,0x454545,true);
tf.text = "Κέρδισες!";
tf.selectable = false;
win.addChild(tf);
tf.x = w/2 - tf.width/2;
tf.y = h/2 - tf.height/2;
// add a drop shadow
var dropShadow:DropShadowFilter = new DropShadowFilter(3,45,0,.35,8,8,1,3);
win.filters = [dropShadow];
// center the graphic
win.x = stage.stageWidth/2 - win.width/2;
win.y = stage.stageHeight/2 - win.height/2;
}
}
Your code isn't written well and needs rewriting to ensure reuse or scalability of your project, but here's a quick way out.
make a holder Sprite, something like
var messageHolder:Sprite = new Sprite();
addChild(messageHolder);
add all the messages to that holder in any fashion you like. When you need to erase the contents of that holder, call following method:
function clearHolderContents(holder:DisplayObjectContainer):void
{
if (holder.numChildren < 1)
return; // no need to continue this method if the target is empty
for (var i:int = holder.numChildren - 1; i >= 0; i--)
removeChild(holder.getChildAt(i));
}
This method can clear contents of any DisplayObjectContainer => use it for your messageHolder:
clearHolderContents(messageHolder);
Hope that helps!
How to bring the current target into the movie clip?
If we can set the current target into "selectMovieClip" variable then i believe we should be able to rotate or manipulate it as a MovieClip.
Please copy the script below and you will see what I mean. Currently I can't rotate it.
Please tell me what is wrong with my script. Thank you.
import flash.display.MovieClip;
/*create multiple MovieClip*/
for(var i:int = 0; i < 10; i++){
var widthSquare:int = 100;
var heightSquare:int = 100;
var square:MovieClip = new MovieClip();
square.graphics.lineStyle(1,0x0000CC);
square.graphics.beginFill(0xCCCCCC);
square.graphics.drawRect(0,0,widthSquare,heightSquare);
square.graphics.endFill();
square.name = "RotableClip_"+i;
square.x = 100 + (widthSquare*i);
square.y = 400;
addChild(square);
}
//SET MOVIECLIP
var selectMovieClip;//declare veriable
function onMousePress(evt:MouseEvent):void{
if( evt.target.name.indexOf("RotableClip_")==0){//only allow RotableClip_ to rotate
selectMovieClip=evt;
trace("Selected movie clip" + evt.target.name);
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMousePress);
//Rotate MovieClip
function MouseMove(evt:Event):void{
rotateTarget();//recreate the menu
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
function onMouseRelease(evt:MouseEvent):void {
rotateTarget()
}
//rotateTarget OR at onMousePress
//How do we bring the movie clip into the below and rotate it.
function rotateTarget(){
selectMovieClip.rotation += 90;
trace("Rotate the movie clip");
}
In onMousePress() , you've assigned selectMovieClip to evt, not to evt.target ! Since selectMovieClip is not typed , Flash won't throw an error. Also,if you'd trace selectMovieClip as opposed to tracing evt.target.name, you would have picked up the error!
I have a movieClip with two buttons inside.
The problem is that when the mouse is over these two buttons, the code that manages the movieClip stops working, as if the mouse is not over the MC (the buttons are children of the MC, shouldn't it work regardless?).
Could you please share some advice?
Thanks
/*mc follows mouse. I can't click btns because when mouse rollover btns the mc moves*/
function showImgOptions (e:Event):void{
if (mc.hitTestPoint(mouseX,mouseY,false)){
mc.y = mc.y;
mc.x = mc.x;
}else{
var delayX:int = mc.x - mouseX;
var delayY:int = mc.y - mouseY;
mc.x -= delayX / 6;
mc.y -= delayY/6;
}
}
mc.btn1.addEventListener (MouseEvent.CLICK, closeClick);
mc.btn2.addEventListener (MouseEvent.CLICK, zoomClick);
function closeClick (e:MouseEvent):void{}
function zoomClick (e:MouseEvent):void{}
stage.addEventListener (Event.ENTER_FRAME, showImgOptions);
addChild (mc);
Changed the code to:
var mc:menuMC = new menuMC();
addChild(mc);
var p:Point = mc.localToGlobal(new Point(mc.mouseX,mc.mouseY));
/*mc follows mouse. I can't click btns because when mouse rollover btns the mc moves*/
function showImgOptions (e:Event):void
{
if (! mc.hitTestPoint(p.x,p.y,false))
{
mc.y = mc.y;
mc.x = mc.x;
}else{
//move mc towards mc.parent's mouseX and mouseY
var delayX:int = mc.x - mouseX;
var delayY:int = mc.y - mouseY;
mc.x -= delayX / 6;
mc.y-=delayY/6;
}
}
mc.btn1.addEventListener (MouseEvent.CLICK, closeClick);
mc.btn2.addEventListener (MouseEvent.CLICK, zoomClick);
function closeClick (e:MouseEvent):void
{
}
function zoomClick (e:MouseEvent):void
{
}
stage.addEventListener (Event.ENTER_FRAME, showImgOptions);
And now I get this error:
TypeError: Error #1010: A term is undefined and has no properties.
Here you can download an FLA. Test it and try to click on the buttons 1 and 2, inside the MC following the mouse
hitTestPoint expects stage coordinates:
The x and y parameters specify a point in the coordinate space of the Stage, not the display object container that contains the display object (unless that display object container is the Stage).
Use localToGlobal to get the stage coordinates:
var p:Point = mc.localToGlobal(new Point(mc.mouseX, mc.mouseY));
if(!mc.hitTestPoint(p.x, p.y,false))
{
//move mc towards mc.parent's mouseX and mouseY
}
Solved it!!
Changed the code. I don't know if this helps anybody, but I hope so. Thanks to you all.
stage.addEventListener(Event.ENTER_FRAME, moveMC);
var mc:menuMC = new menuMC();
addChild(mc);
function moveMC(e:Event):void {
if (mc.hitTestObject(big_mc)) {
mc.visible = true;
} else {
mc.visible = false;
}
if (mc.hitTestPoint(mouseX,mouseY,false)) {
mc.y = mc.y;
mc.x = mc.x;
} else {
var delayX:int = mc.x - mouseX;
var delayY:int = mc.y - mouseY;
mc.x -= delayX / 6;
mc.y-=delayY/6;
}
}
mc.btn1.addEventListener(MouseEvent.CLICK, onBtn1);
mc.btn2.addEventListener(MouseEvent.CLICK, onBtn2);
function onBtn1(e:MouseEvent):void {
trace("do something");
}
function onBtn2(e:MouseEvent):void {
trace("do something else");
}