actionscript Blinking tooltip when addChild(textfield); - actionscript-3

Here is piece of class which called right after i already drawn some objects in it, problem is when i have sprite.addChild(textfield) included it starting to blink alot.
addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function mouseOverHandler(e:MouseEvent):void{
//creating a new tooltip instance
var tooltip:Sprite = new Sprite();
/*//we tell the holder to hold our tooltip
holder = tooltip;
//adding text to the tooltip
//tooltip.myText = "ASS";
//positioning the tooltip on the stage
holder.x = stage.mouseX;
holder.y = stage.mouseY - 15;
//adding the tooltip to the stage*/
textfield.selectable = false;
textformat.align = TextFormatAlign.CENTER;
textformat.size = 12;
textformat.color = 0x000000;
textfield.defaultTextFormat = textformat;
textfield.x = x;
textfield.y = y;
textfield.width = width;
textfield.height = height;
textfield.text = myName;
sprite.graphics.lineStyle(2,0x00BB00);
sprite.graphics.beginFill(0xFFFFFF, 1);
sprite.graphics.drawRect(x, y, width, height);
sprite.graphics.endFill();
sprite.addChild(textfield);
sprite.x = stage.mouseX;
sprite.y = stage.mouseY - 15;
tooltip.addChild(sprite);
//holder.addChild(tooltip);
addChild(sprite)
}
private function mouseOutHandler(e:MouseEvent):void{
//we remove the holder when the cursor is outside our button
removeChild(sprite);
}
//we create this function to move the tooltip everytime the cursor is moved
private function mouseMoveHandler(e:MouseEvent):void{
sprite.x = stage.mouseX;
sprite.y = stage.mouseY - 15;
}

Even i'm not sure about that, this might explain your problem. You can give more information for better solution.
"when you add sprite, it calls mouseOutHandler, cause you add your sprite under your cursor. And you remove the sprite with //removeChild(sprite); and mouseOverHandler calls again. And it goes like that."

Related

Actionscript 3 EventListener

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);
}
}

event.localX of a MovieClip regardless of any other DisplayObject over/under/inside

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);
}

Remove sprite from stage in as3

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!

Change cursor of a pop out panel

how can i change the cursor when my cursor is only within this pop out panel and not in the main stage ?
private function launchPopUp(e:MouseEvent):void
{
panel = new Panel();
panel.width = stage.stageWidth;
panel.height = stage.stageHeight;
panel.setStyle("borderAlpha", 1);
PopUpManager.addPopUp(panel, this, true);
PopUpManager.centerPopUp(panel);
}
Would appreciate if anyone could help.
You can use the following code to change the mousecursor. The trick is to hide the mousecursor and replace it with a sprite.
function launchPopUp(e:MouseEvent):void
{
panel = new Panel();
panel.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveFunc);
panel.addEventListener(MouseEvent.MOUSE_OUT, mouseOutFunc);
panel.width = stage.stageWidth;
panel.height = stage.stageHeight;
panel.setStyle("borderAlpha", 1);
PopUpManager.addPopUp(panel, this, true);
PopUpManager.centerPopUp(panel);
}
function mouseMoveFunc(e:MouseEvent):void
{
Mouse.hide();
customMouseSprite.visible = true;
customMouseSprite.x = e.stageX;
customMouseSprite.y = e.stageY;
}
function mouseOutFunc(e:MouseEvent):void
{
Mouse.show();
customMouseSprite.visible = false;;
}
You also need to call the mouseOutFunc when the popup is closed. the customMouseSprite has to be on the top layer of your stage. customMouseSprite can either be a sprite or movieclip (in fact any displayobject).

ActionScript 3.0 Getting Size/Coordinates From Loader Content

i'm attempting to position a textfield to the bottom left of an image that is added to the display list from the Loader() class. i don't know how to access the width/height information of the image.
var dragSprite:Sprite = new Sprite();
this.addChild(dragSprite);
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest("picture.jpg"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayPic, false, 0, true);
function displayPic(evt:Event):void
{
dragSprite.addChild(evt.target.content);
evt.target.removeEventListener(Event.COMPLETE, displayPic);
}
var tf:TextField = new TextField();
tf.text = "Picture Title";
tf.width = 200;
tf.height = 14;
tf.x //same x coordinate of dragSprite
tf.y //same y coordinate of dragSprite, plus picture height, plus gap between picture and text
addChild(tf);
within the displayPic function, i could assign the evt.target.content.height and evt.target.content.width to variables that i could use to position the text field, but i assume there is a more appropriate and direct way?
There is no direct way since you have to wait the image to be loaded to access width, and height.
But you can place you text as soon as the complete is done if it`s fit your design. Store the value into some var so you can reuse it when moving the sprite.
//...
var tf:TextField = new TextField();
tf.text = "Picture Title";
tf.width = 200;
tf.height = 14;
addChild(tf);
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest("picture.jpg"));
imageLoader.contentLoaderInfo.addEventListener(
Event.COMPLETE, displayPic, false, 0, true
);
var offsetX:Number=0;
var offsetY:Number=0;
function positionText():void {
tf.x=dragSprite.x + offsetX;
tf.y=dragSprite.y + offsetY;
}
function displayPic(evt:Event):void {
var li:LoaderInfo=evt.target as LoaderInfo;
if (li===null)
return;
li.removeEventListener(Event.COMPLETE, displayPic);
var dob:DisplayObject=li.content;
if (dob!==null) {
dragSprite.addChild(dob);
// set only once the offset depending on the loaded image
offsetX = ...//
offsetY = dob.height+gap //...
// position text using the offset setted
// so you can reuse the function when moving your sprite
positionText();
}
}