PDA

View Full Version : [AS3] Classes "stage" and addEventListener


deAd
12-03-2006, 02:08 PM
Hello! I have an AS3 class that extends sprite. In the class, I create a bunch of Shape objects and add them to the stage..I'd like to change how they look when the mouse moves over them -- but here's my problem.

If I use addEventListener to one of the Shape objects, the specified function is never called...but when I use addEventListener to the instance itself:
addEventListener(MouseEvent.MOUSE_OVER,drawHover);
The function is called outside of the boundaries of the shapes, as if the class' stage is larger than the shape objects. That's probably the case, but I can't resize it because (1) I can't access the stage (stage is null) and (2) if I resize the instance's height it shrinks the shape objects as well...

Any help is appreciated :)

stringy
12-03-2006, 03:08 PM
Shape does not inherit from InteractiveObject so i think you need to put them in either Sprites or mcs-although you could do the same here without using shape at all
something like this
package {
import flash.display.*;
import flash.events.*;

public class Triangle extends Sprite {
public function Triangle () {
// Create the triangle
var t:Sprite = new Sprite();
var triangle =new Shape()
triangle.graphics.lineStyle(1);
triangle.graphics.beginFill(0, 1);
triangle.graphics.moveTo(25, 0);
triangle.graphics.lineTo(50, 25);
triangle.graphics.lineTo(0, 25);
triangle.graphics.lineTo(25, 0);
triangle.graphics.endFill();
triangle.x = 300;
triangle.y = 300;
t.addChild(triangle)

// Add the triangle to the display list
addChild(t);


t.addEventListener(MouseEvent.MOUSE_OVER,drawHover );
}

private function drawHover (e:MouseEvent):void {
trace("triangle");
}
}
}

senocular
12-03-2006, 05:55 PM
as for stage, it is only accessible to objets attached to an active display list (a display list attached to the stage).

deAd
12-03-2006, 09:26 PM
Thanks both of you, but you haven't solved my problem still. I am faced with the same thing as before when I add the objects into a sprite -- the mouse hover event is called when the mouse is way out of the area of the actual Shapes, and when I resize the Sprite it stretches everything in it.

senocular
12-04-2006, 07:46 AM
You might have to provide a more complete example of what you're dealing with.

deAd
12-04-2006, 05:59 PM
Alright. Here's an explanation of the code (followed by the code itself ):

* Create a global variable for 2 Shapes and one Sprite
* Add the two Shapes to the Sprite's display list (addChild)
* Add the Sprite to the class' display list
* In the main code where the class was created, add the class object to the stage

EDIT: sorry, the AS tags messed up.
// In the main section of code -- the CustomButton class is created
var b:CustomButton = new CustomButton('Click here!');
b.x = 50;
b.y = 50;
stage.addChild(b);
// Inside the CustomButton class (the initialization function)
// some of this code has been cut out, like the global variables for things like the color and stuff
public class ShinyButton extends Sprite
{
private var _base:Shape;
private var _gloss:Shape;
private var _container:Sprite;
public function CustomButton(sText:String,nBaseColor:Number = 0xF8C107, bEnabled:Boolean = true, nWidth:Number = 120, nHeight:Number = 30):void{
_container = new Sprite();
_base = new Shape();
_gloss = new Shape();
_container.addChild(_base);
_container.addChild(_gloss);
addChild(_container);
_container.addEventListener(MouseEvent.MOUSE_OVER, drawHover);
_container.addEventListener(MouseEvent.MOUSE_DOWN, drawPressed);
_container.addEventListener(MouseEvent.MOUSE_UP,dr awHover);
_container.addEventListener(MouseEvent.MOUSE_OUT,d rawButton);
drawButton();
}
...
}



However, the events I've added listeners for are called when the mouse enters/leaves/etc an area which is much larger than the _base object's rectangle. I've deduced that this is because the sprite's rectangle is that size on creation. However, if I resize the sprite before called addChild on the shapes, they don't appear. If I resize the sprite afterwards, then the whole thing is stretched and distorted.

senocular
12-04-2006, 06:25 PM
are you confusing ShinyButton and CustomButton?

deAd
12-04-2006, 06:27 PM
Woops no, I renamed it but I forgot to fix that one place, sorry :shifty:

It's actually CustomButton :)