PDA

View Full Version : Referring to stage.stageWidth from inside a Class



m90
March 17th, 2009, 04:40 PM
Hello!

I am building a simple ToolTip-Class. In case the cursor gets to close to the stage's border's I'd like it to flip accordingly, so it won't get cropped.

For this I'd like to compare the stage width and my mouseX and then scale and wove things accordingly.

However when I try to get stage.stageWidth from inside my package it will return null.

Can I do this? And if yes how?

Thanks!!!

Digitalosophy
March 17th, 2009, 05:09 PM
You need the stage reference in your other class.

Can I see your code?

m90
March 17th, 2009, 05:16 PM
The class itself looks like (sorry for the German/English-intermingling):



package {

import flash.display.Sprite;
import flash.display.Graphics;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.filters.DropShadowFilter;

public class MouseTip extends Sprite {

public var color:uint = 0xf1ee6a;
public var msg:String = 'Tool Tip';
public var dropShadow:Boolean = true;
public var tipWidth:Number = 140;
public var padding:Number = 10;
public var fontSize:Number = 12;
public var font:String = 'Arial';

private var container:Sprite = new Sprite();
private var txtFormat:TextFormat = new TextFormat;
private var dropShadowF:DropShadowFilter = new DropShadowFilter(8,45,0x000000,0.3,8,8,1,1,false,f alse,false);

public function doMouseTip():void{

for (var i:uint = container.numChildren; i > 0; i--){
container.removeChildAt(i-1);
}

txtFormat.font = font;
txtFormat.size = fontSize;

var txt:TextField = new TextField();
txt.text = msg;
txt.multiline = true;
txt.wordWrap = true;
txt.width = tipWidth;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.selectable = false;
txt.background = true;
txt.backgroundColor = color;
txt.defaultTextFormat = txtFormat;
txt.setTextFormat(txtFormat);

txt.x = 20 + padding/2;
txt.y = - Rectangle(txt.getBounds(stage)).height - padding/2;

var spitze:Sprite = new Sprite();
spitze.graphics.beginFill(color);
spitze.graphics.moveTo(0,0);
spitze.graphics.lineTo(40,0);
spitze.graphics.lineTo(20,-10);
spitze.graphics.lineTo(0,0);
spitze.graphics.endFill();

var hinterleger:Sprite = new Sprite();
hinterleger.graphics.beginFill(color);
hinterleger.graphics.drawRoundRect(20,0,tipWidth + padding,Rectangle(txt.getBounds(stage)).height + padding, padding);
hinterleger.y -= Rectangle(txt.getBounds(stage)).height + padding;

var sprechblase:Sprite = new Sprite();
sprechblase.addChild(spitze);
sprechblase.addChild(hinterleger);

/*if (mouseX > stage.stageWidth - tipWidth+20){
sprechblase.scaleX = -1
txt.x -= 150;
} else {
sprechblase.scaleX = 1;
}*/

if (mouseY < Rectangle(txt.getBounds(stage)).height + padding){
sprechblase.scaleY = -1;
txt.y += Rectangle(txt.getBounds(stage)).height + padding;
} else {
sprechblase.scaleY = 1;
}

container.addChild(sprechblase);
container.addChild(txt);

if (dropShadow){
container.filters = [dropShadowF];
}
else {
container.filters = [];
}

this.addChild(container);

}

}

}


The commented out section is what I want to do but cannot.

The place where I instantiate it looks like:



function mouseOn(e:Event):void{
tip.msg = Rectangle(e.currentTarget.getBounds(stage)).toStri ng();
tip.doMouseTip();
dynamicCursor.addChild(tip);
}


Thanks!!

m90
March 18th, 2009, 05:07 AM
Ok, I got it working by having a "tipStage:Stage" property inside the class that I have to "feed" with the actual stage when instantiating the Class.

Though, I am still wondering if this is neccessary? Is there any way to make the class get the reference automatically.

dandylion13
March 18th, 2009, 05:46 AM
Hi,

No, there's no other way than the way you did it :( This is a big pitfall for developers getting into AS3.0 ... we've all been there!

If your class is bound to a display object, you can access the stage by initializing the class with an ADDED_TO_STAGE event.



public class ObjectOnStage extends MovieClip
{
public function ObjectOnStage()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
//Initialize properties
}
}